How do I move a player between collections

How do I move a player between collections with all changes in the player intact? Like if I child a game object to the player in runtime, or if the player’s level has increased. I was thinking of creating an instance of the player once a collection has been loaded, but the player is going to be restarted to its original version with level as 1. How do I do this?

Collections don’t actually exist in your game. They’re just a grouping tool used to create collections of objects in the editor. When created (e.g through collectionfactory.create) they just create the hierarchy of game objects as you’ve specified it.

So if by move a player between collections you mean changing an object’s parent, then just use go.set_parent.

The rest of your question is quite confusing though :slightly_smiling_face:

2 Likes

Sorry for the confusion, I should have explained the question in more detail. I’m new to Lua and Defold and hope to learn about this. In my original question, I was hoping to know if I can just take a player that I made in a collection, to the next location. For example, I want to move to the next location so I just take the player game object and put in in the next collection. This was because the player can change in a location. So that this change doesn’t disappear, I will just take the player and move it to the next collection. For details, my collections are loaded using collection proxies and a script to control what collection is shown and examples of changes to the player include increase in level and obtaining a shield (shield childed to player). However, I don’t know if this could be done in Defold and thought of a different way:

  1. If there was a factory in each collection to produce a player when a collection is shown, whenever the player moves to different locations, it would seem that the player is taken and moved to the next location, when instead, a player go instance is just being produced.
  2. If there was a global variable to record changes such as level, the player go that are instantiated can just refer to this as their level.

However, if a shield was to be childed to a player instance, and the player is to move to a different location, the player’s shield will disappear. I don’t know how to modify the prototype to be produced by the factory so that every player that is instantiated now has a shield so my solution doesn’t seem to work. I’m wondering if there is a way my solution could be finished, or if there was a different way I could have handled this problem.

You might to have a look at collection proxies. Those are perfect to load the next level.
In your collection you will have your player and the collection proxy loading the level.

1 Like

Try using modules. I think you referenced this in “2”.

Store the global variables in a module and have the player load them when they spawn in the collection.

For example,

-- items.lua
local M = {}

M.shield = false

return M
-- assuming you're using a folder named "modules" and
-- items.lua is in that folder
local items = "modules.items"

local function add_shield(self)
   -- update the armor, add a sprite, etc.
end

function init(self)
-- this will refer to the module to see if the player 
-- has a shield when they load into the collection
   self.shield = items.shield or false

   if self.shield then
      add_shield(self)
   end
end

function on_message(self, message_id, message, sender)
    -- message that updates the module
    if message_id == hash("add_shield") then
       items.shield = true
       add_shield(self)
    end
end

You can also load up the properties from the factory instead of messaging the factory object. Both ways work. You would need to create go.properties if you’re using the factory.create to update the properties. These are all things explained in the manuals and API.

2 Likes

Thank you very much for the reply. The code is going to help me a lot and I should have read the manual first. It seems that I was also confused with my own question and thought about it carefully. What I really wanted was for the player to not disappear when changing what collection is loaded. Can this be achieved by taking the player game object out of all the collections and putting it in the bootsrap collection where it could not disappear? So that when a collection is loaded, the collection is there but the player is also already there? and when it is unloaded, the player will not disappear as it is not in the collection loaded. Will this work?

Everything in the collection disappears when the collection is unloaded. As far as I know, you can’t “transfer” items between collections — but you can message between them, no problem.

Alternatively, you can have the player in its own collection and load/unload the levels. This to me seems too complicated and will likely cause more problems down the road.

1 Like