passing an array index into a function

Started by aussie, Mon 20/07/2015 12:42:07

Previous topic - Next topic

aussie

Hi all,
I have this function in my global script where I have defined an array containing the same variable (health) for a series of characters. It looks like this:

Code: ags

funtion character_health();
{
int health [10];
health [0]=100;
health [1]=80;
...
health [9]=50;
}


Much later, I have different function to check if a character is dead once it takes a hit during a battle. More or less like this:

Code: ags

funtion character_takes_hit();
{
(...)

int hit = 50;

if (health[0]>=hit) {health [0]=health[0]-50;}
else {//character dead code goes here}

}


The problem is I get an "undefined symbol 'health'" error. It looks like AGS cannot pass array index 0 into the second function. Is that so or am I doing anything wrong?

I've run a forum search and it looks like passing array indexes into functions used to be problematic. However, the earliest post I found regarding this topic is from 2010. I wonder whether it's been fixed.
It's not the size of the dog in the fight. It's the size of the fight in the dog.

http://www.freewebs.com/aussiesoft/

Crimson Wizard

#1
Hello, aussie.

Your mistake is that you have declared the array as local to the function (character_health). This means that it exists only in that function, and is destroyed as soon as the function ends.
To keep array all time in your game, and be able to access that array from any other function, you need to declare it as global.


For example:
Code: ags

int health [10]; // <----- notice the array is put outside of any function

function character_health();
{
health [0]=100;
health [1]=80;
...
health [9]=50;
}

function character_takes_hit();
{
<...>
if (health[0]>=hit) {health [0]=health[0]-50;}
else {//character dead code goes here} 
}

aussie

I see. That makes plenty of sense.

Thankyou so much!!
It's not the size of the dog in the fight. It's the size of the fight in the dog.

http://www.freewebs.com/aussiesoft/

Khris

There's still plenty wrong with the code.

Header:
Code: ags
import bool character_takes_hit(int which, int damage = 50); // default value


Main Script:
Code: ags
int health[10];

void init_health() {
  health[0] = 100;
  ...
}

bool character_takes_hit(int which, int damage) {
  if (which < 0 || which >= 10) return false;
  health[which] -= damage;
  return health[which] <= 0;
}

// inside game_start
  init_health();


Then in your interactions:
Code: ags
  if (character_takes_hit(3, 10)) {
    // character 3 just died
  }
  else {
    // character 3 just lost 10 HP but is still alive
  }

ollj

if you store anything in struct instances. the struct instances are like variables with a "lifetime", they are global or local withind a function, with their data being exported or not_exported.
if that struct instance is not exported, the instance may have the same name in the source code in 2 different rooms or scripts, but it compiles to a different address, and that may store null-pointers, leading to an exception_error_crash.
how to have that not happen:

struct structname{
    int value;
}
import structname structinstance; //in the header of a struct; you may import an instance of a struct, so that every other script has access to that instance.

//end of header, start of body

structname structinstance; //at the start of the body of a script you may need to define that instance;

export structinstance; //at the end of the BODY of a script you may need to export that instance, otherwise its values will be seperate/different (addresses) for every script.

//that importing and exporting makes that instance of that struct global and unique from any script and room_script...

SMF spam blocked by CleanTalk