SOLVED: Player: any room x y location stored

Started by steptoe, Wed 22/08/2012 08:38:19

Previous topic - Next topic

steptoe

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




arj0n

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);)

Khris

Use an array:
Code: ags
// 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
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];
  }
}

steptoe

#3
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.


Snarky

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?

Khris

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
// 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.

steptoe

Cheers guys

Khris, all seems to work fine.

Many thanks for your help.

cat

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!

SMF spam blocked by CleanTalk