Frictional Games Forum (read-only)
Help with Global Variables - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: Help with Global Variables (/thread-11597.html)



Help with Global Variables - flamez3 - 12-01-2011

Hey, This is the first time I've ever used Global Variables, and I'm having some troubles with Global variables. This is what I've added to both script files I want the global variable to apply on. This is not all of the script, only the parts that include the global variables.

Map 1:


Quote:void OnStart()
{
SetGlobalVarInt("Globalvar1", 0);
}

void func_on()
{
if(GetGlobalVarInt("Globalvar1") == 1)
{
SetSwingDoorLocked("elevator_door_1", false, false);
}
}
Map 2:


Quote:void OnStart()
{
SetGlobalVarInt("Globalvar1", 0);
SetEntityConnectionStateChangeCallback("valve_iron_1", "valve_func1");

}


void valve_func1(string &in asEntity, int alState)
{
AddLocalVarInt("Var1", 1);
{
AddGlobalVarInt("Globalvar1", 1);
func_on();
}
}

void func_on()
{
if(GetGlobalVarInt("Globalvar1") == 1)
{
SetSwingDoorLocked("elevator_door_1", false, false);
}
}
Again, these are not the full .hps files because I don't want to ruin anything (and there's alot of lines). There are NO fatal errors it works perfectly, but once I do what I'm required by the script, it loads the other map and the elevator door is still locked. Any information about this would be very helpful. Thanks Smile


RE: Help with Global Variables - Obliviator27 - 12-01-2011

You need a global.hps file in the main custom story directory (The one with your extra_english and custom_story_settings.cfg file)
And it should look like


Code:
void OnGameStart()
{
Use SetGlobalVarInt as many times as you like, for as many global variables you like
}
And that's all there is to it. Smile



RE: Help with Global Variables - flamez3 - 12-01-2011

(12-01-2011, 09:05 AM)Obliviator27 Wrote: You need a global.hps file in the main custom story directory (The one with your extra_english and custom_story_settings.cfg file)
And it should look like


Code:
void OnGameStart()
{
Use SetGlobalVarInt as many times as you like, for as many global variables you like
}
And that's all there is to it. Smile
Does that mean that I put


Quote:void func_on()
{
if(GetGlobalVarInt("Globalvar1") == 1)
{
SetSwingDoorLocked("elevator_door_1", false, false);
}
}
under the void OnGameStart place? Once I've added the SetGlobalVarInt("GlobalVar1", 0); inside the void OnGameStart




RE: Help with Global Variables - Obliviator27 - 12-01-2011

It works like this.
Code:
void OnGameStart()
{
SetGlobalVarInt("Globalvar1", 0);
}
and then in your .hps
Code:
void func01()
{
if(GetGlobalVarInt("Globalvar1") == 1)
{
SetSwingDoorLocked("door_1", false, true);
}
}



RE: Help with Global Variables - Your Computer - 12-01-2011

You don't need a global.hps for global map variables to work. The issue here is that the code logic is wrong. This is the kind of scenario where OnEnter() comes in to play. You appear to have the same func_on function in both maps when it isn't required in any. The goal you're showing me is simple: you want a certain door to be unlocked when the user interacts with a valve in another map.

You've done right by trying to set a global map variable within the connection state chage callback, but that is pretty much all that was needed for the connection state change callback. Therefore the valve_func1 function should look something like this:
PHP Code:
void valve_func1(string &in asEntityint alState)
{
    
// You may want to consider alState somewhere in here
    
AddLocalVarInt("Var1"1); // not sure what this is for, but i'll leave it
    // We SET a global map variable here, because ADDing to the variable may cause problems
    
SetGlobalVarInt("Globalvar1"1);
    
// You may want to consider naming your global variables to something more practical


Next you're going to want to remove the func01 function from your scripts, as they are not needed. In your OnEnter function you'll be checking if the global map variable has been set, and if it is set, unlock the door. Therefore make sure your OnEnter function looks something like this:
PHP Code:
void OnEnter()
{
    
// You may want to consider naming your global variables to something more practical
    
if(GetGlobalVarInt("Globalvar1") == 1)
    {
        
SetSwingDoorLocked("elevator_door_1"falsefalse);
    }


Since you didn't clearly specify which map contains the elevator door, i'm going to leave you to put this for the map that contains the elevator door.

One final thing i need to mention: All map variables by default equate to 0, at least the ones that deal with numbers (strings would be an empty string). Therefore there is no need to set a global or local map variable to 0 in OnStart (or anywhere else), that is, unless they are not currently at 0 and you want to reset the value.