Author Topic: Namespace var interaction  (Read 6611 times)

0 Members and 1 Guest are viewing this topic.

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Namespace var interaction
« on: February 13, 2010, 05:01:07 AM »
0
A quick question just to make sure I understand namespace var interaction.

Consider the following:
Code: [Select]
gosub 1
gosub 2
Display %_var
Halt

sub 1
   namespace push
   namespace local 1
   set %_var Hello
   namespace pop
return

sub 2
   namespace push
   namespace local 2
   set %_var World
   namespace pop
return
I was assuming that proper Namespace execution would mean that this snippet wouldn't display anything (NULL) however it does indeed display "World".

Now consider the following:
Code: [Select]
gosub 1
gosub 2
halt

sub 1
   namespace push
   namespace local 1
   set %_var 1
   set %_var2 %_var + 9
   Display %_var2
   namespace pop
return

sub 2
   namespace push
   namespace local 2
   set %_var2 %_var + 9
   Display %_var2
   namespace pop
return

Again, proper Namespace execution would have me assume that this code would show "10" as the first Display value (from sub 1) and "9" as the second Display value (from sub 2) but it does indeed display "10" for both.

I would have thought that the reason to use Namespace would be to isolate variables and even their values to the namespaces in which they were defined. Not only are the values not stored but the variables aren't even isolated. I don't understand the point of using Namespace if this is the case. Maybe I need to go re-read the Namespace tutorials TM and Cerv have so kindly put together but I was a little annoyed this morning when I discovered my use of Namespaces in a script assumed they were implemented correctly and this idiotic assumption has caused me a ton of needless troubleshooting on the next version of my miner LOL.

Anyhow, I'm running off little sleep so hopefully this discussion makes sense.

Any constructive input is welcome!

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline Paulonius

  • Elite
  • *
  • *
  • Posts: 2040
  • Activity:
    0%
  • Reputation Power: 29
  • Paulonius is on the verge of being accepted.Paulonius is on the verge of being accepted.Paulonius is on the verge of being accepted.Paulonius is on the verge of being accepted.Paulonius is on the verge of being accepted.
  • Respect: +162
  • Referrals: 1
    • View Profile
Re: Namespace var interaction
« Reply #1 on: February 13, 2010, 05:26:02 AM »
0
Also interested in getting the answer and posting to get a notice. I wonder if we could put a toggle on threads to add yourself without putting in a post that doesn't add value...
This coin declares Caesar is "Dictator for Life." He did serve as Dictator for the remainder of his life, but his life would end only a few weeks after this issue. For Caesar to put his image on coins and essentially declare himself king was too much for Brutus and his republican allies.

"If everything seems under control, you're not going fast enough'
-Mario Andretti

"If everyone is thinking alike, someone isn't thinking."
- General George Patton Jr

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Namespace var interaction
« Reply #2 on: February 13, 2010, 05:44:32 AM »
0
Ok so I thought to try values between and outside of subs/namespaces to see if that makes a difference and the following results in all displays being the value of "2" which is the last value set:

Code: [Select]
set %_var 0
gosub 1
display %_var
halt

sub 1
   wait 10
   namespace push
   namespace local 1
   set %_var 1
   gosub 2
   Display %_var
   namespace pop
return

sub 2
   wait 10
   namespace push
   namespace local 2
   set %_var 2
   Display %_var
   namespace pop
return

So obviously I'm missing something as to why I would bother or I'm simply executing namespaces wrong.

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • 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: Namespace var interaction
« Reply #3 on: February 13, 2010, 09:05:55 AM »
0
Remember you have to reference namespace variables with the "!" and not the "%" qualifier:

set !_var 0
namespace copy _var to local 0
gosub 1
display !_var
halt

Code: [Select]

sub 1
   wait 10
   namespace push
   namespace local 1
   set !_var 1
   gosub 2
   Display !_var
   namespace pop
return

sub 2
   wait 10
   namespace push
   namespace local 2
   set !_var 2
   Display !_var
   namespace pop
return

