I try to setup a multiplayer game, where each player has his own UI (health bar etc). Creating said UI when a player joins and positioning is no problem, however, I'm struggling to find a solution on how to identify which health bar belongs to which player.
My first idea was to write the UIDs of each UI element into certain columns of an array, so I could get those UIDs and therefor the correct element if I need it. However, this seems a little bit messy/complicated for me (I started using arrays and understand the basics, I find it just hard to handle them, as I do not have a visual representation like a table) and I wonder if there are better solutions.
My "perfect" solution, which doesn't seem to be possible with C2, would be to group UI elements of a player together. When a player joins and his UI is created, I add every UI element that belongs to him to the group PlayerUI1 for example. If I now want to do something with the healthbar of player 1, I would access it by something like PlayerUI1.Child("Healthbar") or an event like "Pick Child of Group".
As I said, the example solution doesn't seem to be supported by C2. I also thought about using families or containers, but both are not providing the events/actions I need.
Does anybody have an idea on how to solve that problem without using an array? Thanks in advance!
Depending on exactly how many elements you intend to store, can you not just use instance variables associated with the instances of each player sprite object? That's certainly how I maintain player stats such as health, score, money etc.
More complicated structures, such as per-player inventories get a bit more difficult because you can't use an array/dictionary as an instance variable. Instead I create separate array objects for each player, or alternatively serialise the inventory contents into a text string that can be unpacked with tokenat()
Why cant you use containers?
All you have to do to pick any object associated with a player is... pick the player, and reference that object in actions.
You can place arrays in containers btw.
You cant use containers in free version of construct.
Develop games in your browser. Powerful, performant & highly capable.
Another simple way (although not the cleanest) is to give a variable to the UI element and the player when its created eg: "owner"
Then do a "pick all health bars"
if: "owner = player" then
It will pick all of the health bars (probably 4 max depending on your game) and then only subtract health from the one that matches the player that got hit.
I don't think its much a performance hit.
Its kinda like the UID method.