How do I filter an array, or loop over it?

Before proceeding, please use the forum search feature at the top of the page to check if your question has already been answered.

How do I filter an array, or loop over it?

I’m calling a function in an extension (MQTT::GetLastMessage()), which returns an array of structures. I save the returned data to a scene variable. (I can change to a global variable, if needed).

The length of the returned array varies a bit. I’d like to process the array in ways that are not clear to me yet (I’m in an exploratory phase), but likely, I want to filter the array, so that only items with a certain value of a child node are passed through. The items that pass through the filter will later move objects on the screen.

How can I iterate over the array?
How can the array be filtered?

What is the expected result

Get the data for the wanted tagId, and use it in the “game”. Note that the structures in the array are populated to varying extent. Hopefully not a problem.

What is the actual result

I don’t know how to do this.

Related screenshots

Here is some data of the kind that I want to process. In this case, there are 3 items in the array, and I want to filter on tagId.

[
{"version":"2.0","tagId":"0","timestamp":1708079383.3820605,"success":true,"data":{"coordinates":{"x":2482,"y":5170,"z":0},"tagData":{"blinkIndex":315802507},"metrics":{"latency":20,"rates":{"success":4,"update":4,"packetLoss":0}},"zones":[]}},
{"version":"2.0","tagId":"3","timestamp":1708079383.366351,"success":true,"data":{"coordinates":{"x":3328,"y":1176,"z":0},"tagData":{"blinkIndex":3102149090},"metrics":{"latency":20,"rates":{"success":4,"update":4,"packetLoss":0}},"zones":[]}},
{"version":"2.0","tagId":"26425","timestamp":1708079383.302753,"success":true,"data":{"coordinates":{"x":5107,"y":10554,"z":850},"tagData":{"blinkIndex":361798,"eulerAngles":{"x":36.3125,"y":5.625,"z":-55.6875}},"anchorData":[{"anchorId":"10762","rss":-90},{"anchorId":"11607","rss":-84},{"anchorId":"14402","rss":-88},{"anchorId":"36290","rss":-84},{"anchorId":"50542","rss":-89}],"metrics":{"latency":18,"rates":{"success":4.95,"update":5.38,"packetLoss":0.02}},"zones":[]}}
]

Project files (optional)

Insert a minimal game showing your issue in a .zip or .rar.

Thanks for the reply.

Where can I find “For every child in …”?
I don’t fully understand if it should go into the conditions or the action column, and what terms I should use in the search field for conditions or actions.

https://wiki.gdevelop.io/gdevelop5/events/foreach-child-variable/

image

1 Like