stuck in if/else loop [solved]

Started by kelp, Fri 20/04/2018 23:40:16

Previous topic - Next topic

kelp

Here's what I've written in my room script file:

Code: ags

function room_LeaveLeft()
{
  if(player.HasInventory(iCita))
  {
    player.ChangeRoom(5,  730,  400, eDirectionLeft);
  }
  else
  {
    cElle.Think("I should probably find and take my meds first.");
  }
}


When I run the game, if Elle attempts to leave by the left side without the item Cita, it displays her thought, but it displays it forever, and I can't move or do anything except exit the game completely. The cursor becomes a small clock icon while it loops. The intended result of attempting to leave the room without Cita is to display the thought once and exit the loop, but I'm not sure what I'm doing wrong/how to do that. Feels like it should be simple, but I'm stuck.

Many thanks in advance.

Crimson Wizard

#1
If I remember correctly, the "walk of edge" event is run endlessly while player is standing behind one. The solution is usually to move character away a bit from the edge, if you are not leaving room immediately.

kelp

Quote from: Crimson Wizard on Fri 20/04/2018 23:42:12
What event is this function connected to?

Also, silly question, but did you just wait, or also tried clicking/pressing any key?

Yes, I waited for ~30 seconds, which I imagine is long enough. The text seems to flash about every 3 seconds, which is what first clued me into the fact that it might be a loop. Also yes, I clicked the mouse and pressed random keyboard keys. None of that did anything. :/

Also, now time for me to look dumb lol, I have no idea what event it's connected to (or what that means). What I posted is the only thing in my room script file. Am I missing something?

Crimson Wizard

Sorry, I realized what the problem may be and edited my original reply, looks like too late. Please read it again :)

Quote from: kelp on Fri 20/04/2018 23:51:23
Also, now time for me to look dumb lol, I have no idea what event it's connected to (or what that means). What I posted is the only thing in my room script file. Am I missing something?

I meant, what event did you click on to create that function on the event pane in the room editor
Spoiler


[close]

kelp

#4
Okay, so I've also tried this now:

Code: ags

int Tried=0;

function room_LeaveLeft()
{
  if(player.HasInventory(iCita))
  {
    player.ChangeRoom(5,  730,  400, eDirectionLeft);
  }
  else if (Tried == 1)
  {
    cElle.Walk(200, 420);
  }
  else
  {
    cElle.Think("I should probably find and take my meds first.");
    Tried=1;
  }
}


With the logic being that, if it goes through once and Elle thinks her thought, the tried variable is set to one and if it loops again, it should make her walk back inside the edge (and thus quit the loop)... and it works, sort of? The loop stops, the thought text disappears and the walk cursor returns, but Elle doesn't walk to the given spot, and clicking around doesn't make her move.

This of course isn't ideal, because I'll have to reset the tried variable such that, if the player attempts to leave again without the necessary item, it will give the same thought instead of immediately walking away.

Crimson Wizard

You do not need to check for "tries", that unnecessarily complicates it.

Code: ags

function room_LeaveLeft()
{
  if(player.HasInventory(iCita))
  {
    player.ChangeRoom(5,  730,  400, eDirectionLeft);
  }
  else
  {
    cElle.Think("I should probably find and take my meds first.");
    cElle.Walk(200, 420);
  }
}


This way she will keep telling that she needs meds, and walk away from the edge.

kelp

#6
Quote from: Crimson Wizard on Sat 21/04/2018 00:19:33
This way she will keep telling that she needs meds, and walk away from the edge.

Right, that makes plenty of sense. :) Modified and tried it the way you've provided...and we're still getting stuck. :/ She is not walking to the specified location, although she does turn immediately to the direction to do so, rather than going in 90 degree turns (which was the previous behavior) (whoops I lied, sorry, she is still going in 90 degree increments).

Crimson Wizard

Maybe there is simply no walkable area there?
Try checking that, or tell character to ignore areas
Code: ags

cElle.Walk(200, 420, eBlock, eAnywhere);

kelp

Quote from: Crimson Wizard on Sat 21/04/2018 01:13:45
Code: ags

cElle.Walk(200, 420, eBlock, eAnywhere);


AHA! The eAnywhere is unnecessary because there is definitely a walkable area at (200,420), but the eBlock seems to have done the trick! (Never mind that I don't understand why...yet)

Thank you so much, Crimson Wizard! :-D

Crimson Wizard

Oh, I forgot that Walk is non-blocking by default, and this means that event was triggering over and over again, contrary to intention.

SMF spam blocked by CleanTalk