Question regarding level selection in game

Hello All,

I am currently going through a platformer tutorial and have come across an issue with my code and cannot seem to find a solution. In this example, there is an end flag that once the player touches the flag then it should load to the next level. However, the message of the level name being sent is including brackets around the name. In this example, I am wanting to load level2, but the message being sent over is as [level2]. I don’t know how or where the brackets are being added to this message. Attached are screenshots for additional info:

Best,
James

The next level name is probably a hash which explains the brackets because that is what the string representation of a hash looks like.

You could instead use a msg.url() as the go.property() and use the url of the next collection proxy to load.

Thanks, looking at the tutorial video, it looks like the # in the property box where I put level2 isn’t there and in Defold 1.9.3 the # is now an icon in that box. So for msg.url() would I be able to include that inside the go.property or replace that altogether?

the level_to_load is likely a hash() and they’re not possible to string concatenate in any meaningful way.
We recently made this more obvious, as in some cases, it worked in debug, but not when users tried to release their game. Perhaps that video tutorial hasn’t recognized their mistake.

However, you might try replacing your code with:

level_to_load = msg.url(nil, level_to_load, nil)

That way it becomes a valid url which whould work better.

What would be the best way of going about this? It makes sense to have the msg.url() in the go.property. For the next part it would be fixing the load_level function I have to instead look for the msg.url() right?

You could actually keep the hash as Mathias suggested and use that to construct a URL to the proxy:

local function load_level(self,  level_to_load)
    if self.current_level ~= nil then
        msg.post(self.current_level, "unload")
    end

    level_to_load = msg.url(nil, nil, level_to_load)
    self.current_level = level_to_load
    msg.post(level_to_load, "load")
end

BTW: Please share code as text and not as images!

1 Like

This worked! Thank you again and will share code as text in the future!

1 Like

I had the same issue. I could see it was the hash triggering the issue in the end_flag but I wasn’t sure (at this point) how to resolve that in the controller script (or maybe the other way around). This solution worked for me too and makes sense! Thank-you!