Teleporting Player Inside Buildings

Discuss modding questions and implementation details.
Post Reply
User avatar
Sluggy
Posts: 20
Joined: Tue Aug 23, 2022 9:28 pm

Teleporting Player Inside Buildings

Post by Sluggy »

Hey all,

I've been working on a mod for DFU that requires being able to teleport the player. I've only been working with the codebase for a few days so I'm still trying to wrap my head around all of the different spaces in the game and how it manages relative positioning and re-centering of origins when moving around. As a result, I'm finding a bug in a very specific situation that is causing me no end of grief.

I need to be able to reliably teleport a player to a specific interior building (in this case, a tavern) in such a way that the interior space is mapped correctly relative to the exterior terrain. Teleporting to exterior locations is no big deal and teleporting to interiors that are within the same DFLocation as the player also works just fine. But when I attempt to teleport the player inside of a building in another DFLocation, it appears that the building is spawned around the current location of the player even though the exterior terrain will revert to the origin of the space - this can lead to disastrous situations where the player exits the tavern only find themselves plummeting from a great height above the tavern or worse yet, underground!

The information I currently have cached in order to teleport the player are the map pixel location and a copy of the StaticDoor that was used to enter the building. I've attempted to teleport the player using a variety of techniques including PlayerEnterExit.Respawn as well as StreamingWorld.TeleportToCoordinates + PlayerEnterExit.StartBuildingInterior and am at a loss for what I can do to fix this issue.

User avatar
Magicono43
Posts: 1141
Joined: Tue Nov 06, 2018 7:06 am

Re: Teleporting Player Inside Buildings

Post by Magicono43 »

So the quest action script for "TeleportPc.cs" might be helpful in its update method.

Also the mod I made "Random Starting Dungeon" does something very similar if you want to look through the code in that (it's a bit messy just fyi), https://github.com/magicono43/DFU-Mod_R ... ng-Dungeon

Unfortunately I have no successfully done any examples where the PC is teleported instantly into an interior that is not a dungeon though. I'm sure there is a way, there is just no great examples that currently exist as far as I have seen. But unloading the place you are teleporting from and loading the place you are going to is most likely what would be required first more than anything I'd imagine. Hopefully some of that helps.
Last edited by Magicono43 on Tue Aug 23, 2022 11:00 pm, edited 1 time in total.

User avatar
Sluggy
Posts: 20
Joined: Tue Aug 23, 2022 9:28 pm

Re: Teleporting Player Inside Buildings

Post by Sluggy »

Hi and thanks for the response! I have in fact perused that teleport script many times while searching for keywords throughout the project. I've also looked through the source code for that very mod you provided. Btw, thanks for that! It was the very first thing I looked at when I started this mod and was immensely helpful! For that very reason I'll be sure to share my own source code in all of my mod projects for DFU.

I think I've narrowed the issue down. When I call PlayerEnterExit.StartBuilding interior I pass the StaticDoor data that was previous cached. This data is the key. Internally it checks to see if the door has a parent and if so moves that door to the origin of its parent. If the door has no parent it simply retains its default values. This is important because the interior is spawned relative to this door. In my case the door's position data was stored from a previous session and reloaded by the mod. This means that it still has the old position from the previous session thus the building ends up being spawned high up in the air. I think if I can find a way to also cache the exact building or at least its position whenever I cached that door, I should be able to restore its original position correctly. I'll keep you posted.

User avatar
Magicono43
Posts: 1141
Joined: Tue Nov 06, 2018 7:06 am

Re: Teleporting Player Inside Buildings

Post by Magicono43 »

Glad to hear some of my code was useful (even though alot of it was another persons code as well originally, lol.) Also glad to hear that you have intentions to share your code in the future as well, for me at least practical examples are my greatest personal asset when trying to make mod features, so it's appreciated.

But yeah, the interior loading stuff was quite perplexing to me, but if you can figure out a way to make it work somewhat consistently that would be great!

User avatar
Sluggy
Posts: 20
Joined: Tue Aug 23, 2022 9:28 pm

Re: Teleporting Player Inside Buildings

Post by Sluggy »

So after smashing my head into a brick wall for several hours I ended up revisiting the Teleport magic effect and found exactly what I needed. It seems the answer was right in there all along as it specifically has code that handles the y-position offset issue caused by shifting reference frames. Essentially all I needed to do was cache a copy of the PlayerPositionData_v1 from the player instead of the StaticDoor they had entered. A little copy-paste and refactoring now lets me easily respawn the player inside of the tavern.

And with that I'm very nearly complete with my death alternative corpse drop/respawn mod! It still needs some cleanup and a few minor features like proper support for localization but all of the main features are complete. I'll probably put it through its paces for a couple of weeks of playing with it activated just to be sure but I think I managed to handle all of the weird edge cases I could list off of the top of my head as well as some I came across during playtesting.

Thanks for reminding me about the teleport code! I can't believe I overlooked it so many times! I might not have even considered checking it again had you not mentioned it.

User avatar
Magicono43
Posts: 1141
Joined: Tue Nov 06, 2018 7:06 am

Re: Teleporting Player Inside Buildings

Post by Magicono43 »

That's awesome you were able to figure it out. It seems recently I've been doing the head into brick-wall part, but pretty rarely getting to the solution in the end unfortunately. Good to know that atleast in your case the teleport spell code was enough to resolve the issue you were having, definitely something to note for anyone doing something similar in the future.

Oh yeah, looking forward to seeing your mod go public, sounds cool.

Post Reply