ScriptUO

Official ScriptUO EasyUO Scripts => Scripting Tutorials => Topic started by: TrailMyx on June 18, 2008, 01:50:03 PM

Title: Understanding namespace interaction
Post by: TrailMyx on June 18, 2008, 01:50:03 PM
I will do my best to help explain how namespaces interact with one another. But first, I think a picture will tell alot about how things work.

(http://scriptuo.com/Pictures/namespace1.JPG)


First thing you can notice is that if you have more than one instance of EUO running, the only way you can transfer information is through system variables, or "*".

But from there, it become more clear how you can transfer information from one "tab" or script to another.

Global namespaces are "global" to all the tabs running in a specific EUO instance. So you can copy information from local to global of Tab 1 and then global to local of Tab 2. I've used this method to allow scripts to interact with one another.

Finally, here's some additional information!

Why do you need namespaces?

First, namespaces are very handy to help the programmer maintain a local variable pool which can be used without fear of corrupting other variables found within their program. Mainly this is evident by subroutines working within their own namespace.

For example, here's a script with a subroutine that computes exponential powers:
Code: [Select]
set !result 2
set !count 0

while !count < 4
{
  gosub POWER !result 2 ; compute !result^2
  set !result #RESULT
  set !count !count + 1
}

display ok !result
stop

; %1 = base, %2 = exponent
sub POWER
  namespace push
  namespace local POW
  set !result 1
  set !count 0
  repeat
    if %2 = 0
      set !result 1
    else
      set !result !result * %1
    set !count !count + 1
  until !count >= %2
  set #RESULT !result
  namespace clear
  namespace pop
return #RESULT
You'll see that !result and !count are used in both the function and the script, but since a new copy is used in the fuction due to the new namespace, the value contained within LOCAL:STD remains untouched. So:

gosub POWER 2 2 ; = 4
gosub POWER 4 2 ; = 16
gosub POWER 16 2 ; = 256
gosub POWER 256 2 ; = 65536

You also notice that we call "namespace clear". That's because since we won't be reusing any of the values computed in this function, we can clear all the contents. If you want to keep values computed in this function, simply omit the "namespace clear" and they will persist until the next time you call POWER.

Caution:
Be careful not to call "namespace clear" after "namespace pop". If you do, you will bring forward the last known namespace, then clear it with potential loss of data from the calling function/namespace. I have see this happen to other people before, and the results can be hard to debug.

Here's another example where a script can copy a value from one namespace to another:

Code: [Select]
set !x 5 ; start out in LOCAL:STD (scope:namespacename)
set !y 20
pause
namespace push ; remember LOCAL:STD
namespace local NS1 ; create a new namespace LOCAL:NS1
set !x 10 ; now LOCAL:NS1 has it's own !x var
namespace copy y from LOCAL STD ; need to use !y from LOCAL:NS1, now a copy is in current namespace
set !temp !x * !y
set #RESULT !temp ; store result such that it can be returned
namespace clear ; clears current namespace or LOCAL:NS1
namespace pop ; bring back last 'pushed' namespace or LOCAL:STD
set !temp !x * !y + #RESULT
display ok The result is , #SPC , !temp
stop
The answer for this is 300. You'll notice that we copy the value of !y from LOCAL:STD to complete the computation. You can also copy information *TO* a known namespace:

Code: [Select]
namespace copy y to LOCAL STD ; copy !y to LOCAL:STDIt's important to note here that you don't need the leading "!" for the copy commands since it's implied with this function.

Finally, you can do wildcard copies to and from namespaces if you need to copy more than one variable:

Copying everthing from current namespace to LOCAL:STD:
Code: [Select]
namespace copy * to LOCAL STDCopying everthing from GLOBAL:NS1 to current namespace:
Code: [Select]
namespace copy * from GLOBAL NS1If you only want to copy a range of items, you can build a selection based on the name:

This copies everything variable staring with "TM_" from GLOBAL:NS1 to the current namespace:
Code: [Select]
namespace copy TM_* from GLOBAL NS1TM
Title: Re: Understanding namespace interaction
Post by: gateCrasher on July 25, 2008, 06:43:37 PM
Nice! A lot of new programmers get caught by namespace and scope issues. Thanks for the comprehensive tutorial on this :)

