Teleporting (FAQ)

In this forum you will find and post information regarding the modding of Star Wars Battlefront 2. DO NOT POST MOD IDEAS/REQUESTS.

Moderator: Moderators

Gametoast Staff
User avatar
Posts: 2973
Joined: Sun Feb 26, 2006 7:36 am
Location: USA
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set

Re: Teleporting

Postby [RDH]Zerted » Sun Sep 09, 2007 5:13 pm

Ace_Azzameen_5 wrote:Wow...thats really complicated as opposed to the custom "Moveentitytomatrix" function that Penguin released in the other teleport thread. (+ the GetCharacterVehicle stuff I added)

Excuse the criticism, but that just looks like a lot of extra custom functions and possibly redundant nil checks. The debugs are nice, but, I like simplicity...

...It seems to work and I thought that 'if Variable then' checked to see if the variable was nil...

Off-Topic: Zerted, do you know how or what entity matrixes are? I could never get them to print into bf2log. I wanted to be able to "dissect" them and alter say, the X value of the coordinates...


It is more complicated and it is a lot more extra functions doing almost nothing, but none of the nil checks are redundant. Someone could call any of the functions directly, so they all need to check their parameters. The more checks, the more errors caught right as they happen which leads to faster debugging too. I wrote this to basically be an interface to Moveentitytomatrix. For my code, you don't have to know if you can teleport a character or only the character's unit. You don't need to figure out if something is an entity or not. All you need to do is call the Location and Destination functions and you can keep your Lua short and clean.

if variable then tests if variable is true (1, 'true', exists) or false (0, 'false', nil [not existing]). When true, it runs the block of then code. When false, it runs the block of else code (if any).

Matrix functions:
AttachEffectToMatrix
GetEffectMatrix
GetEntityMatrix
CreateMatrix
SetEffectMatrix
SetEntityMatrix

GetScreenPosition




Important Edit:
I will keep updating the code at that link until it's finished. Right now everything seems to be working. Its important to note that a region has to have its type/class properties be the same as its name inorder for it to work with SetupEnterRegionTeleport()

Steps to getting it working (using an example eli map):
1) Download teleport.lua or copy the code below and paste it in a text file then rename it teleport.lua
Hidden/Spoiler:
Code: Select all


tele_who_All = 0
tele_who_BotOnly = 1
tele_who_HumOnly = 2

tele_what_All = 0
tele_what_UniOnly = 1
tele_what_VehOnly = 2


--[[

Possible Starting Locations:
Character
Unit
Vehicle

Possible Ending Destinations:
Character
Unit
Vehicle
PathNode

Destinations Funcntions:
GetCharacterDestination( character )
GetCharacterUnitDestination( character )
GetCharacterVehicleDestination( character )
GetPathNodeDestination( path, nodeNumber )
GetUnitDestination( unit )
GetVehicleDestination( vehicle )


GetCharacterLocation( character )
GetCharacterUnitLocation( character )
GetCharacterVehicleLocation( character )
GetRegionLocation( region )
GetUnitLocation( unit )
GetVehicleLocation( vehicle )



DoSingleTeleport( location, destination )
SetupEnterRegionTeleport( regionLocation, destination, who, what )

TeleportEntityToMatrix( entity, destination )

--]]



-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Main modder functions.  Use these to setup teleportation systems --
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- DoSingleTeleport
-- SetupRegionTeleport


--
-- Does a single teleport based on the given locations
--
-- Example Usage:
--
--this will teleport a player to CP3's spawn path's first node
--local from = GetCharacterLocation( player )
--local to = GetPathNodeDestination( "cp3_spawn", 1 )
--DoSingleTeleport( from, to )
--
function DoSingleTeleport( location, destination )
   print("Teleport: DoSingleTeleport: location, destination: ", location, destination )

   --check input
   if location == nil then
      print("Teleport: DoSingleTeleport: No location to teleport from")
      return
   elseif destination == nil then
      print("Teleport: DoSingleTeleport: No teleport destination")
      return
   end

   --do the teleport
   TeleportEntityToMatrix( location, destination )
end


