Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Pax Animo on Tue 05/07/2022 17:15:54

Title: Accessing a struct via a function parameter (Solved)
Post by: Pax Animo on Tue 05/07/2022 17:15:54
Greeting,

I have this personal function:

Code (ags) Select
function melee_combat()  //i'd like a parameter here which accesses a struct (something like (function melee_combat(dmg_type)
{
  if (player.IsCollidingWithChar(cEnemy)) {
    citizens[2].hp -= citizens[0].melee_dmg;  //this is where i'd like to pass the accessed data too, something like (citizens[2].hp -= dmg_type;
    cEnemy.SayBackground(String.Format("-%d", citizens[0].melee_dmg)); //and here too 
  }
}


It's called from within the on_key_press function.

Code (ags) Select
if (keycode == eKey1 && player.Animating == false) {
    melee_combat();  //this is where i want to choose which data of the struct is used


So basically i want to know how to access struct data in a function.

Cheers in advance.
Title: Re: Accessing a struct via a function parameter
Post by: Crimson Wizard on Tue 05/07/2022 17:24:37
I've read this several times, but still not quite certain, if you want to access struct's member inside a function, or pass struct's value into a function as parameter?

Because if latter, it's done as:
Code (ags) Select

function melee_combat(int dmg_value) { // here the function receives a damage value from any source
     ....
     citizens[2].hp -= dmg_value;
     cEnemy.SayBackground(String.Format("-%d", dmg_value));
     ....
}

and then called like:
Code (ags) Select

melee_combat( citizens[0].melee_dmg ); // here you pass a value of citizens[0].melee_dmg into the function



EDIT: What puzzles me in this question is the purpose of this function. As the function is called "melee_combat", it seems like there's no need to know the damage type, as it is defined by the function itself.
Title: Re: Accessing a struct via a function parameter
Post by: Pax Animo on Tue 05/07/2022 17:36:21
Quote from: Crimson Wizard on Tue 05/07/2022 17:24:37
I've read this several times, but still not quite certain, if you want to access struct's member inside a function, or pass struct's value into a function as parameter?

Because if latter,

Yes latter and that's perfect thank you.  :)

Title: Re: Accessing a struct via a function parameter
Post by: Pax Animo on Tue 05/07/2022 17:43:36
Quote from: Crimson Wizard on Tue 05/07/2022 17:24:37

EDIT: What puzzles me in this question is the purpose of this function. As the function is called "melee_combat", it seems like there's no need to know the damage type, as it is defined by the function itself.

I wish to have different types of melee dmg_types, and choose which damage is done via the chosen key press, have i made a mess :\

for example,

Code (ags) Select
if (keycode == eKey2 && player.Animating == false) {  //different key, different dmg_type
    melee_combat(citizens[0].melee_dmg_big);
  }