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 in SOMA
Daemian Offline
Posting Freak

Posts: 1,130
Threads: 42
Joined: Dec 2012
Reputation: 49
#1
Global variables in SOMA

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.

07-10-2016, 11:36 PM
Find Reply
Abion47 Offline
Senior Member

Posts: 355
Threads: 22
Joined: Oct 2015
Reputation: 46
#2
RE: Global variables in SOMA

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.

cScript_SetGlobalVarInt("some_int_var", 1);

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

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".
07-10-2016, 11:49 PM
Find Reply
Daemian Offline
Posting Freak

Posts: 1,130
Threads: 42
Joined: Dec 2012
Reputation: 49
#3
RE: Global variables in SOMA

What if it's a custom class type?

PHP Code: (Select All)
    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.

(This post was last modified: 07-11-2016, 12:16 AM by Daemian.)
07-10-2016, 11:55 PM
Find Reply
Daemian Offline
Posting Freak

Posts: 1,130
Threads: 42
Joined: Dec 2012
Reputation: 49
#4
RE: Global variables in SOMA

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.

07-11-2016, 02:50 AM
Find Reply
Abion47 Offline
Senior Member

Posts: 355
Threads: 22
Joined: Oct 2015
Reputation: 46
#5
RE: Global variables in SOMA

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.
07-11-2016, 03:28 AM
Find Reply
Daemian Offline
Posting Freak

Posts: 1,130
Threads: 42
Joined: Dec 2012
Reputation: 49
#6
RE: Global variables in SOMA

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!.

07-11-2016, 05:24 AM
Find Reply




Users browsing this thread: 1 Guest(s)