Picking specific objects without storing their UID

Get help using Construct 2

Post » Fri Feb 01, 2013 12:52 pm

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!
Posts: 10
Reputation: 822

Post » Fri Feb 01, 2013 1:21 pm

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()
Posts: 83
Reputation: 2,668

Post » Fri Feb 01, 2013 1:41 pm

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.
Image Image
Posts: 7,462
Reputation: 72,546

Post » Sat Feb 02, 2013 6:16 pm

You cant use containers in free version of construct.
Posts: 9
Reputation: 423

Post » Sat Feb 02, 2013 9:11 pm

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
subtract health

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.

Posts: 851
Reputation: 10,493

Return to How do I....?

Who is online

Users browsing this forum: purplemonkey and 22 guests