Posted: 10th Oct 2021 6:22
I am getting this error message:

ain.agc:15: error: Variable "screencenterx" is used without being defined or initialise
d

Here is the code i am using:

constants.agc
+ Code Snippet
// 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


init.agc
+ Code Snippet
function initSystem()
	// show all errors
	SetErrorMode(2)

	// set window properties
	SetWindowTitle( "Drum Machine Hero" )

	// set the window size
	SetWindowSize( 1024, 768, 0 )

	// allow the user to resize the window
	SetWindowAllowResize( 1 )

	// set display properties
	SetVirtualResolution( 1024, 768 ) // doesn't have to match the window

	// allow both portrait and landscape on mobile devices
	SetOrientationAllowed( 1, 1, 1, 1 )

	// 30fps instead of 60 to save battery
	SetSyncRate( 30, 0 )

	// use the maximum available screen space, no black borders
	SetScissor( 0,0,0,0 )

	// since version 2.0.22 we can use nicer default fonts
	UseNewDefaultFonts( 1 )
endfunction


render.agc
+ Code Snippet
function drawDrums(x, y, drumsize, color)
	DrawEllipse(512, 184, 40, 40, MakeColor(255, 255, 255), MakeColor(0, 0, 0), 1)
	DrawEllipse(592, 184, 40, 40, MakeColor(255, 255, 255), MakeColor(0, 0, 0), 1)
endfunction


main.agc
+ Code Snippet
// Project:  Drum Machine Hero
// Created: 2021-10-10

#include "constants.agc"
#include "init.agc"
#include "render.agc"

initSystem()

do
    

    Print( ScreenFPS() )
    drawDrums(DRUMSX, 0, 0, 0)
    Sync()
loop


As you can see I am not even using the constants value at all in the function itself.
Posted: 10th Oct 2021 9:06
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.

+ Code Snippet
// screen constants
#constant SCREENWIDTH 1024
#constant SCREENHEIGHT 768
#constant SCREENCENTERX 512
#constant SCREENCENTERY 384

// drum constants
#constant DRUMSX 512
#constant DRUMSY 100
#constant DRUMRADIUS 80
#constant DRUMSPACING 160
Posted: 10th Oct 2021 11:45
set screencenterx as global

global screencenterx
Posted: 10th Oct 2021 14:02
This was discussed here also.
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:

+ Code Snippet
global COLOR_WHITE = MakeColor(255, 255, 255)
Posted: 11th Oct 2021 1: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.
Posted: 12th Oct 2021 3:16
#constant is a non-recursive text replacement.

+ Code Snippet
#constant COLOR_WHITE  MakeColor(255, 255, 255)

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.
+ Code Snippet
global COLOR_WHITE 
COLOR_WHITE = MakeColor(255, 255, 255)

I opened an issue for this at github. Feel free to vote.
Posted: 14th Oct 2021 7:18
ust 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
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
Posted: 14th Oct 2021 15:38
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.
Posted: 15th Oct 2021 10:01
+ Code Snippet
if SCREENWIDTH = &lt;&gt;1024 then SCREENWIDTH = 1024

What does this code mean? Sorry I?m new to AGK.
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.
Posted: 15th Oct 2021 13:47
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)
Posted: 15th Oct 2021 14:05
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.
Posted: 15th Oct 2021 14:28
I just do what the compiler says to do
Posted: 15th Oct 2021 14:59
I just do what the compiler says to do


A slave to your computer.