Load a new tilemap and set the collisionobject shape to the new tilemap? (SOLVED)

Hey everyone, I was wondering. Does anyone know how to properly unload/load a tilemap and allow proper physics collisions with the newly loaded tilemap? I have seen that I can use tilemap.set_tile in order to change tiles within the map over to a new tilemap but I don’t see any way to change the collision shape to match the new tilemap that’s been loaded. My plan is to create many tilemaps which can eventually be selected at random while the player explores a zone. As I won’t know how many of these there will be I’d like to re-use a single collection and create logic for handling all the interactions but simply change the tilemap and reposition the player to the new tilemap after clearing all the previous entities manually each time. Is there a convenient way to do this sort of design? Thanks if anyone has any insights here! :slight_smile:

EDIT: I’m thinking if I could change the level “Path” to a new URL and then the Collision Shape to match that URL somehow this would be the behavior I’m looking for. It doesn’t seem like there’s a way to change this though but it would be ideal since then I could have generic functions which look at “#level” for instance and handle the logic interdependent of the tilemap URL that is being used.

Yes, this is a relatively easy task. Use collection factories or object factories that spawn your new tilemaps. For example:

In this case, I am looking inside my “overworld” collection, which contains the game world. This game world consists of a “camera” object, a “player” object, and a “overworld” object. The overworld object contains two object factories: “cider_path” and “piglet_hamlet”. The cider_path object that is spawned from its factory contains the cider_path tilemap and level data, while the piglet_hamlet object that is spawned from its factory contains the piglet_hamlet tilemap and level data.

Since the player is outside these factories, its lifespan continues across switching tilemaps. Therefore, I am able to change its position as I “warp” between tilemaps. To warp between tilemaps, simple delete the object spawned by your factory and spawn a new object from a different factory.

I have seen that I can use tilemap.set_tile in order to change tiles within the map over to a new tilemap but I don’t see any way to change the collision shape to match the new tilemap that’s been loaded.

This sounds like an extremely inefficient method. Setting tiles is not a super fast operation, and this method would not allow you to change the collision shape. If you spawn in a new tilemap with a factory, its collision shape is different depending on the tilemap spawned, meaning you do not have to do any manual changes.

Ah thank you, that makes sense. I’ll give it a shot and report back with the results :slight_smile:

Hmm, I tried it and I am able now to load the collection factory constructed tilemap into the main collection. However, when I try to spawn entities from the loaded level I get: “ERROR:SCRIPT: /main/main.script:64: function called can only access instances within the same collection.” Why is this? I suppose I can write some map spawner logic and then add the same logic file to each collection factory but it seems redundant. I.e. if I have 32 different floor layouts as tile maps which can spawn randomly, I would then need 32 collections each with a copy of the spawner logic inside them to access the tilemap data? I wish I could simply swap the tilemap source and collision object shape within the logic. Then I would only need a folder with all the tilemaps I plan to use and could load them into the main game collection at will…

Cool, I think I got it working although I still have some bloat concerns going forward (as in my previous reply.) At least now, I am able to load each tilemap and collision object data into my main game collection and the player instance is preserved across loads.

You can use .lua modules to have access to global (world) data.
That way, you can have control over you data “higher up” in the scene hierarchy.

Curious, did you use a “collection factory” or a “collection proxy” to load the new tilemaps?..

A collection factory

1 Like