Here's how I use arrays...
#1 Global variables
- Code: Select all
#define CharTotal 10 //define a fixed global variable value
char State[3][16]={"Stop","Walk"}; //define the player's state in string
char Direction[4][16]={"Left","Center","Right"}; //define the player's direction in string
#2 Attributes array
- Code: Select all
int Atr[CharTotal][4]= //define the character Attributes as [index][HP,Attak,Defense,Speed]
{
100,125,90,100, //Char1
125,100,100,90, //Char2
90,100,100,125, //Char3
//etc
};
char Name[CharTotal][16]= //define the character Name as [index][Name1,Name2,Name3,etc]
{
"Char1","Char2","Char3", //etc
};
#3 How to use it on: Initiate player's attributes
For example you want to generate 3 (clonned) characters with random type. Player -> Create Actor -> Script Editor:
- Code: Select all
int type=rand(10); //get random number from 0 to 9, use it as the index
HP=Atr[type][0]; //get the HP value from Atr[index][hp,atk,def,spd]
Attack=Atr[type][1];
Defense=Atr[type][2];
Speed=Atr[type][3];
#4 How to use it on: Changing player's animations
First, set the animation code in Player -> Draw Actor -> Script Editor:
- Code: Select all
char str[64]; //we need a local char variable to store string
sprintf(str,"%s%s%s",Name[type],State[walk],Direction[direct+1]); //combine player Name+State+Direction, store it to "str"
ChangeAnimation("Event Actor", str, NO_CHANGE); //change player animation according to "str"
x+=direct*walk*Speed/10; //set the movement
In this case you have to have the following animation list:
- Char1="Char1StopLeft","Char1StopRight","Char1WalkLeft","Char1WalkRight"
- Char2="Char2StopLeft","Char2StopRight","Char2WalkLeft","Char2WalkRight"
- Char3="Char3StopLeft","Char3StopRight","Char3WalkLeft","Char3WalkRight"
- etc
Then, set the walking code in:
Player -> KeyDown -> Left -> ScriptEditor:
- Code: Select all
walk=1; direct=-1;
Player -> KeyDown -> Right -> ScriptEditor:
- Code: Select all
walk=1; direct=1;
Then, set the stopping code in:
Player -> KeyUp -> Left -> ScriptEditor:
- Code: Select all
walk=0; direct=-1;
Player -> KeyUp -> Right -> ScriptEditor:
- Code: Select all
walk=0; direct=1;
#5 How to use it on: Calculating the attack damage
For example you want to calculate the damage (with this formula-> damage=EnemyAtk*10/OwnDef) in Player -> Collision -> Enemy:
- Code: Select all
int damage;
damage=Atr[collide.type][1]*10/Atr[type][2]; //or damage=collide.Attack*10/Defense;
HP-=damage;
if(HP<=0)//died action here
Actor variables list:
- Code: Select all
int type,HP,Attack,Defense,Speed,walk,direct;
That's the base. You can expand it using the same style