[solved] GiveScore (<struct-with-array>) gives expr error?

Started by arj0n, Sat 22/10/2022 23:07:24

Previous topic - Next topic

arj0n

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:

Code: ags
#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:


Code: ags
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:

Code: ags
GiveScore(scores[R01_use_cap_on_owl].points);

Which gives me this error when compiling:
Code: ags
Error (line 322): Parse error in expr near 'scores' | room1.asc

I've no clue what's going wrong here...

eri0o

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.

arj0n

Ow man, I completely overlooked that ";"
Thanx!

Snarky

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
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.

arj0n

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:

Code: ags
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:

Code: ags
Score scores[SCORE_EVENT_COUNT];
export scores;

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


Snarky

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
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.

arj0n


SMF spam blocked by CleanTalk