Author Topic: Understanding namespace interaction  (Read 26586 times)

0 Members and 1 Guest are viewing this topic.

Offline TrailMyxTopic starter

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Understanding namespace interaction
« on: June 18, 2008, 01:50:03 PM »
+1
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.




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

There are 1 attachment(s) in this post. You must register and post an acceptable introduction to download
namespace1.JPG
« Last Edit: July 17, 2017, 08:52:49 PM by TrailMyx »
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline gateCrasher

  • Jr. Member
  • **
  • Posts: 60
  • Activity:
    0%
  • Reputation Power: 0
  • gateCrasher has no influence.
  • Respect: +7
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #1 on: July 25, 2008, 06:43:37 PM »
+1
Nice! A lot of new programmers get caught by namespace and scope issues. Thanks for the comprehensive tutorial on this :)

/gC

Offline maverick

  • Newbie
  • *
  • Posts: 1
  • Activity:
    0%
  • Reputation Power: 0
  • maverick has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #2 on: August 22, 2008, 10:02:56 AM »
+1
Nice! good idea..

Offline TrailMyxTopic starter

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Understanding namespace interaction
« Reply #3 on: August 22, 2008, 10:09:08 AM »
+1
Nice! good idea..

Maverick? Est ut vos?
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Hardyz

  • Jr. Member
  • **
  • Posts: 86
  • Activity:
    0%
  • Reputation Power: 0
  • Hardyz has no influence.
  • Respect: +14
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #4 on: October 13, 2008, 05:23:38 PM »
+1
I could never get this and I don't plan on ever getting it :( lol

Offline TrailMyxTopic starter

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Understanding namespace interaction
« Reply #5 on: October 13, 2008, 06:05:31 PM »
+1
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.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Nicar

  • Full Member
  • ***
  • Posts: 198
  • Activity:
    0%
  • Reputation Power: 0
  • Nicar has no influence.
  • Respect: +20
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #6 on: October 13, 2008, 07:53:25 PM »
+2
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.

Offline TrailMyxTopic starter

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Understanding namespace interaction
« Reply #7 on: October 13, 2008, 08:16:45 PM »
+1
Nic, I want some of what you are smoking!!!
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Nicar

  • Full Member
  • ***
  • Posts: 198
  • Activity:
    0%
  • Reputation Power: 0
  • Nicar has no influence.
  • Respect: +20
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #8 on: October 14, 2008, 07:51:50 AM »
+1
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  :)

Offline Khameleon

  • Script Tester - Team Leader
  • Elite
  • *
  • *
  • Posts: 2574
  • Activity:
    0%
  • Reputation Power: 30
  • Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!
  • Gender: Male
  • Respect: +238
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #9 on: January 26, 2009, 10:43:59 PM »
+1
why does this link send me here
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?

Offline TrailMyxTopic starter

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13301
  • Activity:
    0%
  • Reputation Power: 154
  • TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!TrailMyx is awe-inspiring!
  • Gender: Male
  • Viper!
  • Respect: +1349
  • Referrals: 33
    • View Profile
    • ScriptUO
Re: Understanding namespace interaction
« Reply #10 on: January 27, 2009, 04:35:30 AM »
+1
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 .

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
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline Khameleon

  • Script Tester - Team Leader
  • Elite
  • *
  • *
  • Posts: 2574
  • Activity:
    0%
  • Reputation Power: 30
  • Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!Khameleon is a rising star!
  • Gender: Male
  • Respect: +238
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #11 on: January 27, 2009, 01:27:12 PM »
+1
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

Offline b@ndito

  • Jr. Member
  • **
  • Posts: 48
  • Activity:
    0%
  • Reputation Power: 0
  • b@ndito has no influence.
  • Respect: +5
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #12 on: July 12, 2009, 05:40:16 AM »
+1
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 ;)

Offline sayv

  • Newbie
  • *
  • Posts: 5
  • Activity:
    0%
  • Reputation Power: 0
  • sayv has no influence.
  • Respect: +1
  • Referrals: 0
    • View Profile
Re: Understanding namespace interaction
« Reply #13 on: August 11, 2009, 04:47:40 PM »
+1
nice i have not seen any of this information put together as well anyplace else

Offline Noobie

  • Sr. Member
  • *
  • Posts: 262
  • Activity:
    0%
  • Reputation Power: 1
  • Noobie has no influence.
  • Gender: Male
  • Respect: +13
  • Referrals: 1
    • View Profile
Re: Understanding namespace interaction
« Reply #14 on: September 03, 2009, 11:41:21 PM »
+1
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!!