Jibble

There is a new version of the forums ready for testing. Please post here if you're willing to help test! We could also use the help of coders!

Author Topic: Accessing a struct via a function parameter (Solved)  (Read 195 times)

Greeting,

I have this personal function:

Code: Adventure Game Studio
  1. function melee_combat()  //i'd like a parameter here which accesses a struct (something like (function melee_combat(dmg_type)
  2. {
  3.   if (player.IsCollidingWithChar(cEnemy)) {
  4.     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;
  5.     cEnemy.SayBackground(String.Format("-%d", citizens[0].melee_dmg)); //and here too  
  6.   }
  7. }

It's called from within the on_key_press function.

Code: Adventure Game Studio
  1. if (keycode == eKey1 && player.Animating == false) {
  2.     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.
« Last Edit: 05 Jul 2022, 17:37 by Pax Animo »
I'm a slow learner but I like to ask questions.

Re: Accessing a struct via a function parameter
« Reply #1 on: 05 Jul 2022, 17:24 »
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: Adventure Game Studio
  1. function melee_combat(int dmg_value) { // here the function receives a damage value from any source
  2.      ....
  3.      citizens[2].hp -= dmg_value;
  4.      cEnemy.SayBackground(String.Format("-%d", dmg_value));
  5.      ....
  6. }
  7.  
and then called like:
Code: Adventure Game Studio
  1. melee_combat( citizens[0].melee_dmg ); // here you pass a value of citizens[0].melee_dmg into the function
  2.  


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.
« Last Edit: 05 Jul 2022, 17:37 by Crimson Wizard »

Re: Accessing a struct via a function parameter
« Reply #2 on: 05 Jul 2022, 17:36 »
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.  :)

I'm a slow learner but I like to ask questions.

Re: Accessing a struct via a function parameter
« Reply #3 on: 05 Jul 2022, 17:43 »

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: Adventure Game Studio
  1. if (keycode == eKey2 && player.Animating == false) {  //different key, different dmg_type
  2.     melee_combat(citizens[0].melee_dmg_big);
  3.   }
« Last Edit: 05 Jul 2022, 17:46 by Pax Animo »
I'm a slow learner but I like to ask questions.