--
-- Teleports the units entering the region to the given destination
--
-- region - the region the player needs to enter in order to be teleported
-- destination - the location where the player will be teleported to
--               use the GetXXXXXXXsDestination() to get the correct value
-- who - 'tele_who_All' if all units should be teleported
--       'tele_who_BotOnly' if only AI units should be teleported
--       'tele_who_HumOnly' if only human units should be teleported
-- what - 'tele_what_All' if both units and vehciles should be teleported
--        'tele_what_UniOnly' if only units should be teleported
--        'tele_what_VehOnly' if only vehciles should be teleported
--
--
-- Notes:
-- To disable teleporting, deactivate the region
-- To enable teleporting, activate the regithankson
--
--
--Example Usage:
--
--this will teleport anyone entering the capture region of CP2 to CP3's spawn path's first node
--local region = GetRegionLocation( "cp2_capture" )
--local destination = GetPathNodeDestination( "cp3_spawn", 1 )
--SetupEnterRegionTeleport( region, destination, tele_who_All, tele_what_All )
--ActivateRegion( "cp2_capture" )
--
function SetupEnterRegionTeleport( region, destination, who, what )
   print( "Teleport: SetupEnterRegionTeleport(): ", region, destination, who, what )

   --check input
   if region == nil then
      print("Teleport: SetupRegionTeleport needs a starting region")
      return
   elseif destination == nil then
      print("Teleport: SetupRegionTeleport needs a location to teleport to")
      return
   elseif who ~= tele_who_All and who ~= tele_who_BotOnly and who ~= tele_who_HumOnly then
      print("Teleport: SetupRegionTeleport needs to know who to teleport")
      return
   elseif who ~= tele_what_All and who ~= tele_what_UniOnly and who ~= tele_what_VehOnly then
      print("Teleport: SetupRegionTeleport needs to know what to teleport")
      return
   end

   --the event handling
   local trigger
   trigger = OnEnterRegion(
       function(region, char)
       
         --teleport only those who should be telepeorted
           if who == tele_who_All then
            --do nothing extra here
           elseif who == tele_who_BotOnly then
            if IsCharacterHuman(char) then
               --do not teleport human units
               --print("Teleport: OnEnterRegion: who: skipping this human")
                return
            end
           elseif who == tele_who_HumOnly then
            if not IsCharacterHuman(char) then
               --do not teleport AI units
               --print("Teleport: OnEnterRegion: who: skipping this bot")
                return
            end
           end
          
           --check about vehicles
         local location = nil
           if what == tele_what_All then
               location = GetCharacterLocation( char )
           elseif what == tele_what_UniOnly then
               location = GetCharacterUnitLocation( char )
           elseif what == tele_what_VehOnly then
               location = GetCharacterVehicleLocation( char )
           end

          --teleport the player
         TeleportEntityToMatrix( location, destination )
      end, region
   )
end



-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Utility functions to convert everything possible into a destination --
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Convert a vehicle
-- Convert a character
-- Convert a character unit
-- Convert a character vehicle
-- Convert a unit
-- Convert a vehicle
-- Convert a path w/path node
--TODO add GetCharacterRemote and GetCharacterControllable
-- Note: Regions can't be destinations



--
-- Gets the destination of the given character
-- Checks if the character is in a vehicle too
--
function GetCharacterDestination( character )
   --check input
   if character == nil then
      print("Teleport: GetCharacterDestination(): Character is nil")
      return nil
   end

   --try getting the unit first   
   local dest = GetCharacterUnitDestination( character )
   if dest ~= nil then
      return dest
   end
   
   --no character unit, so maybe the character is in a vehicle
   return GetCharacterVehicleDestination( character )   
end



--
-- Gets the destination of the given character's vehicle
--
function GetCharacterVehicleDestination( character )
   --check input
   if character == nil then
      print("Teleport: GetCharacterVehicleDestination(): Character is nil")
      return nil
   end

   --get the character's vehicle
   local vehicle = GetCharacterVehicle( character )
   if vehicle == nil then
      print("Teleport: GetCharacterVehicleDestination(): Character has no vehicle")
      return nil
   end
   
   return GetVehicleDestination( vehicle )
end



--
-- Gets the destination of the given character's unit
--
function GetCharacterUnitDestination( character )
   --check input
   if character == nil then
      print("Teleport: GetCharacterUnitDestination(): Character is nil")
      return nil
   end

   --get the character's unit
   local unit = GetCharacterUnit( character )
   if unit == nil then
      print("Teleport: GetCharacterUnitDestination(): The character's unit is nil.  The character may be dead or in a vehicle")
      return nil
   end

   return GetUnitDestination( unit )
