Frictional Games Forum (read-only)
Global variables in SOMA - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: SOMA (https://www.frictionalgames.com/forum/forum-55.html)
+--- Forum: User created content (https://www.frictionalgames.com/forum/forum-79.html)
+---- Forum: Development (https://www.frictionalgames.com/forum/forum-82.html)
+---- Thread: Global variables in SOMA (/thread-50855.html)



Global variables in SOMA - Daemian - 07-10-2016

Hello ppl, I'm having trouble making global variables to share values across levels.
As soon as I change the level all the variables from every imported script get reset.
Can we declare variables as static or something?
Thx.


RE: Global variables in SOMA - Abion47 - 07-10-2016

AngelScript doesn't really have anything for static variables. What you can do though is to use the global variable system built into HPL3.

Getter functions:

  • cScript_GetGlobalVarString
  • cScript_GetGlobalVarBool
  • cScript_GetGlobalVarInt
  • cScript_GetGlobalVarID
  • cScript_GetGlobalVarFloat
  • cScript_GetGlobalVarVector2f
  • cScript_GetGlobalVarVector3f
  • cScript_GetGlobalVarVector4f
  • cScript_GetGlobalVarMatrix
  • cScript_GetGlobalVarColor


Setter functions:

  • cScript_SetGlobalVarString
  • cScript_SetGlobalVarBool
  • cScript_SetGlobalVarInt
  • cScript_SetGlobalVarID
  • cScript_SetGlobalVarFloat
  • cScript_SetGlobalVarVector2f
  • cScript_SetGlobalVarVector3f
  • cScript_SetGlobalVarVector4f
  • cScript_SetGlobalVarMatrix
  • cScript_SetGlobalVarColor


Usage is pretty simple. To set a variable, call one of the setter functions that matches the type of the value you wish to set.

Code:
cScript_SetGlobalVarInt("some_int_var", 1);

To get the value back, simply call the matching getter function.

Code:
cScript_GetGlobalVarInt("some_int_var");

To keep your code organized, and to prevent potential conflicts from other scripts setting their own global variables, make sure that you prefix your variable names so that they are unique to your mod or map. For example, name your variable something like "MyMod_SomeName".


RE: Global variables in SOMA - Daemian - 07-10-2016

What if it's a custom class type?

PHP Code:
    array<cStuffaMyStuff;
    
  class 
cStuff
 
{
    
int ID;
    
tString Name;
    
tString Description;
    
float Value;
 } 

aMyStuff is the var I want to share across levels.

I tried those cScript_SetGlobalVar* methods but they don't seem to support arrays or custom types.


RE: Global variables in SOMA - Daemian - 07-11-2016

I guess I could come up with a system that stores primitives and recreates the custom array type I need based on the values I store.


RE: Global variables in SOMA - Abion47 - 07-11-2016

Types that aren't one of the above types would be reference types, which would be awkward to handle as a global variable. The thing about reference types is that their state doesn't get saved when the map saves, and when a script loads that contains a reference type variable at the class level or higher without the "[nosave]" tag, it can cause the game to silently crash.

You can create a module that will act as an intermediary to store the values that you want (and get global variable behavior that way), but it wouldn't solve the saving/loading issue. You would have to save the fields within the object as primitives and regenerate the object when the map starts up again, which defeats the purpose of having objects in the first place.


RE: Global variables in SOMA - Daemian - 07-11-2016

I did the little system and it's working for now, I'll make more tests tomorrow.
I didn't want to do it like this but it seems there is no other way. Thx!.