"%" variables are available in ALL namespaces.
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Namespace var interaction
« Reply #4 on: February 13, 2010, 10:08:35 AM »
0
Ahh... like I said, my execution is probably wrong. Awesome, thanks for clearing that up TM.

Everything is executing as expected now, thanks. It's funny how after all this time I still have so much to learn and can miss such "minor" details hehe.

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • 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: Namespace var interaction
« Reply #5 on: February 13, 2010, 10:33:29 AM »
0
If you look at the picture I did for the namespace interaction, you'll see what I mean.

http://www.scriptuo.com/index.php?topic=108.15
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Namespace var interaction
« Reply #6 on: February 14, 2010, 05:36:16 AM »
0
Funny that I've always viewed the difference between "!" and "%" entirely incorrectly.

With that said, why is it that so many people use Namespace variables in the main body of their scripts? Would this make these variables available ONLY in the "default" namespace?

Consider the following:

Code: [Select]
set !Var1 Hello
gosub sub1
Display !var1 , #spc , !var2
halt

sub sub1
   namespace push
   namespace local Test
   set !var2 World
   Display !var1 , #spc , !var2
   namespace pop
return
I'm assuming the results of the first Display that runs (in the sub) will be "N/A World" and the result of the second Display (in the main body before Halt) would be "Hello N/A". Am I on the right track?

I suppose I could go test this and get my own answer.

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • 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: Namespace var interaction
« Reply #7 on: February 14, 2010, 09:41:01 AM »
0
Yup, exactly.

When you try and reference the "!" variables (either local or global), all you are doing is looking into the place that described with the #NSNAME and #NSTYPE. system variables.  You can think of this sort of like a big array.  When you change the #NSNAME and #NSTYPE variables, you basically point to a whole new memory pool.  That pool is addressable to only on script running (#NSTYPE = LOCAL) or all scripts running in an EUO instance (#NSTYPE = GLOBAL).

Also notice that #NSNAME and #NSTYPE have default values of std:local.  That means you can use "!" referenced variables immediately.
 
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Namespace var interaction
« Reply #8 on: February 14, 2010, 11:45:38 AM »
0
Awesome help here, thanks!

So, if you don't mind TM (or anybody else) if you could entertain a couple more questions.

First, a question regarding var types being passed to a sub declaring a new #NSNAME:

Code: [Select]
set !var1 Hello
gosub sub1 !var1
halt

sub sub1
   namespace push
   namespace local Test
   set !NSVar1 %1
   Display !NSVar1
   namespace pop
return

So, as #NSNAME Test knows nothing about !var1 in the default space why does passing the variable actually work? Wait, maybe I've just answered my own question. Is it becaue the value of !var1 gets assigned to the %1 global variable thus making it available to all namespaces as the currently declared %1? That just makes too much sense.

On a similar note, I'm assuming that any variable being set in a declared namespace that will be used in another namespace (default or otherwise) will need to be a global thus the following won't work:

Code: [Select]
gosub sub1
Display !NSVar1
halt

sub sub1
   namespace push
   namespace local Test
   set !NSVar1 Hello , #spc , World
   namespace pop
return !NSVar1

Now what would be cool is if namespace vars were indeed like an array in that they could be called throught a script such as #NSNAME:!NSVarName (e.g. #TEST:!NSVar1) - now this would be cool!! ;)

Last question - do the variable values in a namespace get saved in memory until overwritten? In other words, can I repeatedly go back to a namespace and further modify a NS variable?

Code: [Select]
for %i 1 10
   gosub sub1
halt

sub sub1
   namespace push
   namespace local Test
   set !NSVar1 !NSVar1 +1
   Display !NSVar1
   namespace pop
return

Would the result of the above be 1,2,3,4,5,6,7,8,9,10 in 10 Display iterations?

