Battle script optimization

Started by Gepard, Thu 08/04/2010 13:27:35

Previous topic - Next topic

Gepard

I created a pretty clumsy and bulky battle script. One of the main reasons why is it like that is that I dont know how to refer to a certain character. Here is an example:

Code: ags
function MeleeAnimation () {
 gFight.Visible = false;
 character[T].StopMoving ();
 character[M1].StopMoving ();
 character[M2].StopMoving ();
 character[M3].StopMoving ();
 character[U].StopMoving ();
 character[W].StopMoving ();
 character[HB].StopMoving ();
 character[L3].StopMoving ();
 character[L4].StopMoving ();
 if (GetGlobalInt(5)>0) {
 if (player.x < enemyx) {
 player.Animate (6,4,eOnce,eBlock);
 player.FaceLocation (enemyx, player.y, eBlock);
 }
 if (player.x >= enemyx)  {
 player.Animate (5,4,eOnce,eBlock);
 player.FaceLocation (enemyx, player.y, eBlock);
 }
 }
 else if (GetGlobalInt(15)>0) {
 if (player.x < enemyx2) {
 player.Animate (6,4,eOnce,eBlock);
 player.FaceLocation (enemyx2, player.y, eBlock);
 }
 if (player.x >= enemyx2)  {
 player.Animate (5,4,eOnce,eBlock);
 player.FaceLocation (enemyx2, player.y, eBlock);
 }
 }
 gFight.Visible = true;
}


or everyones favorite:

Code: ags
if (battlemove > 0) {
 if (player.Moving == true) battlemove -=1;
 if (character[T].Moving == true) battlemove -=1;
 if (character[M1].Moving == true) battlemove -=1;
 if (character[M2].Moving == true) battlemove -=1;
 if (character[M3].Moving == true) battlemove -=1;
 if (character[U].Moving ==true) battlemove -=1;
 }


and of course:

Code: ags
if (GetGlobalInt(3)==2) {
//SetGlobalInt (3, 1);
if (enemyammo == 0) {
 if (GetGlobalInt(5)==0) {
 if (henemy >0) {
 gFight.Visible = false;
 if (henemy2<=0) SetGlobalInt(3, 4);
 battlemove = enemymove;
 if (enemyname.Text == "brown dog") character[D].Walk (player.x, player.y, eNoBlock, eWalkableAreas);
 if (enemyname.Text == "Tika Liji") character[T].Walk (player.x, player.y, eNoBlock, eWalkableAreas);
 if (enemyname.Text == "Untam") character[M1].Walk (player.x, player.y, eNoBlock, eWalkableAreas);
 if (enemyname.Text == "Weeten thugs") character[M2].Walk (player.x, player.y, eNoBlock, eWalkableAreas);
 if (enemyname.Text == "grey dog") character[W].Walk (player.x, player.y, eNoBlock, eWalkableAreas);
 if (enemyname.Text == "heavy bot") character[HB].Walk (player.x, player.y, eNoBlock, eWalkableAreas);
 if (enemyname.Text == "Lostials guards") character[L3].Walk (player.x, player.y, eNoBlock, eWalkableAreas);
 }
 }


These are just parts of the script. As you can see I need to write down every character in the game that could be involved in the combat because I dont know any other way. Sometimes the fight can include two enemy characters, sometimes just one, never three though. It can start by attacking the char (clickin on it) or by coming close to it. Is there a way to get rid of these long scripts?

Thanks!
Drink up me 'arties! Yo ho!

Khris

Use pointers.

Above the topmost function that uses them, outside of it, write e.g.

Code: ags
Character*e1;  // enemy one
Character*e2;  // enemy two


When the player starts to fight Untam, use

Code: ags
  e1 = cUntam;


The pointer e1 now points to cUntam, i.e.

Code: ags
  if (cUntam.Moving) ...


is equivalent to

Code: ags
  if (e1.Moving) ...


Think of pointers as variables; but instead of holding a value, they hold a script object (a GUI, Character, InvItem, etc.)

Crimson Wizard

#2
Yeah, remember, parametrization (look up in the PM I wrote you some time ago  ;)).

By the way, I believe that if you'll follow Khris's suggestion, you can also use
e1.x instead of enemyx.

Don't limit yourself to only these changes, seek for more things you can simplify this way.

Oh, yes, and - if maximal enemies number is 2, then using two separate pointers is OK. However, if you consider using multiple enemies in battle, maybe it would be easier to create an enemies array:
Code: ags

Character * enemies[MAX_ENEMIES]; // static array

or
Code: ags

Character * enemies[]; // dynamic array

Gepard

Wow! That is pretty handy. I replaced all I could with these pointers and I will replace the enemyx too. However, I need to use the pointers in room scripts too. How can I import them in script header?
Drink up me 'arties! Yo ho!

Crimson Wizard

Quote from: Gepard on Thu 08/04/2010 14:49:58
Wow! That is pretty handy. I replaced all I could with these pointers and I will replace the enemyx too. However, I need to use the pointers in room scripts too. How can I import them in script header?
Not sure what you mean... Did you make them global?
You can import/export pointers as well as other variables.

Khris

Example:
// header

import Character*e1;

// global script

Character*e1;
export e1;

SMF spam blocked by CleanTalk