Frictional Games Forum (read-only)
Understanding on Global Variables and how to use them. - 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 Articles (https://www.frictionalgames.com/forum/forum-40.html)
+---- Thread: Understanding on Global Variables and how to use them. (/thread-20927.html)

Pages: 1 2


Understanding on Global Variables and how to use them. - PutraenusAlivius - 03-26-2013

What are global variables? They are variables that are global, which means if you do something on one map, something on another one does something else.
EXAMPLE:
In map 1 you fixed a machinery. In map 3, a door will be lifted because the machinery is done.
END EXAMPLE.

Here's how you do it.
(NOTE: THE global.hps MUST BE IN YOUR MAPS/ DIRECTORY!)
1. Make a global.hps file (EXTENSION MUST BE .HPS!)
---
2. Type this on the global.hps
---
PHP Code:
void OnGameStart()
{
SetGlobalVarInt("NAME"0);  //Change NAME to whatever you want the name to be.

Get it? This means that on Game Start (When the player plays the game first time. Not entering map first time)SetGlobalVarInt will set it's variable to 0.
---
3. In the map where the player must do something to do something else in another map, wrote this to the map's .hps file.
This must be in another function!
PHP Code:
AddGlobalVarInt("NAME"1);  //Change NAME to whatever you want the name to be. 
Get it? This means that if the Player does something, it will add the GlobalVarInt to 1.
---
4. In the map where you want something to do if the player has done something from another map, wrote this the the map's .hps file.
---
PHP Code:
void OnEnter()
{
    if(
GetGlobalVarInt("NAME") == 1
       {
        
//Something changed if Player added the GlobalVarInt.
       
}

Get it? No? Fine. This means each time Player enters the map, the if-statement will check whether GlobalVarInt is 1 or not. If it's 1, it will do something. But if it didn't, it would do nothing.
Hope this helps. If there's another thread for this, i'm deeply sorry for not noticing it. It's directed for people who didn't knew Global Variables.
---
For Local Variables, change Global to Local on each function. Like GetGlobalVarInt to GetLocalVarInt. The only thing is that local variables do not use the global.hps. It must be within the map' hps file itself.
EDIT:
You can declare new global variables in the maps .hps file. Although using a global.hps saves room.

This was pointed out by Adrianis.


RE: Understanding on Global Variables and how to use them. - Adrianis - 03-26-2013

Nice tutorial. It's worth noting that you can declare new global variables (that's using SetGlobalVar...etc) from any .hps map script, they don't all have to be in global.hps


RE: Understanding on Global Variables and how to use them. - PutraenusAlivius - 03-26-2013

(03-26-2013, 01:11 PM)Adrianis Wrote: Nice tutorial. It's worth noting that you can declare new global variables (that's using SetGlobalVar...etc) from any .hps map script, they don't all have to be in global.hps

Oh. But i'm using a global.hps so they don't clutter the .hps.


RE: Understanding on Global Variables and how to use them. - Adrianis - 03-26-2013

(03-26-2013, 02:16 PM)JustAnotherPlayer Wrote:
(03-26-2013, 01:11 PM)Adrianis Wrote: Nice tutorial. It's worth noting that you can declare new global variables (that's using SetGlobalVar...etc) from any .hps map script, they don't all have to be in global.hps

Oh. But i'm using a global.hps so they don't clutter the .hps.

Of course - that's a good idea. Just wanted to add a note to the rest of it Smile


RE: Understanding on Global Variables and how to use them. - No Author - 03-27-2013

Does this script works with the "LocalVarInt" scripts ?


RE: Understanding on Global Variables and how to use them. - PutraenusAlivius - 03-27-2013

(03-27-2013, 02:28 PM)No Author Wrote: Does this script works with the "LocalVarInt" scripts ?
LocalVarInt's are for Local Variables. Meaning that if you do something on one map, something else on that map happen.
GlobalVarInt's are for Global Variables. Meaning that if you do something on one map, something else happens on another one.


RE: Understanding on Global Variables and how to use them. - No Author - 03-27-2013

Took me awhile to understand that


RE: Understanding on Global Variables and how to use them. - PutraenusAlivius - 03-27-2013

(03-27-2013, 02:49 PM)No Author Wrote: Took me awhile to understand that

1 min for me.


RE: Understanding on Global Variables and how to use them. - Wooderson - 03-27-2013

Learning about functions and procedures in A Level Computing at the moment (VB).

I am in no way a pro or anything, I am very amateur but this is what I know about them.

They will make your sub main shorter as you call each procedure/function within sub main which is at a different part of the program.

Main benefit is its easier to test for bugs.
Disadvantage is you need to look out for variables and such being local/global and re-used etc.


RE: Understanding on Global Variables and how to use them. - PutraenusAlivius - 03-27-2013

(03-27-2013, 03:14 PM)Wooderson Wrote: Learning about functions and procedures in A Level Computing at the moment (VB).

I am in no way a pro or anything, I am very amateur but this is what I know about them.

They will make your sub main shorter as you call each procedure/function within sub main which is at a different part of the program.

Main benefit is its easier to test for bugs.
Disadvantage is you need to look out for variables and such being local/global and re-used etc.

I think your in the wrong thread here.