Can I have one collectionproxy to load different collections?

It seems not a very good approach to have collectionproxy for each game stage/room (making it to really stupid long list of collectionproxy for a bigger game). Can I change referenced collection through code, because I don’t see such option in manual?

No you can’t change which collection will be loaded at runtime.

Either you can do a “long list of collection proxies” or you can store your levels in a json format (or similar). Then you will load your json and setup the level from that.

1 Like

Thanks, for reply!
Is there an example somewhere how to do that with json?

You would build your levels in some kind of level editor (can be anything as long as you can export data from it), not within a defold collection.

Then on level load you would load the json file as a resource, parse the data and with code place your objects from factories. How the json is structured totally depends on what kind of game you are making.

This can require a lot more coding on your part (especially if you can’t use some kind of premade level editor and have to make one (think I have seen people use tiled before)).

1 Like

Sorry for the hijack, but I was wondering if there is a pros/cons list for using a bunch of collection proxies vs parsing JSON(besides the fact that JSON needs to be parsed manually)?

I think the classic answer of “It depends” can be used here too.

The only real pro I can think of for JSON, that applies in all cases, is that because you have control of the JSON format it can potentially take less space on your device, but collections are text so they will compressed well anyway.

The obvious con with JSON is that you need to parse it yourself and also set it up yourself.

The pro with collections are of course is that it is faster to setup. (But if you have a good level editor it isn’t necessarily faster to make levels in it.

But all in all it totally depends on what kind of game you are making, and how much control you need. I would say that the more control and intricate your levels are the better JSON probably becomes.

3 Likes

I wish I’d know how to start and do simple stuff with JSON.

JSON is just a format. You can read that into lua as a table and then read the data from that as you would any table. It isn’t hard, you can definitely do that :slight_smile:

I wrote a super quick and dirty example of level loading thing that you can check out.
JsonExample.zip (328.8 KB)

5 Likes

Thank you!!!
I know what is JSON but I don’t know how to translate all collections or objects into JSON and how to read from JSON.
Totally will check example when I get back at home, really thank you!!!

1 Like