Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: steptoe on Wed 22/08/2012 08:38:19

Title: SOLVED: Player: any room x y location stored
Post by: steptoe on Wed 22/08/2012 08:38:19
Hi

How would you store a players x y room locations.

You can change rooms anytime in the game (unless that room completed):

If you go to room 5 from room 3 and then warp back to room 3 I would like the player to appear exactly where he left it. This would apply to all rooms visited.

Can't find the answer at the moment.

cheers



Title: Re: Player: any room x y location stored
Post by: arj0n on Wed 22/08/2012 09:48:42
You could use global variables (int), x and y for each room.
(like r1x, r1y, r2x, r2y, etc).

When leaving a room  you can store the players current x and y into the corresponding global variable.
(like "r1x = 50;" & "r1y = 400;")

When entering a specific room you can use the stored x and y to place the player where he left that room.
(like player.ChangeRoom(1, r1x,  r1y);)
Title: Re: Player: any room x y location stored
Post by: Khris on Wed 22/08/2012 09:52:47
Use an array:
Code (ags) Select
// global header
import int room_x[100], room_y[100];

// global script
int room_x[100], room_y[100];
export room_x, room_y;


Now add the on_event function, if you haven't already:
Code (ags) Select
function on_event (EventType event, int data) {
  if (event == eEventLeaveRoom) {
    room_x[data] = player.x;
    room_y[data] = player.y;
  }
  if (event == eEventEnterRoomBeforeFadein) {
    player.x = room_x[data];
    player.y = room_y[data];
  }
}
Title: Re: Player: any room x y location stored
Post by: steptoe on Wed 22/08/2012 11:37:44
Thanks guys,

maybe I never put it across properly.

small technicality, player can only change rooms via buttons. The button(s) event store that rooms x and y when pressed and transports to another room depending on button pressed ie room 1 room 2 room 3 etc etc when returning to a previous room ags remembers x and y for that room and places players in exact x and y.

The player can therefore change rooms at anytime and at any position in that room.

I have also set up a Listbox for room number to go to as an option at the moment.

Title: Re: Player: any room x y location stored
Post by: Snarky on Wed 22/08/2012 13:38:04
I don't see why those technicalities should mean you can't use Khris's solution. In fact, what you describe sounds very much like what he wrote. What exactly is your problem?
Title: Re: Player: any room x y location stored
Post by: Khris on Wed 22/08/2012 16:19:18
Yeah, barefoot, just try it out.
There is something missing in my code though; I'm guessing using this as-is will always put the player at 0,0 in a room they haven't visited before.

Here's an amended version:
Code (ags) Select
// global header
import int room_x[100], room_y[100];
import bool room_visited[100];
     
// global script
int room_x[100], room_y[100];
bool room_visited[100];
export room_x, room_y, room_visited;

function on_event (EventType event, int data) {
  if (event == eEventLeaveRoom) {
    room_x[data] = player.x;
    room_y[data] = player.y;
    room_visited[data] = true;
  }
  if (event == eEventEnterRoomBeforeFadein && room_visited[data]) {
    player.x = room_x[data];
    player.y = room_y[data];
  }
}


Now the first time player.ChangeRoom(r, x, y) is called for each room r, the coordinates work, and every subsequent time, the previous position overrides them.
So it's save to put that command with coordinates into each room change button's function.
Title: Re: Player: any room x y location stored
Post by: steptoe on Wed 22/08/2012 17:22:29
Cheers guys

Khris, all seems to work fine.

Many thanks for your help.
Title: Re: SOLVED: Player: any room x y location stored
Post by: cat on Thu 23/08/2012 21:46:57
Just wanted to say that I didn't know about on_event and how it can be used.
So thank you Khris for bringing my attention to it!