How to find the biggest number in the variable with many instances in anoher actor

I have this variable named speed that i referenced from another actor with Get All Actors Of Class
and get(copy) and i need to find the biggest number of all instances of it from the level and send it
to Request Turn.
And if 2 or more instances happen to have the same highest number then randomly choose one of
them.
All instances have their own default speed values.
Any idea how to make the blueprint(it is the one in the first image)?


Hey @primaldragon100!

You can achieve this by following the steps below:

  1. Get a For Each Loop node and plug the Out Actors pin of the Get All Actors of Class into it’s Array pin
  2. Create an int type array and store the current element’s speed (which you can get from the Array Element pin) inside the array using the Set Array Elem node. And leave the Index pin of the node as 0 (also check the Size to Fit option)
  3. Compare the first element of our array (which you can get using the Get node and leaving the index as 0) with the second one using a “>” (is greater than) and a Branch node. Make sure to plug the first element into the A pin (the upper one) of the “>” node and the second into the B pin (the lower one)
  4. After the True pin of the Branch node, use the Set Array Elem node again to set the second element of our array to the current element’s speed (or the first element of our array). So you’ll need to set the Index pin of the node to 1 this time as the one single difference from our first use case of this node.
  5. After the False pin of the Branch node, check if the first and second elements of our array are equal using an “==” (is equal to) and a Branch node.
  6. After the True pin of our second Branch node, plug our array into a Shuffle node. Then set the second element of our array to the first element of our array the same way we did in step #4.

Before all of these, store the first ever element’s speed inside our array as the second element, and only then start triggering this logic for the next loops. There’re a bunch of ways to do that, you can see one of the ways to do it from my latest post under this topic.

And here’s an image of the whole thing that I was able to make after launching my PC:

Hope this helps! :innocent:

at the third step when you say Get node i assume you mean the one that says Get ( a ref ),
also am i supposed to get the second element by changing the index as 1 in the Get node
after i copy paste the node?

I meant the copy one :blush:

Yep! Just avoid getting an element that doesn’t exist the way I explained after listing the steps.

up to the fourth step do you mean like this?(btw speedArray is the int array i made


)

Yep :star_struck:

Also add a For Each Loop node at the start, right after the Get All Actors of Class node. And use it’s array element pin to get the current element’s speed.

made the fifth step too but i’m not sure about how to set the for each loop, do i just
plug speedArray at the array node or do i need to do anything else too?

Here:

And then:

like this?

Don’t remember how exactly it should look like, sometimes it does add nodes in between automatically, shouldn’t be a problem though.

I’m not on PC right now so can’t check myself, but try and see how it goes!

here’s step 6 but i’m not sure what you mean after it

Lemme launch my PC real quick. I dunno why I didn’t do it from the start, it’ll be a lot more efficient this way. I’ll edit this post of mine in a minute.

Alright! It indeed looked a little off. So here’s how it should look like @primaldragon100:

Once the loop is over, the second element of our array (which you can get the way I did after the Completed pin) will be the largest speed value among them all, and will be chosen randomly if two are equal like you wanned.

I didn’t include that extra part yet btw, I’ll also add that in a minute.

Aight so you can add this at the beginning to prevent any errors related to access attempts to non-existing array elements:

Btw the explanation of extra part after listing all of the steps had a small mistake, so I corrected that in my initial post. But all of the numbered steps were correct. Also added an image of the whole thing to my initial post.

Hope this helps :innocent:

okay thanks for checking, so while you check the thing after step 6, could you also tell me
how to plug Request Turn instead of print string at the end of the loop so that the correct
Unitbase instance(the one with the highest speed) enters it

Double click on your function, inside, click on the entry or the return node, navigate to the Details window on the right side while having it selected, and add a new parameter under the Inputs section like this :blush:

Screenshot 2025-01-17 221250

when you say function do you mean Request Turn or Begin Battle(the function with the event graph
in the images)?
And entry or return node? Do you mean the white with black inside triangles on the Request Turn?

The one you wanna use the result in, so the former.

These guys:

is this right?

Yep :star_struck:

Also drag out of the Array Element pin of the For Each Loop node, release it on an empty space, and type the variable’s name of the actor (“Speed” in your case)

this? and do i plug something at the target node of Request Turn?

You should do it like this:

You should use your variable that you dragged out of the array elements pin of the for each loop node for the other parts of the code. Check out the image I provided in my previous posts.

Depends. If it’s a function of the actor that you’re working on the Event Graph of, no you should leave it as Self.