[RESOLVED] Can't AGS scripting have constants

Started by Joacim Andersson, Mon 18/11/2024 17:48:03

Previous topic - Next topic

Joacim Andersson

I'm trying to have a few constants in my code, the reason is that several of my rooms start a timer, and the on_call function gets an argument that can identify what timer has ended. I wanted to use some named constants for these values for easy maintenance of the code.

I see in the documentation that AGS has some built-in #declare constants and macros, but I don't seem to be able to define my own. I noticed that the const keyword exists, but I can still change the value of that constant, so basically the const keyword doesn't seem to do anything,
Code: ags
const int my_value = 1;
my_value = 2; // <- In my opinion this line should cause a compiler error
Display("%d", my_value); // shows 2

[Edit]
Clarification:
I know I can use #declare, but I want them to be globally accessible, and I don't know how to do that.

Crimson Wizard

In versions 3.* the AGS script compiler has a number of syntax features that are not implemented to the full extent.
For the most part this is fixed in the new compiler, available in an upcoming AGS 4 version, found here:
https://www.adventuregamestudio.co.uk/forums/ags-engine-editor-releases/ags-4-0-early-alpha-for-public-test/

As for the AGS 3.* versions, I don't remember about "const", but the usual way to make constants there is to use "#define" preprocessor command:

Code: ags
#define MY_VALUE 1

Joacim Andersson

Quote from: Crimson Wizard on Mon 18/11/2024 18:39:06As for the AGS 3.* versions, I don't remember about "const", but the usual way to make constants there is to use "#define" preprocessor command:

Code: ags
#define MY_VALUE 1

Yes, I'm aware of that (I edited my post at about the same time you replied) but I can't seem to make them globally accessible.

Crimson Wizard

Quote from: Joacim Andersson on Mon 18/11/2024 18:50:04Yes, I'm aware of that (I edited my post at about the same time you replied) but I can't seem to make them globally accessible.

Hmm, please tell, where did you put these and where are you trying to use them?

In AGS the scripts can only see what is declared in:
- the script itself (before the usage, obviously);
- its header;
- any header located above them in the list of scripts.

Joacim Andersson

#4
I put the #define statement in the header of one of my scripts and tried to access it from a Room file, but got a compile time error in the Room file.

I have this (sort of) in the header of my code file:
Code: ags
import function MyFunc() // <- MyFunc exist in the associated code file for this header
#define MY_VALUE 1
Next in a room file I have this:
Code: ags
function room_AfterFadeIn()
{
  MyFunc(); // <- This will execute
  Display("%d", MY_VALUE); // <- Compile time error "undefined symbol"
}
As you can see MyFunc is imported but I can't reach MY_VALUE in the Room.

Crimson Wizard

#5
Quote from: Joacim Andersson on Mon 18/11/2024 19:01:26I put the #define statement in the header of one of my scripts and tried to access it from a Room file, but got a compile time error in the Room file.

That should normally work, could you post actual script (relevant parts), and copy error message here?

Joacim Andersson

Quote from: Crimson Wizard on Mon 18/11/2024 19:07:41That should normally work, could you post actual script (relevant parts), and copy error message here?
Funny, again I edited my previous post at the same time you replied.

Crimson Wizard

#7
Above code is supposed to work. (there's a missing ";" after function declaration, but I assume it's a typo?)

It works if I put it into a dummy game project.

Could be the code you posted is not exactly the code you have in game, and that difference matters?

Joacim Andersson

I noticed that I had MY_VALUE defined in 2 different places, however that doesn't explain why I got a 'Undefined Symbol' error instead of a 'MY_VALUE is already defined' error. Anyhow I got it working, thanks a lot for your help.

Crimson Wizard

Quote from: Joacim Andersson on Mon 18/11/2024 19:34:21I noticed that I had MY_VALUE defined in 2 different places, however that doesn't explain why I got a 'Undefined Symbol' error instead of a 'MY_VALUE is already defined' error.

I would only be able to answer this question if you'd post an actual code, and a full error message.

For example, if I put two "#define MY_VALUE" in two different script headers, I am getting an error:
QuoteMacro 'MY_VALUE' is already defined

Joacim Andersson

It doesn't matter, it works now so I've marked this thread as RESOLVED. Thank you again for your help.

I can't post the whole code, it's a big part of my next game,

Crimson Wizard

#11
Quote from: Joacim Andersson on Mon 18/11/2024 19:49:49I can't post the whole code, it's a big part of my next game,

Of course; I did not mean the whole script, just the relevant parts with constant declarations and their use.
But if it's resolved, then it's fine.

Khris

This is the second time you've
a) posted code and an error message that do not go together
b) posted code that should absolutely work but mysteriously fails, then suddenly and mysteriously does work

I'm glad you're able to get your issues resolved, but please keep in mind that these forum threads are also supposed to benefit future readers.

Joacim Andersson

#13
Quote from: Khris on Mon 18/11/2024 22:35:08This is the second time you've
a) posted code and an error message that do not go together
b) posted code that should absolutely work but mysteriously fails, then suddenly and mysteriously does work
What do you mean the second time? I'm only aware of this post about an error message. Besides I can't help that AGS throws error messages that don't make sense.

In the other thread the error message did make sense, and I got the help I needed.

However, I agree with you that forum posts should be a help for others as well as for the OP.

Khris

#14
In the other thread you wrote
Quotebut I get an error with the undefined symbol 'KnownNPC'.
and posted code that throws a different error (because you had 'char' as variable name).

My point is: post the exact code you have in your actual game, and chill. You're just starting out with AGS, but it sounds like you aren't new to coding. You should know that every single character matters. Yet your posts feel rushed, and you go back and edit posts, then try a few more things, then post another five replies. It just seems like a lot of wasted time and energy for everybody involved.

Edit:
Quote from: Joacim Andersson on Mon 18/11/2024 23:19:34I didn't use char in my actual code; I typed it wrong in the simplified example I posted here in the forum.
I'm well aware, that's why I told you to not do that. Fixing the wrong error is a waste of time.

Joacim Andersson

I didn't use char in my actual code; I typed it wrong in the simplified example I posted here in the forum. I got that error because of the order in which the headers are added during compile time. And no, I'm not new to coding. I've been a system developer for 30 years. I also started with AGS 20 years ago; however, it's also been almost 20 years since I last used it, with an exception for about a year ago.

I got your point though.

SMF spam blocked by CleanTalk