PDA

View Full Version : Help with script



Russ
08-08-2007, 01:27 PM
I have just written my first script! But it doesn't work. Here is the code:



import "std.zh"
item script HP_Magic_swapper
{
void run ()
{
if (Link->MaxHP<= 16); // Checks if Link has one heart container or less
{
Quit(); // If he does, it quits
}
else if (Link->HP<= 16); // Checks to see if Link has one heart or less
{
Quit(); // If he does, it quits
}
else
PlaySound (27); // Plays the found secret sfx
Link->MaxHP -= 16; //Subtracts one heart container
Link->HP -= 16; // And one heart
Link->MaxMP += 16; // Gives Link one Magic container
Link->MP +=16; // And slightly increases his magic
}
}


When I try to compile the script, I get an error message saying there is an unexpected else on line 12. What am I doing wrong?

ShadowMancer
08-08-2007, 01:51 PM
Cleaned up alittle for you:



import "std.zh"
item script HP_Magic_swapper
{
void run ()
{
if (Link->MaxHP<= 16 || Link->HP<= 16); // Checks if Link has one heart container or less or one heart or less
{ // the || means 'or' since you have the same result for 1 heart or 1 container, use or instead of two if statements
Quit(); // If he does, it quits
}

else { //you forgot the opening { on this else statement that is why it did not compile
PlaySound (27); // Plays the found secret sfx
Link->MaxHP -= 16; //Subtracts one heart container
Link->HP -= 16; // And one heart
Link->MaxMP += 16; // Gives Link one Magic container
Link->MP +=16; // And slightly increases his magic
}
}


Also note that Magic containers are in mutiples of 32 not 16 like hearts
so you might want to change the last two lines
Link->MaxMP += 32;
Link->MP +=32;

Russ
08-08-2007, 02:08 PM
Thank you for the help.

Edit: Now it says unexpected else on line 11.

C-Dawg
08-08-2007, 02:27 PM
Fixed ShadowMancer's script.


import "std.zh"
item script HP_Magic_swapper
{
void run ()
{
if (Link->MaxHP<= 16 || Link->HP<= 16) // DO NOT USE SEMICOLONS AFTER AN IF STATEMENT.

{ // the || means 'or' since you have the same result for 1 heart or 1 container, use or instead of two if statements
Quit(); // If he does, it quits
}

else { //you forgot the opening { on this else statement that is why it did not compile
PlaySound (27); // Plays the found secret sfx
Link->MaxHP -= 16; //Subtracts one heart container
Link->HP -= 16; // And one heart
Link->MaxMP += 16; // Gives Link one Magic container
Link->MP +=16; // And slightly increases his magic
}
}

Russ
08-08-2007, 02:47 PM
Actually Cdawg, you didn't fix it. You forgot a closing brace:D. But there is something wierd with it. If still subtracts a heart container ifLink only has one left, thus, killing him. However, if you continue from the game over screen, the subscreen shows no hearts and Link is invincible. Plus I got an error on the play sfx line, but I just deleted it and the problem was solved.

pkmnfrk
08-08-2007, 09:06 PM
Here is version awesome:


import "std.zh"
item script HP_Magic_swapper {
void run () {
//you only really need to check for either MaxHP or HP, you don't need both
if (Link->MaxHP <= 1) {
Quit(); // If he does, it quits
} else {
PlaySound (27); // Plays the found secret sfx
Link->MaxHP -= 16; //Subtracts one heart container
Link->HP -= 16; // And one heart
Link->MaxMP += 32; // Gives Link one Magic container
Link->MP += 32; // And slightly increases his magic
} //if
} //run
} //script


Script debugging becomes much easier if you pick one style of formatting, and stuck with it

C-Dawg
08-08-2007, 09:54 PM
I think he wanted to check for current HP as well as MaxHP, so the player can't kill themselves. Remember he loses one heart as well as one heart container.

ShadowTiger
08-08-2007, 11:07 PM
But there is something wierd with it. If still subtracts a heart container ifLink only has one left, thus, killing him. However, if you continue from the game over screen, the subscreen shows no hearts and Link is invincible. .... Holy Crap. That is the coolest thing ever.


"Link. For you to become invincible,

You Must Die.


And yes, it's something we all need to remember; Lines opening up into an if statement cannot end in a semicolon. It's not too easy to remember, initially.

pkmnfrk
08-08-2007, 11:36 PM
I think he wanted to check for current HP as well as MaxHP, so the player can't kill themselves. Remember he loses one heart as well as one heart container.

True.

Here is version Awesome Omega:


import "std.zh"
item script HP_Magic_swapper {
void run () {
//you only really need to check for either MaxHP or HP, you don't need both
if (Link->HP <= 16) {
Quit(); // If he does, it quits
} else {
PlaySound (27); // Plays the found secret sfx
Link->MaxHP -= 16; //Subtracts one heart container
Link->HP -= 16; // And one heart
Link->MaxMP += 32; // Gives Link one Magic container
Link->MP += 32; // And slightly increases his magic
} //if
} //run
} //script

Since Link's HP will never go above his Maximum unless set by a script, we can still keep the simplicity of not using the && operator (which is what should have been used in the first place)

Gleeok
08-09-2007, 03:46 AM
Here is version Super Awesome Omega:


import "std.zh"

item script HP_Magic_swapper
{
void run () {
//you only really need to check for either MaxHP or HP, you don't need both
if (Link->HP <= 16) {
Quit(); // If he does, it quits
} else {
Game->PlaySound(27); // Plays the found secret sfx
Link->MaxHP -= 16; //Subtracts one heart container
Link->HP -= 16; // And one heart
Link->MaxMP += 32; // Gives Link one Magic container
Link->MP += 32; // And slightly increases his magic
} //if
} //run
} //script

Also guys what about making a game out of it:


import "std.zh"


item script lets_play_magic_making_game

{
int RandNum = 0
void run ()
{
if (Link->HP <= 16) {
Quit();
} else if (RandNum = 0 + Rand(1) == 0) {
Link->HP -= 16;
Link->MP += 32;
} else if (RandNum = 0 + Rand(2) == 1) {
Link->HP -= 16;
Link->MP -= 96;
} else if (RandNum = 0 + Rand(3) == 2) {
Link->HP -= 16;
Link->MaxMP -= 32;
} else if (RandNum = 0 + Rand(4) == 3) {
Link->HP -= 16;
Link->MP += 96;
} else if (RandNum = 0 + Rand(5) == 4 {
Link->HP -= 16;
Link->MaxHP -= 16;
Link->MP -= 96;
} else {
Link->HP -= 16;
Link->MaxHP -= 16;
Link->MP += 32;
Link->MaxMP += 32;
}
}
}

// This is a fair game. Honest to God.hehe
Untested, but was Rand used correctly?

Gleeok
08-09-2007, 05:00 AM
I'm an idiot. So this is another good example of watch for typo's and unneccesary/ left out krap. Here is a revised version that only half works.



import "std.zh"

item script lets_play_magic_making_game

{
int RandNum = 0; // probably useless for this...but I thought it needed to be declared??
void run()
{
if (Link->HP <= 16) { //Need a better way to get a random nuber
Quit();
} else if (RandNum + Rand(1) == 0) { //random doesn't work for some reason
Link->HP -= 16;
Link->MP += 32; //Link will always get +1 magic, -1 heart
} else if (RandNum + Rand(2) == 1) { //thus the script ends here because rand always == 0
Link->HP -= 16; // i don't know how to fix it
Link->MP -= 96;
} else if (RandNum + Rand(3) == 2) {
Link->HP -= 16;
Link->MaxMP -= 32;
} else if (RandNum + Rand(4) == 3) {
Link->HP -= 16;
Link->MP += 96;
} else if (RandNum + Rand(5) == 4) { //or here, see below
Link->HP -= 16;
Link->MaxHP -= 16;
Link->MP -= 96;
} else { //also the script never gets to this point as link max hp
Link->HP -= 16; // never drops
Link->MaxHP -= 16;
Link->MP += 32;
Link->MaxMP += 32;
}
}
}

// This is a fair game. Honest to God.hehe

pkmnfrk
08-09-2007, 07:08 AM
Here is a proper usage of Rand:


import "std.zh"

item script lets_play_magic_making_game

{
void run()
{
int RandNum = Rand(5);
if (Link->HP <= 16) {
Quit();
} else if (RandNum == 0) {
Link->HP -= 16;
Link->MP += 32;
} else if (RandNum == 1) {
Link->HP -= 16;
Link->MP -= 96;
} else if (RandNum == 2) {
Link->HP -= 16;
Link->MaxMP -= 32;
} else if (RandNum == 3) {
Link->HP -= 16;
Link->MP += 96;
} else if (RandNum == 4) {
Link->HP -= 16;
Link->MaxHP -= 16;
Link->MP -= 96;
} else {
Link->HP -= 16;
Link->MaxHP -= 16;
Link->MP += 32;
Link->MaxMP += 32;
}
}
}

// This is a fair game. Honest to God.hehe

The reason it didn't work before was because Rand(1) always returns 0.

I'm not sure what your intent was, but Rand(5) will return a random integer between 0 and 4, inclusive. If you want the last block to be called, then change the Rand(5) to Rand(6).