-2

I am remaking portal in gamemaker for my semester final, I was wondering how you find an object, if I have one portal down, and go into it, the game crashes, as the 2nd portal isn't placed, and it can't get its .x,.y pos. How do I set a variable to fix this?

Faranox
  • 1
  • 1

3 Answers3

2

We don't know how you determine the destination teleporter, you should clarify that. But one variant could be to check whether the amount of portals is >= 2, so you have at least one place to go

if (instance_number(your_portal_name) >= 2)
{
    // proceed the portal mechanics
}
German Gorodnev
  • 102
  • 1
  • 7
0

I assume that in some event you have a piece of code that does the teleporting. You just have to place this piece of code in an "if" statement that verifies if the second portal exists. This way, you will attempt teleportation only if the needed exit instance exists. You can use the "instance_exists" function

for example :

if ( instance_exists( exit_portal_or_whatever_you_name_it ) )
{
    your_teleportation_code;
}
  • Wouldn't `instance_exists` also return `true` in the event of there only being one portal? – Timtech Jun 22 '17 at 14:07
  • Well, without your code it's hard to guess, but I tought your two portals were different objects as they have different colors in the original game (I would personally create two different objects with the same parent object). If they are instances from the same object, just use instance_number() to check if there are two. – An intern has no name Jun 29 '17 at 08:37
0

I would say that based on the information you gave us, German Gorodnev's answer is correct. If you only have one portal and you try to get the position of a non-existent portal, then you will get an error. So you should include an if statement that makes sure the needed portals are there before retrieving the positions.

Alex
  • 26
  • 7