Accessing a struct via a function parameter (Solved)

Started by Pax Animo, Tue 05/07/2022 17:15:54

Previous topic - Next topic

Pax Animo

Greeting,

I have this personal function:

Code: ags
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
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.
Misunderstood

Crimson Wizard

#1
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

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

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.

Pax Animo

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

Misunderstood

Pax Animo

#3
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
if (keycode == eKey2 && player.Animating == false) {  //different key, different dmg_type
    melee_combat(citizens[0].melee_dmg_big);
  }
Misunderstood

SMF spam blocked by CleanTalk