Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: arj0n on Sat 22/10/2022 23:07:24

Title: [solved] GiveScore (<struct-with-array>) gives expr error?
Post by: arj0n on Sat 22/10/2022 23:07:24
Using AGS v3.5.1.21

I have an extra script called Points which is on top of all scripts
In its header it contains first 45 '#define' lines followed by a structs with arrays:

#define R01_use_cap_on_owl 0;
#define...

 
struct Score
{
  String name;
  int points;
};

import Score scores[46];
import function set_score_values();

And in the script Points I have this:


Score scores[46];
export scores;

function set_score_values()
{
  scores[0].name = "R1 use cap on owl";
  scores[0].points = 3;
  ...
}

So far so good, but in room 1 I have this line:

GiveScore(scores[R01_use_cap_on_owl].points);
Which gives me this error when compiling:
Error (line 322): Parse error in expr near 'scores' | room1.asc
I've no clue what's going wrong here...
Title: Re: GiveScore (<struct-with-array>) gives expr error?
Post by: eri0o on Sat 22/10/2022 23:23:18
in your define, you have an extra ; at the end, define macros are mere text replacement, so that character gets pasted inside the array before parsing, giving the error reported.
Title: Re: GiveScore (<struct-with-array>) gives expr error?
Post by: arj0n on Sat 22/10/2022 23:26:32
Ow man, I completely overlooked that ";"
Thanx!
Title: Re: [solved] GiveScore (<struct-with-array>) gives expr error?
Post by: Snarky on Sun 23/10/2022 09:48:50
I thought I'd share a little "trick" I like to use in cases like this, where you want to have named indexes into an array. Instead of #define constants I use an enum (set to start from 0), and with a final element that represents the size:

Code (ags) Select
enum ScoreEvent
{
  R01_use_cap_on_owl = 0,
  R01_some_other_thing,
  // ...
  SCORE_EVENT_COUNT
};

// ...

Score scores[SCORE_EVENT_COUNT];

I find it makes it easier to make changes in the list without having to update a lot of different numbers, since the enum entries (after the first) are auto-numbered and the size is calculated automatically. Of course, it only works if there is an exact one-to-one correspondence between the constants/enum and the array.
Title: Re: [solved] GiveScore (<struct-with-array>) gives expr error?
Post by: arj0n on Sun 23/10/2022 12:45:03
Thank you for that 'trick', snarky!
It definitely is an improvement over the #define constants I used.

One question:
should I also use "SCORE_EVENT_COUNT" instead of the static total "46" for "import Score scores[46];"

I currently have this:
ash:

struct Score
{
  String name;
  int points;
};

enum ScoreEvent
{
    R1_use_cap_on_owl = 0,
    R01_some_other_thing,
    ...
    SCORE_EVENT_COUNT
};

import Score scores[46];
import function set_score_values();

asc:

Score scores[SCORE_EVENT_COUNT];
export scores;

function set_score_values()
{
    scores[0].name = "R1 use cap on owl";
    scores[0].points = 3;
    ...
    ...
}

Title: Re: [solved] GiveScore (<struct-with-array>) gives expr error?
Post by: Snarky on Sun 23/10/2022 13:22:27
Quote from: arj0n on Sun 23/10/2022 12:45:03should I also use "SCORE_EVENT_COUNT" instead of the static total "46" for "import Score scores[46];"

Yes, that will ensure that the two are always consistent (and you won't need to count to work out that the value is in fact 46).

To get the benefits of this approach, I would also suggest changing the assignment to:

Code (ags) Select
function set_score_values()
{
    scores[R01_use_cap_on_owl].name = "R1 use cap on owl";
    scores[R01_use_cap_on_owl].points = 3;
    ...
    ...
}

Otherwise you won't be able to make changes in the list without introducing inconsistencies. The drawback, though, is that the assignment becomes wordier. (It might be worth writing a very simple helper function for the assignment so it can be done in one line instead of two.)

I don't think there is a simple solution that eliminates all tedium from the task.
Title: Re: [solved] GiveScore (<struct-with-array>) gives expr error?
Post by: arj0n on Sun 23/10/2022 15:10:16
Clear, thanx.