Find Array Element by its Index

Soo every now and then i notice that the only way to get a quick return of an Element of an Array would be the Array function “find” /** (Not counting foreach loops as quick even tho it can be very quick executed) */ which takes the Object as a parameter, but why isn’t there a function where it takes the index as parameter and returns the Element of the Array that sits at that position? Most of the time i know what the index of the desired Element is but need the object (reference) itself.

E.g.:
Lets say i have a Grid (X=11; Y=11) which holds Tiles (11*11= 121 Tiles Total), in an Array, that can spawn a Room, when a player enters a Unknown Room another Room gets Spawned on every adjacent side of the Entered Room.
So through a Colision trigger i get the Room with the corresponding Tile, with this Tile i get the index position of it and can determine where in a grid the other tile index are, so i know where they are but i would have to run a ForEachLoop through an array 3 to 4 times even tho i know their position in the array…

*(the 4 index are → CenterTileIndex + 1, CenterTileIndex - 1, CenterTileIndex - X and CenterTileIndex + X (X stands for the Grid axis size) one index we wouldn’t need because the Player came from that direction but we would still need to check if we don’t pass the where the Player came from Room through) *

That’s a unnecessary complexity of O(4n) // (4*121 = 484)

instead of 1 and with a nested loop it could easily be O(n²) // (121² = 14641)

So why again isn’t there a the functionality find an Element by its Index? You already have the RemoveAt which removes an Element at an Index by passing it an idenx… so why not a simple FindAt node?

Have a nice day and keep the amazing work up.

Isn’t this just a “GET” node? It does exactly that, it takes your array and you tell it what index to retrieve and it retrieves whatever element is located at that index.

2 Likes

The Get node should do what you want. Here is a list of all array nodes and what they can/can’t do.

Ohhh ■■■■ your right, it’s displayed as “Get (a copy)” i must have over looked it all the time since i probebly thought it only make a copy of the array instead of giving the reference to the array.

Thanks alot, and here i was thinking all the time they havn’t thought of that.

Thanks alot, seems like i looked at the wrong places, its even shown at WorkingWithArrays which was the one i flick through, mayby i shouldn’t try to see if they have documented it at 3 AM

I hit this same problem. This is really a bug, the labelling of the functionality is so poor, and no alternatives (including the string ‘index’ would be a nice start) are offered in the BP context menu.

Usability bugs are still bugs…

1 Like

How is this a bug? Also when you say you hit the ‘same problem’ what do you mean because the OP’s problem was solved by the GET node.

I just wanna to add some comments regarding that “Get (a copy)”:
Something you have to pay good attention is that it is literally getting a COPY! It will not leave some negative impacts regarding some general usage. However, if the array you are getting is real-time, and it changes with its parent or child, then you MUST AVIOD using this node. Since getting a COPY of the array would limit you with the only access with the array itself. Otherwise, you will get system warnings that you do not have any access to its related roots/blueprints/parents/children.

A typical example that I have just encountered was that: I was using AI Perception system to get an array of actors that were operated by my AI_Controller. However, if you use that Get (a copy) node and then attempt to access to the controller of the pawn (e.g., by using the node “Get Owner”, or anything of that nature) you will receive a system error. Which preventing you from doing that.
If such problem happens, you may use a For Each Loop node to directly access elements in the array without making a copy of it.

I hope that might help!

3 Likes

I just wanna to add some comments regarding that “Get (a copy)”:
Something you have to pay good attention is that it is literally getting a COPY! It will not leave some negative impacts regarding some general usage. However, if the array you are getting is real-time, and it changes with its parent or child, then you MUST AVIOD using this node. Since getting a COPY of the array would limit you with the only access with the array itself. Otherwise, you will get system warnings that you do not have any access to its related roots/blueprints/parents/children.

A typical example that I have just encountered was that: I was using AI Perception system to get an array of actors that were operated by my AI_Controller. However, if you use that Get (a copy) node and then attempt to access to the controller of the pawn (e.g., by using the node “Get Owner”, or anything of that nature) you will receive a system error. Which preventing you from doing that.
If such problem happens, you may use a For Each Loop node to directly access elements in the array without making a copy of it.

I hope that might help!