Cleaning up scripts, suggestions?

Started by rodriguez1222, Sat 09/12/2006 16:07:27

Previous topic - Next topic

rodriguez1222

Im looking to optimize some of my scripts.Ã,  I would have to guess there is some way to minimize this:

Code: ags

function repeatedly_execute() {
Ã,  if (GetGlobalInt(100) == 0){btnmarheat.Width=0;}
Ã,  if (GetGlobalInt(100) == 1){btnmarheat.Width=1;}
Ã,  if (GetGlobalInt(100) == 2){btnmarheat.Width=2;}
Ã,  if (GetGlobalInt(100) == 3){btnmarheat.Width=3;}
Ã,  if (GetGlobalInt(100) == 4){btnmarheat.Width=4;}
Ã,  if (GetGlobalInt(100) == 5){btnmarheat.Width=5;}
Ã,  if (GetGlobalInt(100) == 6){btnmarheat.Width=6;}
Ã,  if (GetGlobalInt(100) == 7){btnmarheat.Width=7;}
Ã,  if (GetGlobalInt(100) == 8){btnmarheat.Width=8;}
Ã,  if (GetGlobalInt(100) == 9){btnmarheat.Width=9;}
Ã,  if (GetGlobalInt(100) == 10){btnmarheat.Width=10;}
Ã,  if (GetGlobalInt(100) == 11){btnmarheat.Width=11;}
Ã,  if (GetGlobalInt(100) == 12){btnmarheat.Width=12;}
Ã,  if (GetGlobalInt(100) == 13){btnmarheat.Width=13;}
Ã,  if (GetGlobalInt(100) == 14){btnmarheat.Width=14;}
Ã,  if (GetGlobalInt(100) == 15){btnmarheat.Width=15;}
Ã,  if (GetGlobalInt(100) == 16){btnmarheat.Width=16;}
Ã,  if (GetGlobalInt(100) == 17){btnmarheat.Width=17;}
Ã,  if (GetGlobalInt(100) == 18){btnmarheat.Width=18;}
Ã,  if (GetGlobalInt(100) == 19){btnmarheat.Width=19;}
Ã,  if (GetGlobalInt(100) == 20){btnmarheat.Width=20;}
Ã,  if (GetGlobalInt(100) == 21){btnmarheat.Width=21;}
Ã,  if (GetGlobalInt(100) == 22){btnmarheat.Width=22;}
Ã,  if (GetGlobalInt(100) == 23){btnmarheat.Width=23;}
Ã,  if (GetGlobalInt(100) == 24){btnmarheat.Width=24;}
Ã,  if (GetGlobalInt(100) == 25){btnmarheat.Width=25;}
Ã,  if (GetGlobalInt(100) == 26){btnmarheat.Width=26;}
Ã,  if (GetGlobalInt(100) == 27){btnmarheat.Width=27;}
Ã,  if (GetGlobalInt(100) == 28){btnmarheat.Width=28;}
Ã,  if (GetGlobalInt(100) == 29){btnmarheat.Width=29;}
Ã,  if (GetGlobalInt(100) == 30){btnmarheat.Width=30;}
Ã,  if (GetGlobalInt(100) == 31){btnmarheat.Width=31;}
Ã,  if (GetGlobalInt(100) == 32){btnmarheat.Width=32;}
Ã,  if (GetGlobalInt(100) == 33){btnmarheat.Width=33;}
Ã,  if (GetGlobalInt(100) == 34){btnmarheat.Width=34;}
Ã,  if (GetGlobalInt(100) == 35){btnmarheat.Width=35;}
Ã,  if (GetGlobalInt(100) == 36){btnmarheat.Width=36;}
Ã,  if (GetGlobalInt(100) == 37){btnmarheat.Width=37;}
Ã,  if (GetGlobalInt(100) == 38){btnmarheat.Width=38;}
Ã,  if (GetGlobalInt(100) == 39){btnmarheat.Width=39;}
Ã,  if (GetGlobalInt(100) == 40){btnmarheat.Width=40;}
Ã,  if (GetGlobalInt(100) == 41){btnmarheat.Width=41;}
Ã,  if (GetGlobalInt(100) == 42){btnmarheat.Width=42;}
Ã,  if (GetGlobalInt(100) == 43){btnmarheat.Width=43;}
Ã,  if (GetGlobalInt(100) == 44){btnmarheat.Width=44;}
Ã,  if (GetGlobalInt(100) == 45){btnmarheat.Width=45;}
Ã,  if (GetGlobalInt(100) == 46){btnmarheat.Width=46;}
Ã,  if (GetGlobalInt(100) == 47){btnmarheat.Width=47;}
Ã,  if (GetGlobalInt(100) == 48){btnmarheat.Width=48;}
Ã,  if (GetGlobalInt(100) == 49){btnmarheat.Width=49;}
Ã,  if (GetGlobalInt(100) == 50){btnmarheat.Width=50;}
Ã,  if (GetGlobalInt(100) == 51){btnmarheat.Width=51;}
Ã,  if (GetGlobalInt(100) == 52){btnmarheat.Width=52;}
Ã,  if (GetGlobalInt(100) == 53){btnmarheat.Width=53;}
Ã,  if (GetGlobalInt(100) == 54){btnmarheat.Width=54;}
Ã,  if (GetGlobalInt(100) == 55){btnmarheat.Width=55;}
Ã,  if (GetGlobalInt(100) == 56){btnmarheat.Width=56;}
Ã,  if (GetGlobalInt(100) == 57){btnmarheat.Width=57;}
Ã,  if (GetGlobalInt(100) == 58){btnmarheat.Width=58;}
Ã,  if (GetGlobalInt(100) == 59){btnmarheat.Width=59;}
Ã,  if (GetGlobalInt(100) == 60){btnmarheat.Width=60;}
Ã,  if (GetGlobalInt(100) == 61){btnmarheat.Width=61;}
Ã,  if (GetGlobalInt(100) == 62){btnmarheat.Width=62;}
Ã,  if (GetGlobalInt(100) == 63){btnmarheat.Width=63;}
Ã,  if (GetGlobalInt(100) == 64){btnmarheat.Width=64;}
Ã,  if (GetGlobalInt(100) == 65){btnmarheat.Width=65;}
Ã,  if (GetGlobalInt(100) == 66){btnmarheat.Width=66;}
Ã,  if (GetGlobalInt(100) == 67){btnmarheat.Width=67;}
Ã,  if (GetGlobalInt(100) == 68){btnmarheat.Width=68;}
Ã,  if (GetGlobalInt(100) == 69){btnmarheat.Width=69;}
Ã,  if (GetGlobalInt(100) == 70){btnmarheat.Width=70;}
Ã,  if (GetGlobalInt(100) == 71){btnmarheat.Width=71;}
Ã,  if (GetGlobalInt(100) == 72){btnmarheat.Width=72;}
Ã,  if (GetGlobalInt(100) == 73){btnmarheat.Width=73;}
Ã,  if (GetGlobalInt(100) == 74){btnmarheat.Width=74;}
Ã,  if (GetGlobalInt(100) == 75){btnmarheat.Width=75;}
Ã,  if (GetGlobalInt(100) == 76){btnmarheat.Width=76;}
Ã,  if (GetGlobalInt(100) == 77){btnmarheat.Width=77;}
Ã,  if (GetGlobalInt(100) == 78){btnmarheat.Width=78;}
Ã,  if (GetGlobalInt(100) == 79){btnmarheat.Width=79;}
Ã,  if (GetGlobalInt(100) == 80){btnmarheat.Width=80;}
Ã,  if (GetGlobalInt(100) == 81){btnmarheat.Width=81;}
Ã,  if (GetGlobalInt(100) == 82){btnmarheat.Width=82;}
Ã,  if (GetGlobalInt(100) == 83){btnmarheat.Width=83;}
Ã,  if (GetGlobalInt(100) == 84){btnmarheat.Width=84;}
Ã,  if (GetGlobalInt(100) == 85){btnmarheat.Width=85;}
Ã,  if (GetGlobalInt(100) == 86){btnmarheat.Width=86;}
Ã,  if (GetGlobalInt(100) == 87){btnmarheat.Width=87;}
Ã,  if (GetGlobalInt(100) == 88){btnmarheat.Width=88;}
Ã,  if (GetGlobalInt(100) == 89){btnmarheat.Width=89;}
Ã,  if (GetGlobalInt(100) == 90){btnmarheat.Width=90;}
Ã,  if (GetGlobalInt(100) == 91){btnmarheat.Width=91;}
Ã,  if (GetGlobalInt(100) == 92){btnmarheat.Width=92;}
Ã,  if (GetGlobalInt(100) == 93){btnmarheat.Width=93;}
Ã,  if (GetGlobalInt(100) == 94){btnmarheat.Width=94;}
Ã,  if (GetGlobalInt(100) == 95){btnmarheat.Width=95;}
Ã,  if (GetGlobalInt(100) == 96){btnmarheat.Width=96;}
Ã,  if (GetGlobalInt(100) == 97){btnmarheat.Width=97;}
Ã,  if (GetGlobalInt(100) == 98){btnmarheat.Width=98;}
Ã,  if (GetGlobalInt(100) == 99){btnmarheat.Width=99;}
Ã,  if (GetGlobalInt(100) == 100){btnmarheat.Width=100;}
Ã,  if (GetGlobalInt(100) == 101){btnmarheat.Width=101;}
Ã,  if (GetGlobalInt(100) == 102){btnmarheat.Width=102;}
Ã,  if (GetGlobalInt(100) == 103){btnmarheat.Width=103;}
Ã,  if (GetGlobalInt(100) == 104){btnmarheat.Width=104;}


The code makes a sort of life bar GUI increase in length as the INT increases.
Ive got quite a few of these types of scripts written in my project, just seems messy....

Thanks in advance.

Gilbert

Isn't that simple? ;)


if ((GetGlobalInt(100) >= 0)&&(GetGlobalInt(100) <= 104))btnmarheat.Width=GetGlobalInt(100);

rodriguez1222

WOW!
Perfect!  Thank you ever so much!

This one is a bit more comlicated and in fact the reason I thought I had to lay the prievious script out the way I did:

Code: ags

function room_b() {
  // script for Room: Repeatedly execute
if (GetGlobalInt (107)<=0) { oBecky.SetView(8,5);}
if (GetGlobalInt (107)==1) { oBecky.SetView(8,6); }
if (GetGlobalInt (107)==2) { oBecky.SetView(8,7); }
if (GetGlobalInt (107)==3) { oBecky.SetView(8,8); }
if (GetGlobalInt (107)==4) { oBecky.SetView(8,9); }
if (GetGlobalInt (107)==5) { oBecky.SetView(8,10);  }
if (GetGlobalInt (107)==6) { oBecky.SetView(8,11);  PlaySound (5); }
if (GetGlobalInt (107)==7) { oBecky.SetView(8,10); }
if (GetGlobalInt (107)==8) { oBecky.SetView(8,9); }
if (GetGlobalInt (107)==9) { oBecky.SetView(8,8); }
if (GetGlobalInt (107)==10) { oBecky.SetView(8,7); }
if (GetGlobalInt (107)==11) { oBecky.SetView(8,6);SetGlobalInt (100, 1);}
if (GetGlobalInt (107)==12) { oBecky.SetView(8,5); }
if (GetGlobalInt (107)==13) { oBecky.SetView(8,4);  SetGlobalInt (100, 2);}
if (GetGlobalInt (107)==14) { oBecky.SetView(8,3); }
if (GetGlobalInt (107)==15) { oBecky.SetView(8,2);  SetGlobalInt (100, 3);}
if (GetGlobalInt (107)==16) { oBecky.SetView(8,1); }
if (GetGlobalInt (107)==17) { oBecky.SetView(8,0);  PlaySound (8);  SetGlobalInt (100, 4);}
if (GetGlobalInt (107)==18) { oBecky.SetView(8,2); }
if (GetGlobalInt (107)==19) { oBecky.SetView(8,4); SetGlobalInt (100, 5);}
if (GetGlobalInt (107)==20) { oBecky.SetView(8,6); }
if (GetGlobalInt (107)==21) { oBecky.SetView(8,8);  SetGlobalInt (100, 6);}
if (GetGlobalInt (107)==22) { oBecky.SetView(8,10);  PlaySound (10); }
if (GetGlobalInt (107)==23) { oBecky.SetView(8,7); SetGlobalInt (100, 7);}
if (GetGlobalInt (107)==24) { oBecky.SetView(8,4); }
if (GetGlobalInt (107)==25) { oBecky.SetView(8,1); SetGlobalInt (100, 8);}
if (GetGlobalInt (107)==26) { oBecky.SetView(8,0);  PlaySound (5); }
if (GetGlobalInt (107)==27) { oBecky.SetView(8,3);  SetGlobalInt (100, 9);}
if (GetGlobalInt (107)==28) { oBecky.SetView(8,6); }
if (GetGlobalInt (107)==29) { oBecky.SetView(8,9);  SetGlobalInt (100, 10);}
if (GetGlobalInt (107)==30) { oBecky.SetView(8,10); }
if (GetGlobalInt (107)==31) { oBecky.SetView(8,11); PlaySound (8); SetGlobalInt (100, 11);}
if (GetGlobalInt (107)==32) { oBecky.SetView(8,9); }
if (GetGlobalInt (107)==33) { oBecky.SetView(8,7);  SetGlobalInt (100, 12);}
if (GetGlobalInt (107)==34) { oBecky.SetView(8,5); }
if (GetGlobalInt (107)==35) { oBecky.SetView(8,3); SetGlobalInt (100, 13);}
if (GetGlobalInt (107)==36) { oBecky.SetView(8,1);  PlaySound (10); }
if (GetGlobalInt (107)==37) { oBecky.SetView(8,3);  SetGlobalInt (100, 14);}
if (GetGlobalInt (107)==38) { oBecky.SetView(8,6); }
if (GetGlobalInt (107)==39) { oBecky.SetView(8,9);  SetGlobalInt (100, 15);}
if (GetGlobalInt (107)==40) { oBecky.SetView(8,11); PlaySound (1);}
if (GetGlobalInt (107)==41) { oBecky.SetView(8,7); SetGlobalInt (100, 16);}
if (GetGlobalInt (107)==42) { oBecky.SetView(8,4); }
if (GetGlobalInt (107)==43) { oBecky.SetView(8,1);  PlaySound (5); SetGlobalInt (100, 17);}
if (GetGlobalInt (107)==44) { oBecky.SetView(8,3); }
if (GetGlobalInt (107)==45) { oBecky.SetView(8,6); SetGlobalInt (100, 20);}
if (GetGlobalInt (107)==46) { oBecky.SetView(8,8); }
if (GetGlobalInt (107)==47) { oBecky.SetView(8,11); PlaySound (8); SetGlobalInt (100, 23);}
if (GetGlobalInt (107)==48) { oBecky.SetView(8,12); }
if (GetGlobalInt (107)==49) { oBecky.SetView(8,13); SetGlobalInt (100, 26);}
if (GetGlobalInt (107)==50) { oBecky.SetView(8,14); }
if (GetGlobalInt (107)==51) { oBecky.SetView(8,15); PlaySound (1); SetGlobalInt (100, 29);}
if (GetGlobalInt (107)==52) { oBecky.SetView(9,0); }
if (GetGlobalInt (107)==53) { oBecky.SetView(9,1); SetGlobalInt (100, 32);}
if (GetGlobalInt (107)==54) { oBecky.SetView(9,2); }
if (GetGlobalInt (107)==55) { oBecky.SetView(9,3); SetGlobalInt (100, 35);}
if (GetGlobalInt (107)==56) { oBecky.SetView(9,4); }
if (GetGlobalInt (107)==57) { oBecky.SetView(9,5); SetGlobalInt (100, 38);}
if (GetGlobalInt (107)==58) { oBecky.SetView(9,6); }
if (GetGlobalInt (107)==59) { oBecky.SetView(9,7); SetGlobalInt (100, 41);}
if (GetGlobalInt (107)==60) { oBecky.SetView(9,8); }
if (GetGlobalInt (107)==61) { oBecky.SetView(9,9); PlaySound (3); SetGlobalInt (100, 44);}
if (GetGlobalInt (107)==62) { oBecky.SetView(9,10); }
if (GetGlobalInt (107)==63) { oBecky.SetView(9,11); SetGlobalInt (100, 47);}
if (GetGlobalInt (107)==64) { oBecky.SetView(9,12); }
if (GetGlobalInt (107)==65) { oBecky.SetView(9,13); SetGlobalInt (100, 50);}
if (GetGlobalInt (107)==66) { oBecky.SetView(9,13); PlaySound (5);}
if (GetGlobalInt (107)==67) { oBecky.SetView(9,12); SetGlobalInt (100, 53);}
if (GetGlobalInt (107)==68) { oBecky.SetView(9,10); PlaySound (3);}
if (GetGlobalInt (107)==69) { oBecky.SetView(9,8); SetGlobalInt (100, 56);}
if (GetGlobalInt (107)==70) { oBecky.SetView(9,6); }
if (GetGlobalInt (107)==71) { oBecky.SetView(9,4); SetGlobalInt (100, 59);}
if (GetGlobalInt (107)==72) { oBecky.SetView(9,2); }
if (GetGlobalInt (107)==73) { oBecky.SetView(9,0); PlaySound (1); SetGlobalInt (100, 62);}
if (GetGlobalInt (107)==74) { oBecky.SetView(8,14); }
if (GetGlobalInt (107)==75) { oBecky.SetView(9,1); SetGlobalInt (100, 65);}
if (GetGlobalInt (107)==76) { oBecky.SetView(9,4); }
if (GetGlobalInt (107)==77) { oBecky.SetView(9,7); SetGlobalInt (100, 68);}
if (GetGlobalInt (107)==78) { oBecky.SetView(9,10); PlaySound (3); }
if (GetGlobalInt (107)==79) { oBecky.SetView(9,13); SetGlobalInt (100, 71);}
if (GetGlobalInt (107)==80) { oBecky.SetView(9,13); PlaySound (13); }
if (GetGlobalInt (107)==81) { oBecky.SetView(9,11); SetGlobalInt (100, 74);}
if (GetGlobalInt (107)==82) { oBecky.SetView(9,8); PlaySound (8); }
if (GetGlobalInt (107)==83) { oBecky.SetView(9,5); SetGlobalInt (100, 77);}
if (GetGlobalInt (107)==84) { oBecky.SetView(9,2); }
if (GetGlobalInt (107)==85) { oBecky.SetView(8,15); PlaySound (3);SetGlobalInt (100, 90);}
if (GetGlobalInt (107)==86) { oBecky.SetView(9,3); }
if (GetGlobalInt (107)==87) { oBecky.SetView(9,7); PlaySound (13);SetGlobalInt (100, 93);}
if (GetGlobalInt (107)==88) { oBecky.SetView(9,11); }
if (GetGlobalInt (107)==89) { oBecky.SetView(9,13); PlaySound (1);SetGlobalInt (100, 96);}
if (GetGlobalInt (107)==90) { oBecky.SetView(9,10); }
if (GetGlobalInt (107)==91) { oBecky.SetView(9,6); PlaySound (3);SetGlobalInt (100, 99);}
if (GetGlobalInt (107)==92) { oBecky.SetView(9,2); }
if (GetGlobalInt (107)==93) { oBecky.SetView(8,15); PlaySound (5);SetGlobalInt (100, 101);}
if (GetGlobalInt (107)==94) { oBecky.SetView(9,1); }
if (GetGlobalInt (107)==95) { oBecky.SetView(8,15); PlaySound (3);SetGlobalInt (100, 104);}

if (GetGlobalInt (107) > 95) { SetGlobalInt(100,0); SetGlobalInt(107,0); SetGlobalInt(108,0); player.ChangeRoom(95);}
}


The script reacts to a script that increases INT 107 with keypresses and a timer.  Basically as the user presses the arrow keys in sequence INT 107 increases with correct key presses within a set time.  If the keys are pressed out of order or the timer expires INT 107 decreases.
The issue I'm running into (besides the messy script) is the fact that there is a sound echo.  I think it may be from INT 107 decreasing and increasing repeatedly in one instance, but I'm not sure.

Thanks again.

Khris

This time, there's no easy way, but you can optimize the script.

First of all, if (condition) {command;} is the same as if (condition) command; - the brackets are only needed when several commands are dependant on the if-condition.

Try to group similar things together to find patterns; at first set the view, then the GlobalInt, then play the sound.

I'll give some examples, I'm sure you can work out the rest:

Code: ags
int gi=GetGlobalInt(107);

int abs(int a) {
  if (a<0) return -a;
  return a;
}

// Setview, loop param:
int lo;
// 0-17
if (gi<=17) {
  lo=11-abs(gi-6);
  if (lo<5) lo=5;
}
// 18-22
else if (abs(gi-20)<=2) lo=(gi-17)*2;
// 23-25
else if (gi<=25) lo=(25-gi)*3+1;
// 26-29
else if (gi<=29) lo=(gi-26)*3;
// 30
else if (gi==30) lo=10;
// 31-35
else if (gi<=35) lo=(36-gi)*2+1;
...

// view param:
int vi=9;
if (gi<=51 || gi==74 || gi==85 || gi==93 || gi==95) vi=8;

oBecky.SetView(vi, lo);

// set GlobalInt(100):
// 11-43
if (abs(gi-27)<=16 && (gi/2)*2!=gi) SetGlobalInt(100, gi/2-4);
// 45-83
if (abs(gi-64)<=19 && (gi/2)*2!=gi) SetGlobalInt(100, (gi/2)*3-46);
...


Good luck ;)

SMF spam blocked by CleanTalk