• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Creating a teleporter object and more about instance IDs

WasabiHeat

Member
Recently i've been trying to cut down on the amount of unnecessary objects in my game and one that i've been trying to cut down on is my teleporter objects. I want to make a single teleporter object where each instance of the object will have a "warpID" variable that's set in the room editor. If the player touches an open teleporter they'll be warped to another teleporter object in the same room, so long as it has the same warpID number. I saw that using a combination of a for loop and instance_find is a good way to do something like that, but i'm not entirely sure on how to actually use instance id's to modify an object instance.

GML:
if (other.openPort) {
    for (i = 0; i < instance_number(oTeleporter); i += 1)
   {
   var finder = instance_find(oTeleporter,i);
   }
}
This bit of code is in my player object, for when it collides with a teleporter. openPort is just a variable to see if the teleporter the player just touched is actually open (which they all are by default). What i'm mostly struggling with is what to do after var finder is initialized. I just want to check the warpID of the teleporter that was just identified by instance_find and then change the player's position to the new teleporter if the warpID of both teleporters match.

Having said that, I've also heard rumblings that setting up all of my teleporter id's into an array would be an even slicker solution to this problem than using a temporary variable. I'm not sure where and when I would initialize that array or what the advantages actually are though. Even still, i'd mostly like to learn more about instance id's and how they work/can be manipulated and would appreciate any help or resources to let me get to know them better.
 
Last edited:

Slyddar

Member
Like you say, have 2 teleporters in the room with the same warp_id. Your teleporters also need an active flag, so they don't ping pong the player. In their o_teleporter collision event with the player you can check the warp id and also ensure it's not trying to warp to itself with this:

Code:
//o_teleporter collision event with player
if active {
  with(o_teleporter) {
    if warp_id == other.warp_id and id != other.id {
       active = false;
       o_player.x = x;
       o_player.y = y;
    }
  }
} else alarm[0] = 2;
Then in alarm[0] you can set active = true, enabling it to be used again when the player walks off it. You may need to increase that value to suit.
 
Last edited:

WasabiHeat

Member
Ah, that worked quite well, thank you! I'm glad I was on the right track with comparing instance id's but i'm still a little curious as to what I can do with an instance id after i've stored it in a variable...
 

Slyddar

Member
i'm still a little curious as to what I can do with an instance id after i've stored it in a variable...
Depends what you want to achieve, but you can use it with a dot (.) accessor to basically read/write any of the variables of that particular instance. Every instance has a unique id, and using dot notation with the instance id allows you to work with that specific instance however you require.
 
Top