Complicated movement system [SOLVED]

Started by Wayne Adams, Thu 19/02/2009 05:06:04

Previous topic - Next topic

Wayne Adams

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:


Say north takes you from room one to room two, you click north:
and

Room 2.. obviously..

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

#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:
Code: ags

#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:


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




Gilbert

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:

Code: ags

#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

Code: ags

#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:
Code: ags

#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


Wayne Adams

#2
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!


Trent R

Easy rule of thumb is just to always put brackets, even if you just have one command to call.


~Trent
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Khris

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:

Code: ags
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.

Wayne Adams

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. :)


SMF spam blocked by CleanTalk