Custom ChangeRoom function

Started by brushfe, Mon 14/07/2025 11:22:13

Previous topic - Next topic

brushfe

Hello!

I currently call a Wait(20) command before changing rooms, which is going to add up to a lot of repeated code, so I wondered if there was a way to create these two lines as a single custom function?

Code: ags
Wait(gvWaitTime);
player.ChangeRoom(8, 212, 126, eDirectionDown);

I gave it a shot, but ran into trouble trying to pass the CharacterDirection enum...

Thanks for any help!

Khris

This should do it:

Code: ags
// global header
import void ChangeRoom(int room, int = -10000, int y = -10000, CharacterDirection direction = -1);

// global script
void ChangeRoom(int room, int x, int y, CharacterDirection direction) {
  Wait(gvWaitTime);
  if (x == -10000) x = player.x;
  if (y == -10000) y = player.y;
  if (direction == -1) player.ChangeRoom(room, x, y);
  else player.ChangeRoom(room, x, y, direction);
}

You can now call for instance ChangeRoom(2); or pass more optional parameters, just like the original function.

Also note that in this specific case you can just put the Wait() command into on_event / eEventLeaveRoom, which should have the exact same effect.

Final note: I didn't write an extender function because I assume your game only uses one player character.

brushfe

Fascinating, thanks very much! It's really instructive to see how you've built this in such a flexible fashion that also keeps it error-free.

I really appreciate it, thank you again!

SMF spam blocked by CleanTalk