X
« Last Edit: February 14, 2010, 12:20:24 PM by 12TimesOver »
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • 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: Namespace var interaction
« Reply #9 on: February 14, 2010, 02:37:29 PM »
0
Yup, you answered your own question.  Subroutine arguments are "%" vars, so they are available in ALL namespaces.  However, they will be unique for each script you run.  So Tab1 EUO will have their own set of "%" vars and Tab2 EUO will also be separate.  Local namespace variables function very similarly to "%" vars.  Remember that namespace variables were added well after the first implementation of EUO.  You can consider "%" variables as being legacy.

Here's an example that might answer one of your questions:

Code: [Select]
set !NSVar1 FIRST
gosub sub1
Display !NSVar1 ; hey! it's still FIRST.
namespace copy NSVar1 from local Test
Display !NSVar1 ; There now it's SECOND.
halt

sub sub1
   namespace push
   namespace local Test
   set !NSVar1 SECOND
   namespace pop ; Whoops!  You just flopped back to the original namespace!
return !NSVar1

Finally, once you start writing variables in a namespace, they will persist until you clear the namespace.  That's what the "namespace clear" command does for you.

If you have any questions, check out the Vardump feature of EUO.  You can see all the variables as they have been declared.
« Last Edit: February 14, 2010, 02:40:34 PM by TrailMyx »
Please read the ScriptUO site RULES
Come play RIFT with me!

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Namespace var interaction
« Reply #10 on: February 14, 2010, 03:03:45 PM »
0
You know I was totally looking for a good example for Namespace Copy too, this is great stuff. I'm not sure why it finally started making sense all of a sudden but I'm on a roll this week!! ;P

Thanks much, I'm off to implement now!

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Namespace var interaction
« Reply #11 on: February 26, 2010, 02:43:44 AM »
0
Man, I'm obviously still missing something. So I understand that the "default" NS is STD. So I would assume, and looking at your examples above I should be correct, that if you set !NSVar1 in the main body of the script that 1)it will not be available in a different namespace and 2)it will still be set when I return to the STD namespace.

Example:

Code: [Select]
set !NSVar1 Hello
gosub 1
Display !NSVar1 , #spc , !NSVar2  ;<-----result here should be "Hello N/A"
Halt

sub 1
   namespace push
   namespace local 1
   set !NSVar2 World
   Display !NSVar1 , #spc , !NSVar2  ;<-----result here should be "N/A World"
   namespace pop
return

Rather than getting the above results in a script I'm working on, when I do a Namespace Pop in the sub and return to the STD NS the variables I set in the main body script stay N/A rather than returning to their original values. In other words, the "Display !NSVar1, #spc , !NSVar2" is resulting in "N/A N/A". Shouldn't the originally set value of !NSVar1 be restored?

Hopefully this reads correctly.

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Offline TrailMyx

  • Officially retired from UO
  • Administrator
  • *
  • *
  • Posts: 13302
  • Activity:
    0.2%
  • 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: Namespace var interaction
« Reply #12 on: February 26, 2010, 05:29:06 AM »
0
Hmm, works for me.
Please read the ScriptUO site RULES
Come play RIFT with me!

Scrripty

  • Guest
Re: Namespace var interaction
« Reply #13 on: February 26, 2010, 10:46:05 AM »
0
Hmm, works for me.

That was fun to read... I've been looking at namespace use also.  Keep going please... hehe

Offline 12TimesOverTopic starter

  • Another Day, Another Vendetta
  • Global Moderator
  • *
  • *
  • Posts: 3694
  • Activity:
    0%
  • Reputation Power: 41
  • 12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.12TimesOver is a force to reckon with.
  • Gender: Male
  • Respect: +321
  • Referrals: 2
    • View Profile
Re: Namespace var interaction
« Reply #14 on: February 26, 2010, 11:23:26 AM »
0
Hmm, works for me.
Well I can't actually run my example at the moment so it must be something I'm doing wrong in my application yet again. I'll bet I'm jumping from STD to a new NSName then to another NSName but not Pop'ng somewhere before dropping back to STD.

As always, thank you Mr. Miyagi!

X
When they come for me I'll be sitting at my desk
     with a gun in my hand wearing a bulletproof vest
My, my, my how the time does fly
     when you know you're gonna die by the end of the night

Tags: