Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global variables and Local Map variables.
Homicide13 Offline
Senior Member

Posts: 323
Threads: 41
Joined: Nov 2010
Reputation: 14
#1
Global variables and Local Map variables.

What exactly is the difference between Global Variables (ie declaring a variable within a local scope) and Local Map Variables (ie using the SetLocalVarInt function)? Is there any reason why I would want to use a Local Map Variable over a Global Variable? Being able to simply declare a variable in the conventional manner just seems a lot simpler to read and less complicated to me.

04-28-2012, 08:56 PM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#2
RE: Global variables and Local Map variables.

Global variables will reset each time the script is run. (e.g. every time the player re-enters the map, or loads a saved game.) Using the API for variables will make them retain their value. As an example of how this could be problematic, consider a switch-case structure using a global variable used to run a scary event with the shadow roar, falling dust particles, sanity loss, etc etc. If the player (for some reason) exits the map and re-enters, the event might start over or stop partway through. Could be consequential if some critically important things happen during the event, like a door breaking or an item becoming "pickable".

Edit: Apjjm is still the sensei of everything.

(This post was last modified: 04-28-2012, 09:02 PM by palistov.)
04-28-2012, 08:58 PM
Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#3
RE: Global variables and Local Map variables.

The scope with which the variables can be accessed is equivalent. However, Global variables (in terms of the script) are not restored when a save & load occurs. "SetLocalVar" functions let the values associated with the variables be preserved when saves occur, and as such you should really be using those with the exception of a few very particular circumstances.

Edit: Ninja'd.
(This post was last modified: 04-28-2012, 09:01 PM by Apjjm.)
04-28-2012, 09:00 PM
Find
Cranky Old Man Offline
Posting Freak

Posts: 986
Threads: 20
Joined: Apr 2012
Reputation: 38
#4
RE: Global variables and Local Map variables.

(04-28-2012, 09:00 PM)Apjjm Wrote: The scope with which the variables can be accessed is equivalent. However, Global variables (in terms of the script) are not restored when a save & load occurs. "SetLocalVar" functions let the values associated with the variables be preserved when saves occur, and as such you should really be using those with the exception of a few very particular circumstances.

Edit: Ninja'd.
That's really counter-intuitive. Confused


Noob scripting tutorial: From Noob to Pro

04-28-2012, 09:06 PM
Find
Homicide13 Offline
Senior Member

Posts: 323
Threads: 41
Joined: Nov 2010
Reputation: 14
#5
RE: Global variables and Local Map variables.

I see, so global vars are re-declared every time the script is run, but local map vars are more like (if they aren't actually, though I don't have a very good knowledge of game scripting as a whole yet) private class variables for each map.

I feel like this could cause a memory leaks though. Are local map variables kept in memory every session (whether the player is in the map that uses them or not), or are the read/written to some save file somewhere or something and only loaded during loading screens when they're needed?

04-29-2012, 01:37 AM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#6
RE: Global variables and Local Map variables.

Haha what? Memory leak? The variables are stored in the .sav file, as far as I can detect. They're probably read from the save file and stored in memory, then the opposite happens when the game is saved and closed.

As far as the variables go though, just refrain from using global variables if you plan on making a custom story that isn't a one-map deal kind of like 24-hour Amnesia's maps were, or something similar to my Holiday Series maps.

(This post was last modified: 04-29-2012, 02:21 AM by palistov.)
04-29-2012, 02:20 AM
Find
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#7
RE: Global variables and Local Map variables.

I imagine (but can't confirm, this is all just a hypothesis) that the SetLocalVar string-value pairs are stored in some kind of mapping (probably hashtable) which is constructed as the variables are declared. When a save occurs all the pairs are just dumped to the save file and when a load occurs all are loaded back in. The bindings are probably not cleared until ClearSavedMaps() [testing needed] is called from a different map [they shouldn't be cleared before this as the game wont know what variables will be re-used again]. The consequences to this means implementing arrays or lists by string concatenation with a number will "leak", and as such a different approach should (and can - see my string list implementation) be taken.

SetGlobalVar vars will work in the same way but will always be loaded in any map after creation and probably aren't affected by ClearSavedMaps (Again, testing needed). GetLocalVar / GetGlobal var on a non-existant var will return 0 (or the empty string) but i am uncertain if this also creates an entry for the var, or if entry creation only occurs when writing to a var.

Of course, in scripts the leak potential is not a problem is 99% of cases - you would have to go through a huge number of variables to even make a dent in memory usage (think about the size of one skybox texture for instance - An integer is going to be 4 bytes here - that is quite a lot of integers!). I actually think save file size would become just as much an issue as memory usage. Also that kind of computation is something we aren't rarely going to have to deal with in amnesia, so i wouldn't really worry about it unless you are going to be computing on larger volumes of data than normal.
(This post was last modified: 04-29-2012, 02:39 AM by Apjjm.)
04-29-2012, 02:36 AM
Find
Homicide13 Offline
Senior Member

Posts: 323
Threads: 41
Joined: Nov 2010
Reputation: 14
#8
RE: Global variables and Local Map variables.

By your string list implementation do you mean your functions for parsing strings? (and if not could you provide a link to what you're referencing? D: Unless I just completely missed something haha)

But so in other words if I use a lot of different local map variables in a specific map, it will keep these variables in memory (or will re-load them to memory whenever I load a save) until I use the ClearSavedMaps() function (supposedly).

But would I be able to clear specific variables by setting them to a null value? I know there was something similar to this back when I used to script for Warcraft 3 maps, and certain variables wouldn't necessarily be removed (specifically "location" variables) unless they were manually destroyed and set to null.
(For example, a function might look something like this: Although it was only really location variables that would leak if not set to null, it was considered good form to do this with all of your variables.)
PHP Code: (Select All)
function Trig_ModeAR_Func002A takes nothing returns nothing
    local player p 
GetEnumPlayer()
    
local location l GetUnitLoc(udg_Base[GetConvertedPlayerId(p)])
    
local location sl GetPlayerStartLocationLoc(p)
    
set udg_Hero[GetConvertedPlayerId(GetOwningPlayer(GetLastCreatedUnit()))] = GetLastCreatedUnit()
    
   
//Do stuff

    
call RemoveLocation(l)
    
call RemoveLocation(sl)
    
set l null
    set sl 
null
    set p 
null
endfunction 

04-29-2012, 03:24 AM
Find
Your Computer Offline
SCAN ME!

Posts: 3,456
Threads: 32
Joined: Jul 2011
Reputation: 235
#9
RE: Global variables and Local Map variables.

(04-29-2012, 02:36 AM)Apjjm Wrote: The bindings are probably not cleared until ClearSavedMaps() [testing needed] is called from a different map [they shouldn't be cleared before this as the game wont know what variables will be re-used again].

SetGlobalVar vars will work in the same way but will always be loaded in any map after creation and probably aren't affected by ClearSavedMaps (Again, testing needed).

I can confirm that ClearSavedMaps does not affect global and local map variables (if they have values to them); if it did, it would cause my custom story to function undesirably. Although, i have not necessarily tested if the map variables have null values to them.

Tutorials: From Noob to Pro
(This post was last modified: 04-29-2012, 04:16 AM by Your Computer.)
04-29-2012, 04:12 AM
Website Find




Users browsing this thread: 4 Guest(s)