[SOLVED] -- Load and Unload collection proxies to avoid overlap

What i need is to load stage 2 after i complete stage 1…(i want stage 1 removes from screen when i completed it and then show stage 2)…this is how i exactly made in my project but whenever i tried to launch the game, i always complete stage 1 but it did not remove on screen and also didnt show stage 2…what do you think is the real problem?..did i missed something?..

  1. I put a GO in main.collection (named it “nextlevel”)…inside nextlevel GO i’d put a collection proxy with
    id – “proxy”… i’d set up it’s collection property to “level_2.collection” … and after that i also put a script file named “script.script”

  1. inside “script.script” i’d put the following code…

function on_message(self, message_id, message, sender)

if message_id == hash(“proxy_loaded”) then
– New world is loaded. Init and enable it.
msg.post(sender, “init”)
msg.post(sender, “enable”)
end

end

  1. I put the “level1.collection” inside “main.collection” so that whenever i launch the game, it will load the level_1.collection automatically…inside “level_1.collection”, i put a GO named “finishline” and inside of it i put a collision object (it’s actually no sprite…i dont put an image on it…i only use it as a portal to level_2 whenever the character collides on it)…and also, i put another script file on it (i named it “script.script” also BUT IT IS ANOTHER SCRIPT FILE and not the one i used in main.collection)…inside of script i put the following code below:

function init(self)

self.collected = false
msg.post("main:/nextlevel#proxy", "load")

end

function on_message(self, message_id, message, sender)

if self.collected == false and message_id == hash(“collision_response”) then
self.collected = true
end

end

that’s it…BUT whenever i launch the game,…ill play the level_1,…but after my character collides to the finishline GO(i know my character already collides to it coz it is not avoidable and in the properties i already set to kinematic, and i also set the group and mask properly)…the level_2 didn’t load on the game…i check the console and error log for any problem but its actually clear…no signs of error…is there anything that i’d missed?..if so, well can you tell me what it actually is?..thanks! :')

The “proxy_loaded” message is sent back to the script that sent the “load” message to the proxy. So you need to move the code from nextlevel script to finishline script.

@sicher oh!..yeah…i didn’t notice that one sir (where did my logic go?..haha),…thank you very much for your answer!..now it works fine,…except for one thing: stage 2 can now load but stage1 still didn’t unload…i will look for some solution by myself,…(i dont want to rely much on you,…i will not learn a lot by myself)…but when there’s no other choice,…please don’t be tired to answer my questions coz i learn a lot from you and appreciate it (i swear)…thanks! :’)

No problem. Just ask away if you need help!

@sicher sir,…im sorry to ask you again…but i have question that wanted to clear…here it is:

-As you can see now,…i removed the level1.collection in the root of main.collection. and i put it in the collection proxy named “level_1”…and also,…i add a script file named “start_level.script”…inside of the script i add this code:

function init(self)

msg.post("main:/nextlevel#level_1", "load")

end

function on_message(self, message_id, message, sender)

if message_id == hash(“proxy_loaded”) then
– New world is loaded. Init and enable it.
msg.post(sender, “init”)
msg.post(sender, “enable”)
end

end

actually, when i launch the game, level_1 loads perfectly,…BUT i cant control it (it didnt jump when i pressed space key)…is there anything wrong in my code?.or i didn’t put some important code on it?

1 Like

This is a bit tricky. Input has to be acquired by the game object that contains the proxy component in order for input to be directed into the loaded collection. So send an “acquire_input_focus” message from start_level.script.

Check out “Input and Collection proxies” at the end of the input manual here: http://www.defold.com/manuals/input/

1 Like

Thank you sir for giving me the solution…i actually fixed it right now,…i put the
msg.post(".", “acquire_input_focus”) in the function_init(self) and it works well…there’s also a plenty of problems that i had encountered last time but i actually fixed it by myself (and i actually cant believe it, thanks for all the solution and explanations that you gave to me, i think i’m improving little by little… ) my game runs in its right game flow…and…uhm…hehe…one question…remember when i said that i need to find a solution to remove/unload stage 1 whenever stage 2 show in the screen?..actually,…i didn’t find a right solution on this.,…so i just wanna ask you if
msg.post(“main:/nextlevel#level_1”, “unload”) is the right code that i need just to remove stage 1 on screen so that it will not overlap to stage 2?..if yes, where do i need to put it?..waiting for you response…thanks! :’)

  • I add a GO directly in the outline of level1.collection…i named it “fl” …inside of it,…i put a sprite, collisionobject (set to kinematic, group to finish and mask to hero)…i also create and put a script file named “controller1.script” on the GO…inside of the script, i add the following code:

function init(self)

self.collected = false

end

function on_message(self, message_id, message, sender)

if self.collected == false and message_id == hash("collision_response") then
self.collected = true
msg.post("#sprite", "disable")
msg.post("main:/nextlevel#level_2", "load")

elseif message_id == hash(“proxy_loaded”) then
– New world is loaded. Init and enable it.
msg.post(sender, “init”)
msg.post(sender, “enable”)

end

end
  • i did it coz i want my hero to loads and enable stage 2 whenever i managed to finish stage 1…but when i tried to launch the game, stage2 actually loads perfectly but stage 1 overlaps on it…
1 Like

Oh…i actually manage to fixed this problem…so here what i did…i use this code in a script file inside my collider GO:

function init(self)

self.collected = false

end

function on_message(self, message_id, message, sender)

if self.collected == false and message_id == hash("collision_response") then
self.collected = true
msg.post("#sprite", "disable")
msg.post("main:/nextlevel#level_2", "load")
elseif message_id == hash("proxy_loaded") then
-- New world is loaded. Init and enable it.
msg.post(sender, "init")
msg.post(sender, "enable")
-- unload the cureent stage to not overlap to the new stage
msg.post("main:/nextlevel#level_1", "unload")

--if engine sent back an unload message to the script, disable and unload the current stage to
--not overlap to the new loaded stage
elseif message_id == hash("proxy_unloaded") then
    -- let unloader know
    local level_1 = msg.url("main:/nextlevel#level_1")
    msg.post(level_1, "disable")
    msg.post(level_1, "final")
    msg.post(level_1, "unload")
	end

end

function final(self)

msg.post(".", "release_input_focus")

end

3 Likes