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 / Error variable foo is used without being defined

Author
Message
Olde Worlde Illusions
2
Years of Service
User Offline
Joined: 10th Oct 2021
Location:
Posted: 10th Oct 2021 06:22
I am getting this error message:

Quote: "Running App Game Kit 2\Tier 1\Compiler\AGKCompiler.exe (in directory: E:\AGK Projects)
Compilation failed.
main.agc:15: error: Variable "screencenterx" is used without being defined or initialise"
d

Here is the code i am using:

constants.agc


init.agc


render.agc


main.agc


As you can see I am not even using the constants value at all in the function itself.
tboy
11
Years of Service
User Offline
Joined: 1st Jan 2013
Location: UK
Posted: 10th Oct 2021 09:06 Edited at: 10th Oct 2021 09:28
Constants usually use literal values, so if you know a value won't change, use a constant but if it changes use a variable.

In your example, just use literal values.

Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 10th Oct 2021 11:45
set screencenterx as global

global screencenterx
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 10th Oct 2021 14:02
This was discussed here also.

Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Olde Worlde Illusions
2
Years of Service
User Offline
Joined: 10th Oct 2021
Location:
Posted: 10th Oct 2021 18:40
So there is no way to assign the value of an expression to a constant or function? Doesn't that defeat the purpose of using constants? Also this doesn't seem to work either:

Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 11th Oct 2021 01:03


Constants should not be used unless something is never changing.

Setting a location of a sprite to never change is a odd thing to do.

May I ask why you are setting your display never to change?

just wondering, everyone's programs are different.



adambiser
AGK Developer
8
Years of Service
User Offline
Joined: 16th Sep 2015
Location: US
Posted: 12th Oct 2021 03:16 Edited at: 12th Oct 2021 03:19
#constant is a non-recursive text replacement.


is valid.

If you want to assign global values in #included files, you should use #insert instead, which runs the code where the #insert takes place in main.agc. Otherwise the assignment is never called because #include puts the file's code at the end of the code and isn't reached until your game closes.

Can't declare and assign a variable at the same time.

I opened an issue for this at github. Feel free to vote.
Olde Worlde Illusions
2
Years of Service
User Offline
Joined: 10th Oct 2021
Location:
Posted: 14th Oct 2021 07:18
Quote: "Constants should not be used unless something is never changing.

Setting a location of a sprite to never change is a odd thing to do.

May I ask why you are setting your display never to change?

just wondering, everyone's programs are different."


Because it’s a UI element and not a sprite per se. The constant is simply adjusting the size and location of it based on the screen size.

// screen constants
#constant SCREENWIDTH = 1024
#constant SCREENHEIGHT = 768
#constant SCREENCENTERX = SCREENWIDTH / 2
#constant SCREENCENTERY = SCREENHEIGHT / 2

// drum constants
#constant DRUMSX=SCREENCENTERX
#constant DRUMSY = 100
#constant DRUMRADIUS = 80
#constant DRUMSPACING = DRUMRADIUS * 2
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 14th Oct 2021 13:38
UI elements can stay constant always with out changing there position in the program.

With that said You can say

if SCREENWIDTH = <>1024 then SCREENWIDTH = 1024

So on and forth
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 14th Oct 2021 15:38
Quote: "UI elements can stay constant always with out changing there position in the program."


For most cases yes but a dynamic UI would need variable location data, if you give a window size of 1024 and allow the user to resize or use both landscape and portrait on mobile (for example) then you want to react to the size change and move your UI elements, resize backgrounds etc etc, In which case you want global variables not constants ...

We established (in Phaelax's thread) that the assignment rules are a little quirky for both constants and globals, although constants have this ability to (sometimes) break the rules its best to keep to the namesake and use only for static numbers and literal strings, if your data is the result of a calculation or function return it should be in a variable.
Open Source plugins
Cl - DnD Plugin
Buy Me A Coffee
Olde Worlde Illusions
2
Years of Service
User Offline
Joined: 10th Oct 2021
Location:
Posted: 15th Oct 2021 10:01

What does this code mean? Sorry I’m new to AGK.
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 15th Oct 2021 13:35
if SCREENWIDTH = <>1024 then SCREENWIDTH = 1024

If your screen width is over or under 1024 or out of place, it will reset it to 1024.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 15th Oct 2021 13:47 Edited at: 15th Oct 2021 13:48
Quote: "if SCREENWIDTH = <>1024 then SCREENWIDTH = 1024"

that line of code isn't valid.

it should be something like:

if SCREENWIDTH <>1024 then SCREENWIDTH = 1024

(which, i don't see a point in but at least the code will run)
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 15th Oct 2021 14:05
Quote: "that line of code isn't valid."


That's true

it cant = it lol.

It was just a example

Listen to .Virtual Nomad As Virtual Nomad is very smart.
Virtual Nomad
Moderator
18
Years of Service
User Offline
Joined: 14th Dec 2005
Location: SF Bay Area, USA
Posted: 15th Oct 2021 14:28
I just do what the compiler says to do
Game_Code_here
3
Years of Service
User Offline
Joined: 2nd Jun 2020
Location:
Posted: 15th Oct 2021 14:59
Quote: "I just do what the compiler says to do"


A slave to your computer.

Login to post a reply

Server time is: 2024-04-19 18:31:13
Your offset time is: 2024-04-19 18:31:13