Jump to content

Photo

Teleporting function hangs the game up.

Crash while loop wizzrobe teleporting

  • Please log in to reply
8 replies to this topic

#1 Orithan

Orithan

    Studying Scientist - Commission from Silvixen

  • Members
  • Location:Australia

Posted 07 May 2014 - 05:59 AM

I've been using this teleporting function I found in another topic here for a ghosted Wizzrobe and it teleports fine... until it hangs the game.

The script will usually run for some time before it hangs the game, usually when Link is at an angle away from the Wizzrobe.

 

void teleport(npc ghost){
    int teleX;
    int teleY;
    bool reroll = true;
    while(reroll){  
        teleX = GridX(Rand(32,224));
        teleY = GridY(Rand(32,112));
        TraceB(reroll);
        reroll = false;
        if(Screen->isSolid(teleX,teleY) || Abs(teleX - Link->X) < 48 || Abs(teleY - Link->Y) < 48){
            reroll = true;
        }
    }
    Ghost_X = teleX;
    Ghost_Y = teleY;
}
 

 

 

I made some edits and I found exactly this section of code to be the culprit, as when removed, stops the crashes:

 

Abs(teleX - Link->X) < 48 || Abs(teleY - Link->Y) < 48

 

 

However, removing this also allows the Wizzrobes to teleport close to Link or sometimes even right on top of him.

 

Can somebody help?



#2 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 07 May 2014 - 06:05 AM

You forgot to add a Waitframe() in that while loop. That's likely the true culprit.



#3 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 07 May 2014 - 06:06 AM

That's because if the if statement ends up being false, it'll skip it and then the while loop will continue, and you haven't put a Wiatframe();, which causes the crashing. Add the waitframe after the if statement closing bracket. :)

#4 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 07 May 2014 - 06:08 AM

That's because if the if statement ends up being false, it'll skip it and then the while loop will continue, and you haven't put a Wiatframe();, which causes the crashing. Add the waitframe after the if statement closing bracket. :)

I NINJA'D YOU! BAHAHAHAHAHA!

 

... Sorry for that outburst. Don't know what came over me. XD


  • David likes this

#5 David

David

    Fallen leaves... adorn my night.

  • Administrators
  • Real Name:David
  • Pronouns:He / Him

Posted 07 May 2014 - 06:38 AM

I NINJA'D YOU! BAHAHAHAHAHA!
 
... Sorry for that outburst. Don't know what came over me. XD

Lol. If only my post was shorter....... :P

#6 Saffith

Saffith

    IPv7 user

  • Members

Posted 07 May 2014 - 07:59 AM

That shouldn't need a Waitframe; it's supposed to stop looping once it finds a valid location.
The problem is that the disallowed range on the Y axis is too wide. 48+48=96, but 112-32=80. If Link's near the center of the screen, the point selected will always be too close. Try this instead:
if(Screen->isSolid(teleX,teleY) || (Abs(teleX - Link->X) < 48 && Abs(teleY - Link->Y) < 48)){
Incidentally, this is the perfect time to use a do-while loop.
 
    do{  
        teleX = GridX(Rand(32,224));
        teleY = GridY(Rand(32,112));
    } while(Screen->isSolid(teleX,teleY) || (Abs(teleX - Link->X) < 48 && Abs(teleY - Link->Y) < 48));


#7 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 07 May 2014 - 08:18 AM

I wasn't aware that we had do while. That's new. But what's the difference between do while, and simply putting the same info inside a regular while loop?



#8 Saffith

Saffith

    IPv7 user

  • Members

Posted 07 May 2014 - 08:33 AM

Compared to using a bool as in the original loop, essentially nothing. It just makes sense to use it. do-while runs once, checks the condition, and repeats if it needs to, so it's perfect when you want to do something that might fail and need to be repeated.

#9 Master Maniac

Master Maniac

    Earth, Wind, Fire, and Water.

  • Members
  • Real Name:kris

Posted 07 May 2014 - 08:39 AM

Ah, I see. Thanks :P





Also tagged with one or more of these keywords: Crash, while loop, wizzrobe, teleporting

1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users