Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: NearExtinctionEvent on Sat 19/07/2025 05:14:09

Title: How do I go about making an instance of a managed struct global?
Post by: NearExtinctionEvent on Sat 19/07/2025 05:14:09
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?
Title: Re: How do I go about making an instance of a managed struct global?
Post by: Snarky on Sat 19/07/2025 05:44:36
The problem is probably something simple, but it's difficult to tell without seeing the exact code you have. Please post the relevant lines.
Title: Re: How do I go about making an instance of a managed struct global?
Post by: NearExtinctionEvent on Sat 19/07/2025 07:47:16
Here's my code.

(the part in the header)
//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)
// 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;
  }
// 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.
Title: Re: How do I go about making an instance of a managed struct global?
Post by: Snarky on Sat 19/07/2025 08:09:13
The last bit should be:

// 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.)
Title: Re: How do I go about making an instance of a managed struct global?
Post by: Khris on Sat 19/07/2025 11:07:14
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.

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?)
Title: Re: How do I go about making an instance of a managed struct global?
Post by: Snarky on Sat 19/07/2025 11:28:34
And finally, add the line

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