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


Poll: Was this helpful to you?
You do not have permission to vote in this poll.
Yes
33.33%
1 33.33%
No
66.67%
2 66.67%
Total 3 vote(s) 100%
* You voted for this item. [Show Results]

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding on Global Variables and how to use them.
Adrianis Offline
Senior Member

Posts: 620
Threads: 6
Joined: Feb 2012
Reputation: 27
#12
RE: Understanding on Global Variables and how to use them.

(03-27-2013, 03:17 PM)Wooderson Wrote:
(03-27-2013, 03:16 PM)JustAnotherPlayer Wrote:
(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.

Wouldn't surprise me at all since I am shit at computing and such. Global/Local variables are to do with functions/procedures though right?


Your right in some ways, but you gotta be careful with the local/global variables you learn about in programming languages and what they mean in Amnesia scripting. I tripped up on it at one point and got YC to explain it.

The problem is that global/local in programming is referring to the scope, which is why it's relevant to functions and procedures. In a C style language, a change of scope is always denoted by using {}, that counts for functions as well as if/while/do/for statements & loops. VB is slightly different in that it uses 'if...end if' rather than 'if { ... }'

int inThisIsAGlobalVariable; // because it is outside of the scope of a function

void OnStart() {
     int inThisIsALocalVariable; // because it is only relevant to the scope of OnStart, cannot be used outside
}

void function() {
     inThisIsAGlobalVariable = 2; // this variable can be used, because it has been declared in the global scope (outside any functions/statements)
     inThisIsALocalVariable = 2; // this will cause an error, because you are no longer inside the scope of OnStart
}

The way I was taught in C, was to never ever use global variables - you never need to use them because other options are available, and they cause unnecessary difficulty debugging if you're unexpectedly setting the value somewhere you can't remember. I use them in scripts though, because they are incredibly useful Tongue Just make sure you name them in a unique way, so you'll never accidentally use it.

Neither of the variables in the code above will be saved by the game if the player saves & exits, they all get wiped out. All except for the variables declared with the functions SetLocalVar... and SetGlobalVar..., those variables are correctly saved by HPL2
In terms of those functions, Local is referring to the whole .hps file you are using (regardless of the scope when declared), and Global means available in all the script files.

Hope that helps clear things up, if you want any further explanation please ask Smile

(This post was last modified: 03-27-2013, 11:19 PM by Adrianis.)
03-27-2013, 11:14 PM
Find


Messages In This Thread
RE: Understanding on Global Variables and how to use them. - by Adrianis - 03-27-2013, 11:14 PM



Users browsing this thread: 1 Guest(s)