Download

How to get an actor class from an array of actors?

Hello everyone , just a small problem. I have to get a value from the array to the left (which contains a list of Actor Class) and give it to the function to the right.

How can I do that please?

Don’t you just drag from the get on the left onto the purple class pin on the right?

but first I have to select an item from the array, even randomly but I don’t know how to properly do that, can I ask you how to do it please?

@KhappaMJ

Use Random Integer to get an index.

wonderful :slight_smile: many thanks

@KhappaMJ

Don’t copy/paste this code everywhere as it will break things. ​
The code above works because Random-Integer uses max-1.
However, arrays generally are 0 bounded, i.e. start from zero.
So, overall its better to use 'Last-Index’ everywhere instead.:wink:

Per docs “Returns a uniformly distributed random number between 0 and Max - 1”

Array (0, 1, 2, 3, 4) returns a length of 5
Random Integer (max 5) would choose a random from 0-4

Not sure where the problems is?

The code works. What I’m hinting at is this. Will it train the OP consciously / subconsciously to use Array.Length elsewhere next time (ignoring the quirk that Random-Integer expects max-1)? After all, most of the time Array.Length is never used on its own. So I’m just making the case that maybe its unwise. Whereas LastIndex is safer than using Length or Length-1 (the latter especially dangerous if Array.Length is ever 0). But no worries dude, it just depends on what the OP takes away from this. Overall, it isn’t really about code, more about people…:wink:

Gotcha :wink:

I worked with databases (oracle, mysql etc) and arrays daily for work so it’s kind of a given to me. numrows() / length() in my field are constantly used before update/insert/get similarly to is valid. There should be no assumption that an array or any other variable has a value. Yet if you know by design that there will always be n values in an array, then x approach is fine. Otherwise you code for variance.

Using last index on an empty array. Coder has to learn, failure teaches.

IF(!empty(n)) { do this }else{ can’t I’m empty }

Yep, there’s no escape from IsValidIndex / Length check anyway if the array is empty. I wonder if it’d ever make sense for Epic to add an impure version of Array.Get with a Bool? Similar to how LineTrace works, with a ReturnValue bool to stop the code evaluating the Out-Hit struct if there’s no hit…

I don’t think there’s a real need for it. Coders need to simply learn and apply proper if conditional logic. Can I … jump, prone, crouch, sprint, shoot, reload, equip, drop, add, interact etcetera and so forth?

The volume of “is valid” based questions we get here and answer hub is astounding. says quite a bit.