/gC
Title: Re: Understanding namespace interaction
Post by: maverick on August 22, 2008, 10:02:56 AM
Nice! good idea..
Title: Re: Understanding namespace interaction
Post by: TrailMyx on August 22, 2008, 10:09:08 AM
Nice! good idea..

Maverick? Est ut vos?
Title: Re: Understanding namespace interaction
Post by: Hardyz on October 13, 2008, 05:23:38 PM
I could never get this and I don't plan on ever getting it :( lol
Title: Re: Understanding namespace interaction
Post by: TrailMyx on October 13, 2008, 06:05:31 PM
I could never get this and I don't plan on ever getting it :( lol

Well it really is a kinda squirrley way of managing a block of data.  You might understand it finally, but then applying it can sometimes be even stranger.
Title: Re: Understanding namespace interaction
Post by: Nicar on October 13, 2008, 07:53:25 PM
I think, well, I won't say that part. But think about it like this....

You have lightswitches in the house. Right inside your front door, you have one that contols the outside, on that controls your entry way, and one that controls the hall way. Now, the outside one, it's like, well, your persistant variable, can be used by other instances. Now, your hallway light, you also have a switch for it in the living room, so, you can leave your entry way, walk down your hallway, and into the leaving room, and then use a switch there to turn off the light. Now, this is like your standard variables, you can use them in different rooms (subs). Now, your namespace variable, that's like that light in the entry way. Entering the entry way, you turn on the variable, and when you leave the entry way, you must turn it off, well, you don't have to, but should, you're wasting electricity. Namespace variables are like lights for each room of your house, where the switch is only found in that room. Some rooms have multiple entrys, just like subs can be called multiple times, but, there's that switch, pop/clear, that allows you to see the room (use the namespace)

Yeah, bad analogy, but, heck, i already typed it.
Title: Re: Understanding namespace interaction
Post by: TrailMyx on October 13, 2008, 08:16:45 PM
Nic, I want some of what you are smoking!!!
Title: Re: Understanding namespace interaction
Post by: Nicar on October 14, 2008, 07:51:50 AM
Yeah, and I was completely sober with all that. I'm sure Hardyz can get this, was just trying to lay out that perhaps, something that can be related to... you know, i could delete it all  :)
Title: Re: Understanding namespace interaction
Post by: Khameleon on January 26, 2009, 10:43:59 PM
why does this link send me here
http://winuo.org/new-member-introductions/73-he-o.html (http://winuo.org/new-member-introductions/73-he-o.html)

and how do you create a url link with a name instead of the actual address?
Title: Re: Understanding namespace interaction
Post by: TrailMyx on January 27, 2009, 04:35:30 AM
Freddy must have changed the URL basics for his board, so it borked the hard-links.  I did a search of his site and found the proper link:

http://winuo.org/euo-commands/63-understanding-namespace-interaction.html

