HELP! Portals Code.

B

Baltasar Esteban

Guest
Hello community,

I have an object (oPortal) and I use more than 2 in a room, how can I get the X and Y of a specific portal in a room?

In the level editor you could have 2 portals to which you want to link and another 2 with those who want to link (I would use 1 single object (oPortal)), I want to be able to travel and return to the single invuculado portal. Bone 1, portal 1 is linked with 2. 3 with 4, 5 with 6.
 

matharoo

manualman
GameMaker Dev.
If you're placing them using code, you can store their id's in variables/array...
Code:
portal[0] = instance_create(32, 32, obj_portal);
portal[1] = instance_create(456, 542, obj_portal);
If you're placing them in the room editor, you can change their Instance Name/ID (Right Click and it's the second-to-last option).
Then in code you can reference that particular instance by using the id you assigned in the room editor (be it a number or a string)
Code:
"portal0".image_angle += 5;
 

The-any-Key

Member
Just use one oPortal object and use the creation code in the room editor.

In the room editor add creation code on all the portals:
Ex in room_1:
Code:
PortalName="Portal1"
PortalRoom=room_2;
Next portal in the room got:
Code:
PortalName="Portal2"
PortalRoom=room_3;
In the room where "Portal1" is linked.
Ex in room_2
Code:
PortalName="Portal1"
PortalRoom=room_1;
In the room where "Portal2" is linked.
Ex in room_3
Code:
PortalName="Portal2";
PortalRoom=room_1;
Now when the player collide with oPortal get the PortalName from oPortal and save it in a global variable or in a variable in your player object, if your player object is persistent (Ex global.PortalNameUsed or just PortalNameUsed).
Also get PortalRoom from oPortal and use room_goto(PortalRoom). Also save a global or a variable if your player object, if your player object is persistent, that tell we just used a portal:
Ex UsedPortal=true; or global.UsedPortal=true;

You are now in the new room and we need to setup the new position of the player. So we must search for the name of the linked portal.
in the step begin event (We must wait until the creation code is done in oPortal) of the player object:
Code:
if UsedPortal
{
    // Find portal link
    var portalLinkId=noone;
    with oPortal
    {
        if PortalName=other.PortalNameUsed portalLinkId=id;
    }
    // Set player pos
    x=portalLinkId.x+10; //+10 or something so we dont collide with it again in the new room
    y=portalLinkIId.y;
    // Only once
    UsedPortal=false;
}
 
B

Baltasar Esteban

Guest
The theme that portals would not use to pass the room if not to use between portals in the same room.
 

The-any-Key

Member
Ok. But you can still use the same idea.

Name the portals and then search for it and set the player position next to the linked portal position.
 
Top