Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

AppGameKit Classic Chat / Question about functions and variables

Author
Message
Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 28th Jan 2012 12:59
When I create a variable in main.agc my understanding is that it
should be global and if I refer to via a GoSub in another file I have no issues.
When I refer to it in function even in main I get an error unless it's an object variable.

Example:
MyTxt = createtext("")
I get an error when referencing MyTxt in a function. The value is always 0

Type T_Obj
txt
EndType
MyTxt as T_Obj
MyTxt.txt = createtext("")
No error when referencing MyText.txt

My question is. Is this a bug or can I only reference objects in functions that are not passed in via the function parameters or am I missing something.

Thanks
Talairina
18
Years of Service
User Offline
Joined: 9th Jan 2006
Location: United Kingdom
Posted: 28th Jan 2012 14:33 Edited at: 28th Jan 2012 14:37
Variables used within a function are unique to that function.
For example;


This variable will only be usable within that function. To make a variable usable within and utilize the same variable and its value outside of a function you need to declare it a global variable like following example.



Now you can use it where ever you want without fear of losing its value that includes gosubs, functions, loops etc.

Here's what the AppGameKit documentation say's on functions and variables within them.



More information on Functions at the AppGameKit Documentation page.
Canadian Mark
12
Years of Service
User Offline
Joined: 28th Jan 2012
Location:
Posted: 28th Jan 2012 18:49
I see and thank you. I was under the impression that variables
declared in main were globals and that you only had to to use the global keyword in other files.

Your post cleared this up for me.

Login to post a reply

Server time is: 2024-11-23 02:18:22
Your offset time is: 2024-11-23 02:18:22