For the links, you can just press the hyperlink button above (http://www.scriptuo.com/smf/Themes/default/images/bbc/url.gif).

To make it a LINKY, just edit the url link to look like this:

 [url=http://winuo.org/euo-commands/63-understanding-namespace-interaction.html]LINKY[/url]


LINKY (http://winuo.org/euo-commands/63-understanding-namespace-interaction.html)
Title: Re: Understanding namespace interaction
Post by: Khameleon on January 27, 2009, 01:27:12 PM
ya I did the search and found the link.  used it injunction with c2's login script and lumber script, after some tweaking worked great!
Name spaces can be fun. now I just need to learn why its better to use localy
Title: Re: Understanding namespace interaction
Post by: b@ndito on July 12, 2009, 05:40:16 AM
thank you very much, with the ns copy you solved me an issue i had in my script, i'm making a sort of debugger for persistent variables ;)
Title: Re: Understanding namespace interaction
Post by: sayv on August 11, 2009, 04:47:40 PM
nice i have not seen any of this information put together as well anyplace else
Title: Re: Understanding namespace interaction
Post by: Noobie on September 03, 2009, 11:41:21 PM
Well i was thinking this was really hard but, after this tutorial and a little reading i played with it and it seems really fun so far. Thanks for the tutorial TM!!
Title: Re: Understanding namespace interaction
Post by: UOMaddog on September 05, 2009, 12:22:02 PM
"Namespaces" really are EUO's way of implementating a sort of "stack". In most programming languages (I'll use C as an example), you can call a function to do something to data you pass it, or you can have it return a value to you, or both.

At the Operating System level, when you call a function, you are basically putting any data you're giving it onto the "stack" and telling the function where you put it. When control is handed over to the function, the very first thing is does is make a copy of the variables it was passed so that it can play around with them without corrupting the original values. Essentially it's creating it's own "namespace" where it can play around with "x, y, z, etc" as much as it wants without changing their original values.

After the function completes, it will then "pop" the new values back onto the stack and officially change those values UNLESS some sort of error occurs! That's the beauty of it! Since it's not changing the actual values, if an error occurs, the function can hand control of the program back to whatever called it and say "Oh yea, btw, something was all screwed up so I couldn't do what you wanted" and hands back the original values.

Now this is a very watered-down version of what happens, because it all depends on the error controls of the function. Some functions may return the original values as well as the messed up values so that you can analyze what went wrong. There are many other possibilities, but you get the general idea.

So here's a quick summary of namespaces:

Quote
Person 1:
"Hey, I need you to take a look at something and figure something out"

Person 2:
*looks at what person 1 is showing him and jots it down on his own sheet of paper*

Person 1:
*waits patiently while person 2 is working*

Person 2:
"Hey, I did what you wanted, here it is!"
*writes down the results on person 1's original paper*

Person 1:
"Thanks!"
*take his results and goes to do whatever with them*

Now, EUO is a very BAD implementation of this! gosub's are supposedly "functions" but arguments are not passed the same way as they would be in any other scripting language. Type's are pretty much non-existent in EUO. There is no difference between numbers, strings, arrays, or anything else. And let's not even get started with pointers. I guess the moral of this story is, don't ever model anything after EUO!!
Title: Re: Understanding namespace interaction
Post by: OMGBurgers on March 23, 2011, 02:50:30 PM
Edited -

I didn't fully understand push/pop and I think that's what was screwing me up.

I was under the assumption you needed to save your namespace variables with push, but you don't.
Push simply stores/saves the CURRENT namespace name/type, as tm said in his post "Remembers", or easyuo wiki says "stores ....... in an internal stack".
Pop simply restores/sets/whatever the current namespace to whatever namespace name/type is "remembered"/"stored internally". (?)


namespace local main   ;sets current local/main
namespace push   ;stores local/main
namespace local setup   ;sets current local/setup
namespace push   ;stores local/setup
namespace local scan   ; sets current local/scan
namespace clear   ;cleares current namespace local/scan
namespace pop   ;restores current namespace to local/setup
namespace clear   ;cleares current namespace local/setup
namespace pop   ;restores current namespace to local/main

I thought that easyuo wouldn't be able to pop twice, to restore the main namespace, but it does.  I suppose it kinda "queues" namespaces in a "list", as you push namespaces, they go to the top of the "list", when you pop a namespace, it restores the namespace on the top of the "list", removes it from "queue", and you can continue to pop, until your "list" of "queued" namespaces has been depleted.


I hope that makes sense to anyone, and is now correct... lol
Title: Re: Understanding namespace interaction
Post by: TrailMyx on March 23, 2011, 03:40:09 PM
Really it's more like a stack (i.e. push/pop), but I think you get it.  ;)

http://en.wikipedia.org/wiki/Stack_%28data_structure%29
Title: Re: Understanding namespace interaction
Post by: OMGBurgers on March 23, 2011, 04:06:47 PM
makes sense!

i guess thats where the "internal stack" comes from, as described on the easyuo wiki.

once i understood how push/pop worked, it's been easier.  especially when i found those namespace variables that store current namespace, and current namespace type! lol
Title: Re: Understanding namespace interaction
Post by: Pearls on July 02, 2014, 06:15:46 AM
Thanks :) Pretty easy in theory hehe :D
Title: Re: Understanding namespace interaction
Post by: TrailMyx on July 02, 2014, 10:16:42 AM
There's no data structures in EUO, so this is really the closest you can get.
Title: Re: Understanding namespace interaction
Post by: Pearls on July 02, 2014, 10:43:56 AM
There's no data structures in EUO, so this is really the closest you can get.

And... now you've lost me haha :D