More properties [SOLVED]

Started by Joe, Mon 11/09/2006 20:41:15

Previous topic - Next topic

Joe

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?
Copinstar © Oficial Site

monkey0506

#1
Code: ags
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);

Khris

You can't extend AGS's structs (yet).

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

Joe

OK thanyou Khiris. :o
monkey, i dont know why, but your code dindnt work, thanks anyway
Copinstar © Oficial Site

monkey0506

#4
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.

Joe

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

//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.
Copinstar © Oficial Site

monkey0506

#6
Do something like this:

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

Joe

Ok thankyou.
My problem is finally solved.
Thanks again
Copinstar © Oficial Site

Ashen

Incidentally, this was just suggested over in the Tech Forum, which might interest you. You may want to go add your support.
I know what you're thinking ... Don't think that.

SMF spam blocked by CleanTalk