PDA

View Full Version : Why does this not work?



Joe123
09-28-2007, 08:24 PM
if(Link->HP <=64 && Game->Counter[1] != 0){ //open if 1 (HP)
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(l);
Link->InputL = false;
} //close if 1

if(Game->Counter[1] == 0 && Link->HP !=64){ //open if 2 (Rupees)
Game->Counter[1] = Game->Counter[1] +10;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(m);
Link->InputL = false;
} //close if 2

if(Link->HP <=64 && Game->Counter[1] == 0){ //open if 3 (HP & Rupees)
Game->Counter[1] = 10;
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(n);
Link->InputL = false;
} //close if 3

if(Link->HP > 65 && Game->Counter[1] != 0){ //open if 4
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(o);
Link->InputL = false;
} //close if 4

I have this, inserted in the middle of Pkmnfrk's NPC script. It does the different functions (healing HP and all) fine, but for some reason it is determined to only play the one string (the last one) can anyone tell me why this is please?

C-Dawg
09-29-2007, 12:46 PM
It's just a logic error.

I havn't worked with Strings, but I imagine that if you tell the game to do this:

Screen->Message(1);
Screen->Message(2);

Only the second one will be displayed.

That's what you're doing. Notice that, by the time the script gets to the last IF statement, the player has already had his life filled up. Right? So the last if statement will always trigger, and always tell the screen to display it's string.

Here's a few ways to fix your script so it does what you want:

(1) Replacing your ifs with nested if-elses. This is big pain in the ass to read and debug, but without switch statements (AHEM DARKDRAGON), this is my preferred method.

(2) Having a variable, say, "int did_something." This variable is set to zero when the script starts and set to 1 once any of the if statements are executed. Add another && to each if statement so they only execute if did_somthing is still zero.

Joe123
09-29-2007, 06:50 PM
So I did this:


if(Link->HP <=64 && Game->Counter[1] > 0){ //open if 1 (HP)
Scounter = 1;
} //close if 1
else{
if(Game->Counter[1] == 0 && Link->HP !=64){ //open if 2 (Rupees)
Scounter = 2;
} //close if 2
else{
if(Link->HP <=64 && Game->Counter[1] == 0){ //open if 3 (HP & Rupees)
Scounter = 3;
} //close if 3
else{
if(Link->HP > 65 && Game->Counter[1] > 0){ //open if 4
Scounter = 4;
} //close if 4
}//else 1
} // else 2
} // else 3

if(Scounter == 4){
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(o);
Link->InputL = false;
}
else{
if(Scounter == 1){
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(l);
Link->InputL = false;
}
else{
if(Scounter == 2){
Game->Counter[1] = Game->Counter[1] +10;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(m);
Link->InputL = false;
}
else{
if(Scounter == 3){
Game->Counter[1] = 10;
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(n);
Link->InputL = false;
}
}
}
}

why does this now not work? Surely the Ifs should only be be triggered if they are set to the certain criteria? the HP filling up and stuff only works in the correct situation, but it plays the same string every time.

