Need help understanding Collections [Game Maker Studio 2 Background]

Hey guys!

I’m picking up Defold to learn more lua and develop ZenRPG alongside it’s Game Maker/Unity versions.
But I need help understanding collections… From what I can gather from the documentation they’re prefabs/Unity or objects/game maker.

So in Game Maker I have a Player object so I’d have a player collection then, I also have a party object however this is ONLY for the game setup so this would actually then become our active party collection which handles: Adding, Removing, Swapping (Think JRPG “Formations” where you can swap the positions of a party member. So for Example I have a party of Guy1, Guy2, Guy3 I then swap Guy1 with Guy3 so Guy1s index was 0 it is now 2 (I assume arrays/dictionaries start at 0) and then Guy3 becomes index0 and also our Party Leader).

What I want to know is, how do I talk between collections? IE calling scripts, check if our game.collection with quest object with add Guy2 is finished then add guy2 to our party.collections?

In GM it’s easy, in Party just call script(argument) where argument will be the index in the party database but not the active party and if no party guy2 will have a index of 0 in the party as he’d be the first.

In Unity this is somewhat the same.

1 Like

In Defold, instead of calling functions on scripts, you send messages. Here is the message passing manual. It’s really not too different.

Instead of something like this:

-- FYI I have no idea how GM actually works...
script.addPlayerToParty(party1, guy3)

You might do this:

msg.post("party1#script", "addPlayer", { guy = guy3 })

The nice thing is: if you know the hierarchy you can write out the URL and pass a message to and from any scripts or objects without having to pass references around or do typecasting or anything. (With a dynamic hierarchy nothing much changes).

The annoying this is: messages don’t get received instantly/synchronously. The rest of your function will run, and then the message will arrive. A lot of the time this doesn’t matter too much, but for some things it’s a pain. In the painful cases, you can generally move your code into a lua module, which you can synchronously call functions on.

[Edit] P.S. In Lua, array-style tables start with index 1…

4 Likes

as @ross.grams said, in defold we use message passing.

in my experience, gamemaker script is like lua function, you can write functions in *.lua file(lua script), and call like normal programming: func_name(arguments).

you can do this. but, in defold, I think they recommand using messages.

It really depends on the use case. Message passing is perfect for notifying another part of your game about a state change, like a score update, an item pickup, a navigation request or similar thing that is infrequent and asynchronous. It should not be used for things that happen very frequently and where you need an immediate response or result.