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.

Newcomers AppGameKit Corner / Any alternative to global?

Author
Message
Danishly
9
Years of Service
User Offline
Joined: 10th Aug 2015
Location: Norway
Posted: 9th Sep 2015 14:24
Hello, and thank you for all the help so far! I have had a little hiatus, but I'm back

I have a question about functions and globals. If I don't declare a variable global, i get the following error:"Variable is used without being defined or initialized." So at the start of mye program I set every variable as a profile. But I have heard that using globals may cause some problems, so I would like to know if there is another way of define or initialize the variables?

Just started - Much to learn
Crazy Programmer
AGK Developer
20
Years of Service
User Offline
Joined: 6th Sep 2004
Location: Lost in AGK
Posted: 9th Sep 2015 23:45 Edited at: 9th Sep 2015 23:48
Quote: "Variable is used without being defined "

This error could mean 2 things.

1. You are using a variable that has not been defined in your file that needs to be set to Global in your Main.agc file

2. You have not designated a value to your variable.


This alone would cause this error

The right way...


or



You don't have to define every variable a global...Its all a matter of how you have your project laid out.

Beta Test Age of Knights:https://play.google.com/apps/testing/com.CrazyProgrammerProductions.my_AgeOfKnights
Download JellyFish Dive:https://play.google.com/store/apps/details?id=com.CrazyProgrammerProductions.my_JellyFishSwim
mrradd
9
Years of Service
User Offline
Joined: 24th Feb 2015
Location: CA, USA
Posted: 11th Sep 2015 23:38 Edited at: 11th Sep 2015 23:42
Variables are a bit odd in BASIC--I feel. If you define a variable in an .agc outside of a method(aka function), that variable is not going to be available in any method. Correct me if I am wrong, but I believe they are available in subroutines however.

If I have a main.agc like:


I personally have not had any issues with any Globals, since I set them once only and never touch it again. I also like to define ALL of my globals in one .agc file that I usually call Globals.agc. This way I can include that file wherever I need it and be done with it.

Does this help to explain it/help? I wrote this hastily, so I'm not sure....

-mrradd-
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 12th Sep 2015 22:23
I believe the "proper" way to handle this would be take the variable that you have as global and pass it to the function as a parameter instead.

AGK is limited tho in that it can only return 1 variable. In AGK2 you can output a typed variable from a function. (Not sure you can do this in AGK1 or not)

Login to post a reply

Server time is: 2024-11-21 14:51:53
Your offset time is: 2024-11-21 14:51:53