Gleeok
09-29-2007, 08:43 PM
ffc script real_npc_heal_ammo { //open script
void run(int s, int f, int d, int def_dir, int l, int m, int n, int o) { //open void run

int d_x;
int d_y;
int a_x;
int a_y;
int orig_d = this->Data;
int Scounter = 0;
int switch = false; //we need to check if we did anything, if so the trap is set.

if(d == 0) d = 48;


while(true) { //open while loop
d_x = this->X - Link->X;
d_y = this->Y - Link->Y;
a_x = Abs(d_x);
a_y = Abs(d_y);

if(f != 0) { //open if
if(a_x < d && a_y < d) { //open if
if(a_x <= a_y) { // open if
if(d_y >= 0) { //open if
this->Data = orig_d + DIR_UP;
} else { //close if open else
this->Data = orig_d + DIR_DOWN;
} // close else
} else { //close if open else
if(d_x >= 0) { //open if
this->Data = orig_d + DIR_LEFT;
} else { //close if open else
this->Data = orig_d + DIR_RIGHT;
} //close else
} //close if
} else { //close if open else
this->Data = orig_d + def_dir;
} //close if
} //close if

if(Link->InputL && a_x < 24 && a_y < 24) {

if (switch == false){
if(Link->HP <=64 && Game->Counter[1] > 0){ //open if 1 (HP)
Scounter = 1;
switch = true;
} //close if 1

if(Game->Counter[1] == 0 && Link->HP !=64){ //open if 2 (Rupees)
Scounter = 2;
switch = true;
} //close if 2

if(Link->HP <=64 && Game->Counter[1] == 0){ //open if 3 (HP & Rupees)
Scounter = 3;
switch = true;
} //close if 3

if(Link->HP > 65 && Game->Counter[1] > 0){ //open if 4
Scounter = 4;
switch = true;
} //close if 4

if(Scounter == 4){
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(o);
Link->InputL = false;
}

if(Scounter == 1){
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(l);
Link->InputL = false;
}

if(Scounter == 2){
Game->Counter[1] = Game->Counter[1] +10;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(m);
Link->InputL = false;
}

if(Scounter == 3){
Game->Counter[1] = 10;
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(n);
Link->InputL = false;
}

} // close if
}

Waitframe();
} // close while loop
} //close void run
} //close script


This should debug the loop, and fix the hp+80 problem. Try it and let me know if it works.
Oh, and I give th explanation in the other thread..

