[solved] Not sure if it's a bug (struct instance on .ash)

Started by eri0o, Sat 15/06/2019 01:47:06

Previous topic - Next topic

eri0o

I think I found a bug

myfile.ash
Code: ags
enum EnumState {
  eES_False = 0, 
  eES_True = 1,   
};

struct Foo{
  protected EnumState _state;
  import attribute EnumState public_state;
};

Foo bar;



myfile.asc
Code: ags
FindoutStateEnum get_public_state(this Foo*){
  return this._state;
}
void set_public_state(this Foo*, EnumState value){
  this._state = override;
}


Trying to read bar.public_state get's me zero - elsewhere or in the same file (using bar.get_state()).

I guess I shouldn't declare the instance bar of Foo in the .ash, but I wanted something akin to a global singleton.

My solution was to change myfile.asc to
Code: ags

int local_state;

FindoutStateEnum get_public_state(this Foo*){
  return local_state;
}
void set_public_state(this Foo*, EnumState value){
  local_state = override;
}


---

Edit: CW solved below.

Crimson Wizard

#1
myfile.ash
Code: ags

Foo bar;


This is not a global instance, this is multiple instances, one per every script that includes a header.

You did not tell how did you set and get values. But e.g. if you use setter in one script and getter in another script, then you will work with two separate objects of Foo.
This is why you should not put variables in header like that, only use "import".


EDIT:
If you want to make a "singleton" in AGS:
* have only static functions and static attributes in a struct.
* have actual data hidden inside a module.

Only for the sake of example, I usually make second struct inside module called FooData and store actual data there.

SMF spam blocked by CleanTalk