Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Joe on Mon 11/09/2006 20:41:15

Title: More properties [SOLVED]
Post by: Joe on Mon 11/09/2006 20:41:15
Hello again.

How could I make another property for a character?

I mean:
e.g.: character[].Life=100

so that you can set/get that property.

Is there any way to do this?
Title: Re: More properties
Post by: monkey0506 on Mon 11/09/2006 21:14:49
struct my_char {
  Character* Char;
  int Life;
  };

my_char mcEgo;

// game_start
mcEgo.Char = cEgo;
mcEgo.Life = 100;

// EGO takes damage
mcEgo.Life -= 20; // 20 dmg

// EGO walks to point (X, Y)
mcEgo.Char.Walk(X, Y);
Title: Re: More properties
Post by: Khris on Mon 11/09/2006 21:19:35
You can't extend AGS's structs (yet).

// global script
int life[100];

function GetLife(Character*c) {
Ã, return life[c.ID];
}

function SetLife(Character*c, int l) {
Ã, life[c.ID]=l;
}

// script header
import function GetLife(Character*c);
import function SetLife(Character*c, int l);


Then use them like this:
SetLife(cEgo, 100);
SetLife(cEgo, GetLife(cEgo)-10);


monkey was quicker, posting anyway.
Title: Re: More properties
Post by: Joe on Mon 11/09/2006 21:28:56
OK thanyou Khiris. :o
monkey, i dont know why, but your code dindnt work, thanks anyway
Title: Re: More properties
Post by: monkey0506 on Mon 11/09/2006 21:33:49
Khris, just curious, why you used 100 for the size of the array, instead of, say, AGS_MAX_CHARACTERS? The current limit is 300, so it is conceivable that he might have more than 100 characters. ;)

But in the end both of these methods achieve the same result. It just comes down to a matter of preference, and, personally I would prefer to have all the functions related to any one character stored in one place (even though accessing the built-in functions requires an extra property (i.e., mcEgo.Char.Walk instead of cEgo.Walk)).

[EDIT:]

What happened Joe? Did you get an error or did it just not update properly or what? You do of course realize that everything from the line "// game_start" onward was some-sort of pseudo-code, where the line following "// game_start" would go into your global script's game_start function, the line following "// EGO takes damage" would go into that appropriate interaction event, etc., right?

Also in order to use mcEgo from room scripts you would have to put the struct definition in the script header along with an "import my_char mcEgo;" and then put an "export mcEgo;" in the global script.
Title: Re: More properties [SOLVED]
Post by: Joe on Mon 11/09/2006 21:45:18
Well maybe it worked fine, bout i found Khris code more simple.

Anyway I think i dont need to create function, do I?

Because I could do this:

//At global script top
int life[100];

//game_start
life[cEgo]=100;

//some script at attack time

life[cEgo.ID]-=20


But you see, with both codes I cant set the inital value for all lifes.
Cause I want all lifes begin being 100.
Title: Re: More properties [SOLVED]
Post by: monkey0506 on Mon 11/09/2006 21:49:50
Do something like this:

// script header
import int life[AGS_MAX_CHARACTERS]; // maybe just "import int life;" I don't remember exactly when importing arrays

// global script
int life[AGS_MAX_CHARACTERS];
export life;

void InitializeCharactersLife() {
  int i = 0;
  while (i < AGS_MAX_CHARACTERS) {
    life[i] = 100;
    i++;
    }
  }

// game_start
InitializeCharactersLife();

// some script
life[cEgo.ID] -= 20;


[EDIT:]

And you don't technically even have to use a function to initialize the health, you could just put that code directly into game_start...I just like to keep things a bit neater.
Title: Re: More properties [SOLVED]
Post by: Joe on Mon 11/09/2006 21:56:01
Ok thankyou.
My problem is finally solved.
Thanks again
Title: Re: More properties [SOLVED]
Post by: Ashen on Tue 12/09/2006 13:20:31
Incidentally, this was just suggested (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=28233.0) over in the Tech Forum, which might interest you. You may want to go add your support.