Joe123
09-30-2007, 05:15 AM
ahhh Gleeooooooook :( It works, but then if you want to be able to talk to the guy more than once a screen, you have to set switch back to 1 after the strings have played, but I can't do anything after the strings have played, because zscript can't detect that! So if just before the waiframe or something I put 'switch = 1;' then it just plays that one string over and over again >_<

Oh, and I had to change the true and false commands that you gave to that integer to 0 and 1, because of something about bools when compiling.

(Probably best just to keep this here now, spanning two forums is a bit silly doncha think :P)


ffc script real_npc_heal_ammo { //open script
void run(int s, int f, int d, int def_dir, int l, int m, int n, int o) { //open void run

int d_x;
int d_y;
int a_x;
int a_y;
int orig_d = this->Data;
int Scounter = 0;
int switch = 0; //we need to check if we did anything, if so the trap is set.
int switch2 = 0;

if(d == 0) d = 48;


while(true) { //open while loop
d_x = this->X - Link->X;
d_y = this->Y - Link->Y;
a_x = Abs(d_x);
a_y = Abs(d_y);

if(f != 0) { //open if
if(a_x < d && a_y < d) { //open if
if(a_x <= a_y) { // open if
if(d_y >= 0) { //open if
this->Data = orig_d + DIR_UP;
} else { //close if open else
this->Data = orig_d + DIR_DOWN;
} // close else
} else { //close if open else
if(d_x >= 0) { //open if
this->Data = orig_d + DIR_LEFT;
} else { //close if open else
this->Data = orig_d + DIR_RIGHT;
} //close else
} //close if
} else { //close if open else
this->Data = orig_d + def_dir;
} //close if
} //close if

if(Link->InputL && a_x < 24 && a_y < 24) {

if (switch == 0 && switch2 ==0){
if(Link->HP <=64 && Game->Counter[1] > 0){ //open if 1 (HP)
Scounter = 1;
switch = 1;
} //close if 1

if(Game->Counter[1] == 0 && Link->HP > 64){ //open if 2 (Rupees)
Scounter = 2;
switch = 1;
} //close if 2

if(Link->HP <=64 && Game->Counter[1] == 0){ //open if 3 (HP & Rupees)
Scounter = 3;
switch = 1;
} //close if 3

if(Link->HP > 65 && Game->Counter[1] > 0){ //open if 4
Scounter = 4;
switch = 1;
} //close if 4

if(Scounter == 4){
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(o);
Link->InputL = false;
switch2 = 1;
}

if(Scounter == 1){
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(l);
Link->InputL = false;
switch2 = 1;
}

if(Scounter == 2){
Game->Counter[1] = Game->Counter[1] +10;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(m);
Link->InputL = false;
switch2 = 1;
}

if(Scounter == 3){
Game->Counter[1] = 10;
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(n);
Link->InputL = false;
switch2 = 1;
}

} // close if
}
if(switch2 == 1){
switch = 0;
switch2 = 0;
}
Waitframe();

} // close while loop
} //close void run
} //close script

I tried this, but no luck.

Gleeok
09-30-2007, 05:33 AM
ffc script real_npc_heal_ammo { //open script
void run(int s, int f, int d, int def_dir, int l, int m, int n, int o) { //open void run

int delay = 0;
int d_x;
int d_y;
int a_x;
int a_y;
int orig_d = this->Data;
int Scounter = 0;
int switch = 0; //we need to check if we did anything, if so the trap is set.

if(d == 0) d = 48;


while(true) { //open while loop
d_x = this->X - Link->X;
d_y = this->Y - Link->Y;
a_x = Abs(d_x);
a_y = Abs(d_y);

if(f != 0) { //open if
if(a_x < d && a_y < d) { //open if
if(a_x <= a_y) { // open if
if(d_y >= 0) { //open if
this->Data = orig_d + DIR_UP;
} else { //close if open else
this->Data = orig_d + DIR_DOWN;
} // close else
} else { //close if open else
if(d_x >= 0) { //open if
this->Data = orig_d + DIR_LEFT;
} else { //close if open else
this->Data = orig_d + DIR_RIGHT;
} //close else
} //close if
} else { //close if open else
this->Data = orig_d + def_dir;
} //close if
} //close if

if(Link->InputL && a_x < 24 && a_y < 24) {

if (switch == 0 && delay == 0){
if(Link->HP <=64 && Game->Counter[1] > 0){ //open if 1 (HP)
Scounter = 1;
switch =1;
} //close if 1

if(Game->Counter[1] == 0 && Link->HP !=64){ //open if 2 (Rupees)
Scounter = 2;
switch = 1;
} //close if 2

if(Link->HP <=64 && Game->Counter[1] == 0){ //open if 3 (HP & Rupees)
Scounter = 3;
switch = 1;
} //close if 3

if(Link->HP > 65 && Game->Counter[1] > 0){ //open if 4
Scounter = 4;
switch = 1;
} //close if 4

if(Scounter == 4){
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(o);
Link->InputL = false;
}

if(Scounter == 1){
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(l);
Link->InputL = false;
}

if(Scounter == 2){
Game->Counter[1] = Game->Counter[1] +10;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(m);
Link->InputL = false;
}

if(Scounter == 3){
Game->Counter[1] = 10;
Link->HP = Link->HP +80;
Link->MP = Link->MP +256;
if(s != 0) Game->PlaySound(s);
Screen->Message(n);
Link->InputL = false;
}

} // close if
}
if (delay>0){ delay--;}
Waitframe();
if (switch ==1){
switch = 0;
delay = 360;
Scounter = 0;
}
} // close while loop
} //close void run
} //close script

Hmm...this one resets everything after a 6 second delay. Hopefully all your strings are not uber long. You might have to adjust the length.

Joe123
09-30-2007, 05:56 AM
Thankyoooooou :)

My strings are only 1 string long, so this works perfect. I thought I was going to have to have it warp you between two duplicate screens, which wouldve been a pain.

I'm such a n00b at this &#172;_&#172;

Gleeok
09-30-2007, 06:25 AM
No prob. It's alway's tough to try and change a script when you have little clue to how it was constructed in the first place. I ended up going the other route and just made up a bunch of very simple scripts to learn how. Even though most are simple by design, when put together they drastically change the entire quest. And i'm using all of them. Just because a script is simple doesn't mean the effects of it are.

Believe it or not...there really is no feature hault. Not as long as you can script a new feature. ;)

Joe123
09-30-2007, 06:27 AM
which apparently I can't :D

Ah well, I'm learning :P

C-Dawg
09-30-2007, 02:15 PM
Believe it or not...there really is no feature halt. Not as long as you can script a new feature. ;)

Limed for truth.

DarkDragon
09-30-2007, 08:40 PM
This is big pain in the ass to read and debug, but without switch statements (AHEM DARKDRAGON)

I'll get on that after the feature freeze.