How do I go about making an instance of a managed struct global?

Started by NearExtinctionEvent, Today at 05:14:09

Previous topic - Next topic

NearExtinctionEvent

I was attempting to use structs for an RPG I started building, and following the manual. The managed struct itself is defined in the header, and has the factory module from the examples in the manual. I used the factory module to create an instance, but I've had trouble getting it out of GlobalScript and into rooms.

When I have an import call in the header, I get the error 'Local variable cannot have the same name as an import'. When I attempt to run the factory in the header or room where I need the instance, I get the error 'cannot assign initial value to global pointer'.

How would I go about making this work?
Sometimes you do something stupid. The important thing is to not let that prevent you from being smart too.

Snarky

The problem is probably something simple, but it's difficult to tell without seeing the exact code you have. Please post the relevant lines.

NearExtinctionEvent

Here's my code.

(the part in the header)
Code: ags
//make struct
managed struct EnemyStats {
    int hp;
    int atk;
    int def;
    int xpyield;
    int nameid;
    
    import static EnemyStats* Create(int hp, int atk, int def, int xpyield, int nameid);
  };

(the part in the body)
Code: ags
// i dont even know anymore
static EnemyStats::Create(int hp, int atk, int def, int xpyield, int nameid);
  {
    EnemyStats* e = new EnemyStats;
    e.Init(hp, atk, def, xpyield, nameid);
    return e;
  }
Code: ags
 // the actual stats builder
  
  EnemyStats* HumanWeak = HumanWeak.Create(30, 1, 4, 10, 1);

'HumanWeak' is the instance I'm trying to get globally, but I can't figure it out (as previously stated). Most of this is readapted from the code examples in the manual.
Sometimes you do something stupid. The important thing is to not let that prevent you from being smart too.

Snarky

The last bit should be:

Code: ags
 // the actual stats builder
  
  EnemyStats* HumanWeak = EnemyStats.Create(30, 1, 4, 10, 1);

As .Create() is a static method, you have to call it with the class name, not the instance name. (And anyway, you can't call it with HumanWeak before that variable is assigned.)

Khris

Also note that
Quote from: error messagecannot assign initial value to global pointer
is caused by putting that line outside a function.

You need to declare it outside, then assign the initial value inside a function.

Code: ags
EnemyStats* HumanWeak; export HumanWeak;

void game_start() {
  HumanWeak = EnemyStats.Create(30, 1, 4, 10, 1);
}

(I also don't see a EnemyStats::Init function, but I guess you omitted it when posting your code?)

Snarky

And finally, add the line

Code: ags
import EnemyStats* HumanWeak;

to the GlobalScript header in order to access HumanWeak from all room scripts. (It is also possible to add it to each room where you want to access it, but to make it "global" the easiest thing is to put the import in the header.)

SMF spam blocked by CleanTalk