Game Development Community

dev|Pro Game Development Curriculum

Portal Tutorial

by Austin Whitlatch · 04/17/2008 (9:29 pm) · 5 comments

Earlier this week I was having issues with getting a portal system to work (player standing at X,Y and being transferred to X1,Y1). The idea and concept were simple enough, but a little daunting. This code will show you how I was able to implement and I hope it helps you guys out in your games as well.
NOTE: this implementation is only good for single player games.
Create a new file in the server folder called portalTrigger.cs.

$canTeleport = true;
This is a global variable that is used to denote when a player can travel through a portal. The reason for this is when traveling through a two way portal, the player needs a small amount of time (close to 1 second) to get out of the telepad so he his not teleported back and forth either infinitely or until the character ends up out of the portal range.
function resetTeleport()
{
   $canTeleport = true;
}
This function resets the global variable back to true. Used later in the code.
[code]
datablock TriggerData(PortalTrigger)
{
   tickPeriodMS = 100; //how many ms it ticks between checking when in trigger.
};
This code is taken directly from the default triggers.cs file. Just controls how often it will check. Here it is 1/10 of a second.
function PortalTrigger::onEnterTrigger(%this,%trigger,%obj)
{
   Parent::onEnterTrigger(%this,%trigger,%obj); //on enter trigger code
   if(%trigger.placement==1 && $canTeleport)
   {
      $canTeleport = false;
      schedule(1000, %obj, resetTeleport);
      echo(trigger2.getTransform());
      %obj.setTransform(trigger2.getTransform());
   }
   if(%trigger.placement==2 && $canTeleport)
   {
      $canTeleport = false;
      schedule(1000, %obj, resetTeleport);
      echo(trigger1.getTransform());
      %obj.setTransform(trigger1.getTransform());
   }
}
When the player enters the trigger, it finds out exactly which trigger it is on the map. I created a dynamic data field in the map called placement. Each portal has the naming standard of 'trigger' + number. The placement is the number, so trigger1 has placement = 1. Remember, for this code to work, you must put in these dynamic fields within The Torque World creator and saved in the specific *.mis file that you are working with. The schedule that is in the code is there to ensure that a player does not get teleported immediately upon entering the other portal that he was teleported to. It gives the player 1 second (which is plenty of time) to remove themselves from the portal.
%obj is in most cases your player (I haven't checked if throwing items through this portal will work, projectiles do not, but items may). %obj.setTransform is setting the players new position, in this case the position of the other portal.

I hope this helps some of you in your future games. I'd like to give special thanks to Lee and Aun for helping me out in a forum post I had a while ago. Thanks guys, its people like you that make the TGE community so great.

Please feel free to use this code as your own, I just hope someone finds it useful ;)

About the author

Recent Blogs


#1
04/17/2008 (11:43 pm)
Thank you for your effort but you should also check the teleportation script described here:
http://www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=2930
it works on multiplayer and additionally it includes sound & visual effects.

And lastly, you should put your sources under the "code" subject of forums instead of the blog. That helps others to find your sources.
#2
04/18/2008 (5:44 am)
@Austin - Nifty bit of script code.

Now, it's true that people typically submit their code samples as code resources, as opposed to blog resources. However, I have seen a decent amount of .plans that contain code. At least you didn't post engine code or a massive code block.

Good luck with your future projects, and keep the resources rolling Here
#3
04/18/2008 (6:44 am)
Quite cool!

This was a fun forum topic a while back.

Looks like you've got a nice system!
#4
04/18/2008 (1:38 pm)
Pretty neat, one thing you could do though to prevent from having to code for them being instantly teleported back is to not teleport them exactly to the other pad, but just next to it, out of the teleport range. Or put some kind of switch they need to press to activate the teleport.
#5
04/19/2008 (8:49 am)
Thanks for the suggestions :)

I know as of right now that the teleporters are not working for multiplayer and thanks much for the resources on that, it was a great help. What the scope of the project is for a single player game that has your character and your minions traveling around a maze with these teleporters everywhere. I will definitely implement portions of that multiplayer code, but Torque is one engine that is a piece of cake for single player games. This code isn't finished, I will eventually add a mesh, sounds, and maybe even a teleporter effect. What I would like to do if it were relatively easy, would be to create an offset of the player by the player's width and place him outside of the teleporter that way, but I haven't come across a way to do that yet (haven't looked because this worked for our needs).
Thanks for the responses :)