Store Character Direction (like in ChangeRoom function) in a variable? [SOLVED]

Started by shamz, Thu 11/05/2017 06:46:53

Previous topic - Next topic

shamz

Hi, there's something I can't figure out.

I really appreciate this new parameter in the player.ChangeRoom function, where I can specify which direction the character will face after the room change. I was wondering if I can store this in a variable, because I'm writing a function that runs every room change in which there's a chance the player character will be sent to a completely different room (in which the game can be saved). Otherwise, the player will go to the usual room like nothing ever happened.

Here's what I mean:
Code: ags

function SaveChanceRoomChange(int targetRoom, int targetx, int targety, String direction)
{
  if (saveRoomActive){ 
    int randomchance = Random(saveRoomChance);
    if (randomchance == 0) {
      targetRoomNumber = targetRoom;
      targetRoomX = targetx;
      targetRoomY = targety;
      player.ChangeRoom(303, 57, 119, eDirectionRight);
    }
    else {
      player.ChangeRoom(targetRoom, targetx, targety, direction);
    }
  }
  else player.ChangeRoom(targetRoom, targetx, targety, direction);
}


In the example I store the direction as a String because I'm not sure what to store it as. Any help is appreciated!

Gurok

Quote from: shamang37 on Thu 11/05/2017 06:46:53
Hi, there's something I can't figure out.

I really appreciate this new parameter in the player.ChangeRoom function, where I can specify which direction the character will face after the room change. I was wondering if I can store this in a variable, because I'm writing a function that runs every room change in which there's a chance the player character will be sent to a completely different room (in which the game can be saved). Otherwise, the player will go to the usual room like nothing ever happened.


You can use a CharacterDirection to store and pass directions among functions. It's a built-in enum (so it's really an int). You just declare an enum variable like you would a variable of any other type. Here is your example, amended:

Code: ags
// Wherever targetRoomNumber, targetRoomX and targetRoomY are also declared:
CharacterDirection targetRoomDirection;

function SaveChanceRoomChange(int targetRoom, int targetx, int targety, CharacterDirection direction)
{
	if (saveRoomActive) {
		int randomchance = Random(saveRoomChance);
		if (randomchance == 0) {
			targetRoomNumber = targetRoom;
			targetRoomX = targetx;
			targetRoomY = targety;
			targetRoomDirection = targetRoomDirection;
			player.ChangeRoom(303, 57, 119, eDirectionRight);
		}
		else {
			player.ChangeRoom(targetRoom, targetx, targety, direction);
		}
	}
	else player.ChangeRoom(targetRoom, targetx, targety, direction);
}


Note: Since it is technically an int, you can get away with storing it in an int variable if you like. I prefer using CharacterDirection because it gives you autocomplete and adds to the clarity of the code.
[img]http://7d4iqnx.gif;rWRLUuw.gi

shamz

Oh, that was simple. I guess that should've been the first thing I thought of :-D

Thanks very much!

SMF spam blocked by CleanTalk