Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Wayne Adams on Thu 19/02/2009 05:06:04

Title: Complicated movement system [SOLVED]
Post by: Wayne Adams on Thu 19/02/2009 05:06:04
So, in my project, the player doesn't change screens by walking off them, but rather thru a travel hud that pops up.. as illustrated here:

(http://www.wayneadams.net/stuffola/AGS_Stuff/bugs/travel1.jpg)
Say north takes you from room one to room two, you click north:
and
(http://www.wayneadams.net/stuffola/AGS_Stuff/bugs/travel2.jpg)
Room 2.. obviously..

now I'm handling this in the global script like so:

#sectionstart GO_NORTH_Click
function GO_NORTH_Click(int interface, int button)
  {
  if (player.Room == 1)
   gTravel_menu.Visible = false;
   player.ChangeRoom(2);
}
#sectionend GO_NORTH_Click


this works.. until i need the player to be able to go back or to another room...etc.. if i have multiple IF statements in the global GO_NORTH_Click function, AGS gets upset and tells me this:

code with multiple IF statements:

#sectionstart GO_NORTH_Click
function GO_NORTH_Click(int interface, int button)
  {
  if (player.Room == 1)
   gTravel_menu.Visible = false;
   player.ChangeRoom(2);
  if (player.Room == 2)
   gTravel_menu.Visible = false;
   player.ChangeRoom(1);

}
#sectionend GO_NORTH_Click


error:
(http://www.wayneadams.net/stuffola/AGS_Stuff/bugs/travel3.jpg)

I know I'm doing this wrong, but i can not for the life of me figure out how to write this function..
Any help is much appreciated..
Thanks in advance



Title: Re: Complicated movement system
Post by: Gilbert on Thu 19/02/2009 05:13:00
I haven't checked details of this post, but one obvious cause of your error is because you did not write if clauses with multiple lines onf content correctly (Even the single 'if' version was not actually correct). So:


#sectionstart GO_NORTH_Click
function GO_NORTH_Click(int interface, int button)
  {
  if (player.Room == 1) {
   gTravel_menu.Visible = false;
   player.ChangeRoom(2);
  }
}
#sectionend GO_NORTH_Click


and


#sectionstart GO_NORTH_Click
function GO_NORTH_Click(int interface, int button)
  {
  if (player.Room == 1) {
   gTravel_menu.Visible = false;
   player.ChangeRoom(2);
  }
  if (player.Room == 2) {
   gTravel_menu.Visible = false;
   player.ChangeRoom(1);
  }
}
#sectionend GO_NORTH_Click


You may even drop in an 'else' if you like, but apart from readibility of the codes the result is the same in your case:

#sectionstart GO_NORTH_Click
function GO_NORTH_Click(int interface, int button)
  {
  if (player.Room == 1) {
   gTravel_menu.Visible = false;
   player.ChangeRoom(2);
  } else if (player.Room == 2) {
   gTravel_menu.Visible = false;
   player.ChangeRoom(1);
  }
}
#sectionend GO_NORTH_Click

Title: Re: Complicated movement system
Post by: Wayne Adams on Thu 19/02/2009 05:16:15
I'll reformat my function to that structure.. i knew it was matter of inncorrect code grammar, i just didn't know how it should be laid out..
Thanks for a quick reply, Gilbet.

EDIT: That is in fact what i was doing wrong, all works well now..

Gilbet, you get one piece of triforce for the assist. Thanks a bunch!

Title: Re: Complicated movement system [SOLVED]
Post by: Trent R on Thu 19/02/2009 06:27:35
Easy rule of thumb is just to always put brackets, even if you just have one command to call.


~Trent
Title: Re: Complicated movement system [SOLVED]
Post by: Khris on Thu 19/02/2009 08:53:26
If you're going to have a lot of rooms, you might consider using custom properties:
Add one for each direction, type int, name "north", "west", etc., default value 0.
Use one function to handle all four buttons, then check for the button's ID to determine which one was clicked:

function GO_DIR_Click(GUIControl *control, MouseButton button) {
  if (button != eMouseLeft) return; // only process left-clicks
  int d;
  if (control.ID == 0) d = GetRoomProperty("north");    // north button's ID is 0
  if (control.ID == 1) d = GetRoomProperty("west");
  ...
  if (d > 0) player.ChangeRoom(d);
  else {
    // handle exceptions here
    if (player.Room == 5) {
      if (boulder_destroyed) player.ChangeRoom(7);
      else player.Say("There's a boulder blocking the way north.");
    }
  }
}


Now you can simply enter the adjacent room numbers for each room / leave them set to zero if the player can't always go there.

I just noticed that the parameters are "int interface, int button" in your function?
You're using the old 2.72, but still the parameters of a button's Click function should be "GUIControl *control, MouseButton button";
yours were used in the even then obsolete interface_click function.
Title: Re: Complicated movement system [SOLVED]
Post by: Wayne Adams on Thu 19/02/2009 13:55:45
Im gonna see about integrating that into my code after work today. Thanks man..

Snap.. forgot.. my copy of Street Fighter 4 comes in today..

Maybe I'll write it in tomorrow. :)