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


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

(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?


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

(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 { ... }'

Code:
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


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

Yeah I've never used anything other than VB for programming, just testing what I already know.

The HPL2 looks pretty confusing to me.


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

(03-28-2013, 09:04 AM)Wooderson Wrote: Yeah I've never used anything other than VB for programming, just testing what I already know.

The HPL2 looks pretty confusing to me.
HPL2 doesn't look that confusing to me.


RE: Understanding on Global Variables and how to use them. - The chaser - 03-28-2013

(03-28-2013, 11:41 AM)JustAnotherPlayer Wrote:
(03-28-2013, 09:04 AM)Wooderson Wrote: Yeah I've never used anything other than VB for programming, just testing what I already know.

The HPL2 looks pretty confusing to me.
HPL2 doesn't look that confusing to me.

It doesn't look confusing to me either, but as you may have experience, Wooderson doesn't have that much (or doesn't have).


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

(03-28-2013, 12:11 PM)The chaser Wrote:
(03-28-2013, 11:41 AM)JustAnotherPlayer Wrote:
(03-28-2013, 09:04 AM)Wooderson Wrote: Yeah I've never used anything other than VB for programming, just testing what I already know.

The HPL2 looks pretty confusing to me.
HPL2 doesn't look that confusing to me.

It doesn't look confusing to me either, but as you may have experience, Wooderson doesn't have that much (or doesn't have).
Yeah, maybe Wooderson doesn't have much. I started out having zero experience. Three months later, and here i am!


RE: Understanding on Global Variables and how to use them. - The chaser - 03-28-2013

(03-28-2013, 12:15 PM)JustAnotherPlayer Wrote:
(03-28-2013, 12:11 PM)The chaser Wrote:
(03-28-2013, 11:41 AM)JustAnotherPlayer Wrote:
(03-28-2013, 09:04 AM)Wooderson Wrote: Yeah I've never used anything other than VB for programming, just testing what I already know.

The HPL2 looks pretty confusing to me.
HPL2 doesn't look that confusing to me.

It doesn't look confusing to me either, but as you may have experience, Wooderson doesn't have that much (or doesn't have).
Yeah, maybe Wooderson doesn't have much. I started out having zero experience. Three months later, and here i am!

I started in... July? So, I made my CS and at this time I can assure you that I have learnt a lot. Just have it present: You can never know everything about the editor, there will always be things to learn.