end



--
-- Gets the destination of the given vehicle
--
function GetVehicleDestination( vehicle )
   --check input
   if vehicle == nil then
      print("Teleport: GetVehicleDestination(): Vehicle is nil")
      return nil
   end

   return GetEntityMatrix( vehicle )
end



--
-- Gets the destination of the given unit
--
function GetUnitDestination( unit )
   --check input
   if unit == nil then
      print("Teleport: GetUnitDestination(): The unit is nil")
      return nil
   end

   return GetEntityMatrix( unit )
end



--
-- Gets the destination of the given node on the given path
--
function GetPathNodeDestination( path, nodeNumber )
   --check input
   if path == nil then
      print("Teleport: GetPathNodeDestination(): The path is nil")
      return nil
   elseif nodeNumber == nil then
      print("Teleport: GetPathNodeDestination(): The node number is nil")
      return nil
   end

   return GetPathPoint( path, nodeNumber )
end



-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Utility functions to convert everything into a location --
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Locate a vehicle
-- Locate a character
-- Locate a unit
-- Locate a region



--
-- Gets the location of a vehicle
--
function GetVehicleLocation( vehicle )
   --check input
   if vehicle == nil then
      print("Teleport: GetVehicleLocation(): Vehicle is nil")
      return nil
   end

   return vehicle
end



--
-- Gets the unit or vheicle location of the given character
--
function GetCharacterLocation( character )
   --check input
   if character == nil then
      print("Teleport: GetCharacterLocation(): Character is nil")
      return nil
   end

   --try getting the unit first
   local location = GetCharacterUnitLocation( character )
   if location ~= nil then
      return location
   end

   --no character unit, so maybe the character is in a vehicle
   return GetCharacterVehicleLocation( character )
end



--
-- Gets the unit location of the given character
--
function GetCharacterUnitLocation( character )
   --check input
   if character == nil then
      print("Teleport: GetCharacterUnitLocation(): Character is nil")
      return nil
   end

   --get the character's unit
   local unit = GetCharacterUnit( character )

   return GetUnitLocation( unit )
end



--
-- Gets the vehicle location of the given character
--
function GetCharacterVehicleLocation( character )
   --check input
   if character == nil then
      print("Teleport: GetCharacterVehicleLocation(): Character is nil")
      return nil
   end

   --get the character's vehicle
   local vehicle = GetCharacterVehicle( character )

   return GetVehicleLocation( vehicle )
end



--
-- Gets the location of the given unit
--
function GetUnitLocation( unit )
   --check input
   if unit == nil then
      print("Teleport: GetUnitLocation(): Unit is nil")
      return nil
   end

   return unit
end



--
-- Gets the location of the given vehicle
--
function GetVehicleLocation( vehicle )
   --check input
   if vehicle == nil then
      print("Teleport: GetVehicleLocation(): Vehicle is nil")
      return nil
   end

   return vehicle
end



--
-- Gets the location of the given region
--
function GetRegionLocation( region )
   --check input
   if region == nil then
      print("Teleport: GetRegionLocation(): Region is nil")
      return nil
   end

   return region
end



-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Functions which do the real teleporting --
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-- Teleports an entity to a matrix

--
-- Teleports the given entity to the given destination (matrix)
--
function TeleportEntityToMatrix( entity, destination )
   print("Teleport: TeleportEntityToMatrix() entity, dest: ", entity, destination )
   
   --check input
   if path == nil then
      print("Teleport: TeleportEntityToMatrix(): The entity is nil")
   elseif nodeNumber == nil then
      print("Teleport: TeleportEntityToMatrix(): The destination is nil")
   end

   SetEntityMatrix( entity, destination )
end

2) Put teleport.lua into data_XXX\Common\scripts
3) Add "teleport" to the script section of data_XXX\Common\mission.req

Opened ZeroEdit:
4) Added a region: region0
5) Set the region's Type/Class Properties to region0
6) Save

Open data_XXX\Common\scripts\XXX\XXXg_eli.lua
7) Add ScriptCB_DoFile("teleport") as the first line
8) Before function ScriptPostLoad's end add:
Code: Select all
--A single teleport
local region = GetRegionLocation( "region0" )
local destination = GetPathNodeDestination( "eli_cp4_spawn", 0 )
SetupEnterRegionTeleport( region, destination, 0, 0 )
ActivateRegion( "region0" )


