Suggestion for AGS (Pointers to non-managed types) (Solved)

Started by Creator, Thu 22/09/2011 10:39:45

Previous topic - Next topic

Creator

I'm trying to create a battle system in my game and, without the ability to declare a pointer to my structure, it's making me have to created a seperate health down function for each character that mine attacks.
Code: ags

void peeps::HitTyler()
{
  if (didHit == true)
  {
    if (double == true)
    {
      Tyler.hp -= this.damage*2;
    }
    else
    {
      Tyler.hp -= this.damage;
    }
  }
}
//And so on for every other character.


Called by:
Code: ags

Blain.HitTyler();


It could be easier if, I could, in my attacking script, have the damage scripts available there.
Code: ags

void peeps::Attack(Character* person, Character* enemy, peeps* attacker, peeps* victim)
{
  //Play attacking animations and sound and show some text.
  victim.hp -= attacker.damage;
}


I've not studied C# much, nor do I know much about programming in general. I've only studied a year of C++ and that was last year. I've done very little programming since then. Is there any reason why AGS doesn't support pointers to custom structures and is there an easier way I can do my battle system (provided I gave you enough code to work with)?

PS. My attacking script is the exact same as I've shown, minus the peeps pointers. It just plays the animation and shows some text. The attacking script is called by
Code: ags

Blain.Attack(cBlain, cTyler);
Tyler.Attack(cTyler, cBlain);


I need the two character pointers in order the get both characters' names, so it shows "Blain attacked Tyler with fireball" and then "Tyler attacked Blain with freeze" and so on.

EDIT - Oh, and before I forget, obviously there's an error if I try to compile with pointers to my structure. It says: Cannot declare pointer to non-managed type.

Snarky

When you create a (non-static) method as part of the struct, you automatically get a pointer to the current instance of the struct called "this". So you don't need the "victim" pointer. As for the "attacker" pointer, if you're really just using it to look up attacker.damage, why don't you just do that whenever you're calling the function and simply pass it the damage?

So you'd get:

Code: ags

void peeps::Attacked(Character* person, Character* enemy, int damage) // "Attacked" because it's run on the peep being attacked
{
  //Play attacking animations and sound and show some text.
  this.hp -= damage;
}


And call it like:

Code: ags

Blain.Attacked(cBlain, cTyler, Tyler.damage);


I'm not convinced this is really the best way to organize the code (I wouldn't put the animation/display code inside the peeps struct, just the logic for handling the damage), but it's the working solution that is closest to what you have currently.

Khris

What I've done is limited myself to a maximum of three party members versus five enemies (this isn't necessary though, you can of course have more).

When a battle starts, the stats of all involved characters are copied over into a special struct array; the party members' stats get copied to f[0], f[1] and f[2] and the enemies' to f[3] - f[7]. f[] now holds all character data and in addition frame and coordinate info etc. I'm going to need to visually depict the battle.

During the battle, I simply call e.g. PartyAttacks(int who, int whom). This handles all the maths and animation, using the values storied in f[who] and f[whom].
f has a Character* member which I can use to refer back to the AGS character.

Creator

Snarky's method is probably the easiest to do going from what I already have, but I'll try your method first, Khris. Thanks for your help, guys.

I still would like to know why AGS doesn't allow pointers to non-managed types if there's any reason.

discordance

I believe it's because there's currently no way to do garbage collection on non-managed types.

Creator

Oh, right. Fair enough. Thanks for the reply. Pity that. Could make it a whole lot easier. Anyway, I now have the system down to about 4 different functions instead of the 10 I had.

Calin Leafshade

Yea, the best way to deal with problems like this in AGS is as Khris suggested.

Generally speaking you have a global array of structs and then pass the index in the struct's stead. It's not as neat as passing a struct but the effect is largely the same.

Alternatively you make a c++ plugin with the types you use, thus making them managed. I made a plugin like this with common structures like Point, Rectangle, StringList and so on.

Monsieur OUXX

Alternative solution: If you're not stisifed with using indexes, and if you don't want to make your own plugin to address the issue (as Calin suggested), then use the Luags plugin (you can find it in Google). Ready-to-use top notch scripting.
 

Creator

Quote from: Monsieur OUXX on Fri 23/09/2011 10:20:00
if you don't want to make your own plugin to address the issue (as Calin suggested), then use the Luags plugin (you can find it in Google). Ready-to-use top notch scripting.

It would make my own module in C++ if I knew how. My programming knowledge is quite basic when it comes to C++. I only did a year and I still struggle with dynamic memory (which was considered easy by my teacher).

The Luags plugin sounds interesting, but I couldn't find it on Google or on these forums (search function is back :D). Could you provide a link?

David Ostman


monkey0506

There's also no reason why you couldn't use serialization techniques. Slower, yes, but it shouldn't be noticeable unless you're general_knox.

SMF spam blocked by CleanTalk