ID:138680
 
Could someone please tell me how global vars work? I'm really stumped.
On 3/24/00 3:46 am Manifacae wrote:
Could someone please tell me how global vars work? I'm really stumped.

var
globalvar = 1

mob
proc
Hello()
if(globalvar == 1)
usr << "Global var is equal to one!"
return

Basically, a global variable can be accessed by simply not specifying src. or usr. in front of it (if it exists, of course).

They're useful for Login() procedures which have to call a world startup proc (though a static var works much nicer), and you can also set them to be moderators, avatars, or whatnot, by simply setting them to equal the usr. Then you can access moderator like so:

if(moderator.health)
...

...instead of having to go thru the trouble of typing out:

var/mob/M
for(M in world)
if(M.moderator)
if(M.health)
...

Both of these would accomplish the same thing with the almost the same amount of memory allocation, but if you have to access moderator functions a lot, it might be a good angle to aim for the first one.

We all know how much of a pain typing is =)
In response to Spuzzum
On 3/24/00 12:46 pm Spuzzum wrote:
On 3/24/00 3:46 am Manifacae wrote:
Could someone please tell me how global vars work? I'm really stumped.

var
globalvar = 1

mob
proc
Hello()
if(globalvar == 1)
usr << "Global var is equal to one!"
return

Basically, a global variable can be accessed by simply not specifying src. or usr. in front of it (if it exists, of course).

They're useful for Login() procedures which have to call a world startup proc (though a static var works much nicer), and you can also set them to be moderators, avatars, or whatnot, by simply setting them to equal the usr. Then you can access moderator like so:

if(moderator.health)
...

...instead of having to go thru the trouble of typing out:

var/mob/M
for(M in world)
if(M.moderator)
if(M.health)
...

Both of these would accomplish the same thing with the almost the same amount of memory allocation, but if you have to access moderator functions a lot, it might be a good angle to aim for the first one.

We all know how much of a pain typing is =)
Tha's what I assumed it was. I guess there's something wrong with my code... Damnit.
On 3/24/00 3:46 am Manifacae wrote:
Could someone please tell me how global vars work? I'm really stumped.

Spuzzum has summarized it pretty well for you. You can also use global.varname to access globals. I think this is better because it makes things more clear and also resolves conflicts when a local or member var has the same name as a global var. Without the "global", the local copy takes precedence.