9) Save
10) Munge Common
11) Load the game
12) Run the map
13) Enter region0 and get teleported to CP4

2008 Most Original Avatar
User avatar
Posts: 3101
Joined: Sun Aug 20, 2006 5:55 pm
Location: X-Fire: caleb1117 ಠ_ಠ
Projects :: No Mod project currently.
xbox live or psn: No gamertag set

Re: Teleporting

Postby Caleb1117 » Sun Sep 09, 2007 9:13 pm

Awsome, it works.
Mostly. :?
my node is underground, and it teleports me to its exact location above ground.
The start region is underground but not very deep, can the teleport penetrate the ground?

Gametoast Staff
User avatar
Posts: 2973
Joined: Sun Feb 26, 2006 7:36 am
Location: USA
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set

Re: Teleporting

Postby [RDH]Zerted » Sun Sep 09, 2007 10:30 pm

Caleb1117 wrote:can the teleport penetrate the ground?
I guess not. You could put a hole in the ground right above the spot or you could use the kill/respawn method (but thats mean to anyone with an award or limited unit count class).

2008 Most Original Avatar
User avatar
Posts: 3101
Joined: Sun Aug 20, 2006 5:55 pm
Location: X-Fire: caleb1117 ಠ_ಠ
Projects :: No Mod project currently.
xbox live or psn: No gamertag set

Re: Teleporting

Postby Caleb1117 » Mon Sep 10, 2007 8:29 am

[RDH]Zerted wrote:
Caleb1117 wrote:can the teleport penetrate the ground?
I guess not. You could put a hole in the ground right above the spot or you could use the kill/respawn method (but thats mean to anyone with an award or limited unit count class).


A hole, good idea!
All I need is to find a sutable terrain cutter.
Thanks.

Jedi Admin (Formally Penguin)
User avatar
Posts: 2537
Joined: Sun Mar 05, 2006 12:00 am
Location: 127.0.0.1
Projects :: Operation Katana [SWBF 2 Map Series]
Games I'm Playing :: Minecraft GMod CSS

Re: Teleporting

Postby Kateana » Mon Sep 10, 2007 8:48 am

Not sure what the differences are between the old/new code, but with the old one I could teleport anyway (ground, objects, etc)

2008 Most Original Avatar
User avatar
Posts: 3101
Joined: Sun Aug 20, 2006 5:55 pm
Location: X-Fire: caleb1117 ಠ_ಠ
Projects :: No Mod project currently.
xbox live or psn: No gamertag set

Re: Teleporting

Postby Caleb1117 » Tue Sep 11, 2007 12:29 pm

It seems to have lacked simplicity. :?
That, or I'm not good with lua script... Which I'm not. :runaway:

I think this should be added to the FAQ.

General
User avatar
Posts: 791
Joined: Sat Jul 16, 2005 3:23 pm

Re: Teleporting

Postby t551 » Tue Sep 18, 2007 7:05 am

What Zerted did was to slap an idiot-proof API on top of your code, Penguin.

Edit1: Well, not your code, but a function similar to yours.

Gametoast Staff
User avatar
Posts: 2973
Joined: Sun Feb 26, 2006 7:36 am
Location: USA
Projects :: Bos Wars AI - a RTS game
Games I'm Playing :: SWBF2 and Bos Wars
xbox live or psn: No gamertag set

Re: Teleporting

Postby [RDH]Zerted » Tue Sep 18, 2007 8:18 pm

t551t551 wrote:...slap an idiot-proof API on top of your code...

Yes, thats basically what I was going for. I hate writing idiot-proof documentation...

Teleporting seems to work best when the distances are far apart. If you are teleporting a player to another player and they are both standing near the same wall/force shield (was tested it with the space hangers) the player won't make it through. However when the outside player is hanging in the middle of space somewhere, the teleport works.

Another thing to note is that if you teleport to a player and that player isn't level (player could be falling, rolling, or in a rotated fighter), then your unit is teleported to that same rotation. You can end up on your side, floating in the middle of space. You cannot get back to the normal rotation until you touch ground or respawn.

Previous

Return to SWBF2 Modding

Who is online

Users browsing this forum: No registered users and 1 guest