PDA

View Full Version : Varible Problems..



ShadowMancer
06-05-2007, 04:13 PM
I've been wracking my brain all day trying to figure out what is wrong with this code, I'm stumped so lets see if anyone else can tell me what i am doing wrong.

It compiles fine, but it does nothing, i put in a Trace(); to test the results of the varible but all i get is 0's



ffc script pathTest{
void run(int this_ffc_num)
{

while (true){
//declare vars
int best_dir = 0;

// "this_ffc->" bug workaround
ffc this_ffc = Screen->LoadFFC(this_ffc_num);
int rnum = Floor(Rand(4)+1);
if (rnum==1) { best_dir = 1; }
if (rnum==2) { best_dir = 2; }
if (rnum==3) { best_dir = 3; }
if (rnum==4) { best_dir = 4; }
//routine to move FFC in chosen direction
Trace(best_dir);
if (best_dir==1)
{
for (int n=1 ; n<16 ; n+=4)
{
this_ffc->Y-=4;
Waitframe();
}
}
if (best_dir==2)
{
for (int n=1 ; n<16 ; n+=4)
{
this_ffc->X-=4;
Waitframe();
}
}
if (best_dir==3)
{
for (int n=1 ; n<16 ; n+=4)
{
this_ffc->Y+=4;
Waitframe();
}
}
if (best_dir==4)
{
for (int n=1 ; n<16 ; n+=4)
{
this_ffc->X+=4;
Waitframe();
}
}


Waitframe();
}//ends while true
}//ends void run
}//ends pathTest


This is not my original script, but a simplified version, still the problem remains the same. it SHOULD move the FFC one grid space in a random direction every 4 tics

EDIT: okay i figured out the Rand problem (i forgrot to floor it) but now it looks like that was not the problem in the original script, (i did not use a random number) so mabye i can figure it out now...

Also, this time when i ran a Trace(rnum); i thought it would have given me the output of the random number but it just gave me a bunch of Zasm (which i only partialy understand) how can i just get the output of a var??

DarkDragon
06-05-2007, 04:48 PM
Sure enough, you've found a bug: Rand() currently returns a float, instead of an integer as documented. Fixed in the next build.

Dan Furst
06-05-2007, 05:13 PM
Yeah, Rand() is defined as int but returns a float. Good catch.

Here is a cleaned-up version that does what you want. Note that the ffc will probably fly off the screen, since there is no bounds checking. Don't forget to pass in the ffc number with D0.


import "std.zh"
ffc script pathTest{
void run(int this_ffc_num)
{
// "this_ffc->" bug workaround
ffc this_ffc = Screen->LoadFFC(this_ffc_num);

while(true)
{
int best_dir = Floor(Rand(4))+1;
//routine to move FFC in chosen direction
if(best_dir==1)
{
for(int n=0 ; n<4 ; n++)
{
this_ffc->Y-=4;
Waitframe();
}
}
else if(best_dir==2)
{
for(int n=0 ; n<4 ; n++)
{
this_ffc->X-=4;
Waitframe();
}
}
else if(best_dir==3)
{
for(int n=0 ; n<4 ; n++)
{
this_ffc->Y+=4;
Waitframe();
}
}
else if(best_dir==4)
{
for(int n=0 ; n<4 ; n++)
{
this_ffc->X+=4;
Waitframe();
}
}
else
{
Waitframe();
}
}//ends while true
}//ends void run
}//ends pathTest

If you don't want it to fly off the screen, I would suggest adding some ifs like so:



for(int n=0 ; n<4 ; n++)
{
if(this_ffc->Y < 4)
{
this_ffc->Y+=4;
}
else
{
this_ffc->Y-=4;
}
Waitframe();
}

ShadowMancer
06-05-2007, 05:49 PM
Thank you DarkDragon and Dan Furst,
as i said this is a simplified version,
i already have walkabilty checking and decision makeing in place, just a few tweaks now and Zelda Classic will have a working A* Pathfinding script :)

as soon as it is working perfectly I will post it in Script Showcase