Spawn Actor from Index

How can I add different actors to an index, for which in a for loop it will randomly select one of the actors and spawn it using spawnActorFromClass?

You want to create an array of actor classes and then populate that array with the actor types you’d be interested in spawning. Then, you use the “Random Int In Range” node to get a random number between 0 and the array length and use that as an indexer into your array. Use the ‘get’ node on the array to pull out a class template and feed that into your “SpawnActor” node.

How can I populate the actor types into the array of actor classes if I do not have access to a reference of the actors, as they will be spawned after startup, but are not present on the map?

You’re creating an array of class types (they’re purple in the editor). These are not references (blue). Within the “Spawn Actor” node, it’s looking for a class template to use for spawning an actor. When you’ve created an array of these class types, you can pre-populate it with the class types of the allowable actors you’d like to randomly spawn. The “Spawn Actor” node will create an actor instance based off of this class template, and the instance of that class is a ‘reference’ (blue).

So I’d be typing in the class name into each variable, then hook up the Set to random int in range?

Your approach works, but it’s too much work for me. Just click on the array, look for the defaults section, then there’s a “+” sign. Click on that, then you’ll get an array item with a drop down. From that drop down, select the appropriate class type you’d like to spawn. Do this for each class type you’d like to spawn.

The random integer is an array index value. Usually in blueprints, you’d just use it as a look up index into an array. In blueprints, you really can’t set an array element to be at a particular index (though you can in C++). This shouldn’t really matter though, you’re only trying to access a random element from the array, so you’d use the “Get” node and feed it the index value.

As for your error… I can’t see what you’re doing so I can’t diagnose it. Please attach a screenshot.

The random integer is an array index value. Usually in blueprints, you’d just use it as a look up index into an array. In blueprints, you really can’t set an array element to be at a particular index (though you can in C++). This shouldn’t really matter though, you’re only trying to access a random element from the array, so you’d use the “Get” node and feed it the index value.

As for your error… I can’t see what you’re doing so I can’t diagnose it. Please attach a screenshot.

Ah very cool. How can I get my Random Int in Range to work with the array SET module? Int cannot go to Array of Actor Class

I also get the error, when plugging in GET of array to spawn actor, that Array Actor Class is not compatible with Actor Class

For my Array, I am using Object Types/Actor → Class

67340-asdf.png

http://www.wobblyduckstudios.com/Images/UE4/asdf.jpg

It only spawns a cube, which is not part of any class that I have listed. Is there a reason for why none of my blueprint classes are being spawned?

Hey, you’re spawning something! That’s progress, right?! congrats!

Now you just need to figure out why you’re spawning cubes instead of the actors you think you should be spawning. Might I recommend using the “Print String” node to print off the class template name the moment before it gets spawned? Maybe add a breakpoint on the spawning node and see what kinds of input values its getting? Verify that the actor classes in your array are the values they should be?

I can help a bit more with a screenshot of your blueprint and its data values.

Using PrintString the int being displayed is randomly changing. However, my class is still null, only a cube is being spawned.

Using PrintString the int being displayed is randomly changing. However, my class is still null, only a cube is being spawned. Could it be because of my blueprint actor classes, having more than one actor, it is not functioning?

I’m more interested in printing out the value of the array value, not the array index. Make sure that the array value is not null, and that you have a list of items in the array.

Whether you have sub actors or not in your blueprint shouldn’t matter as far as spawning is concerned.

It still is not working and there is no null value. Is there any way you can show me a blueprint of what you are describing?

I got it to work, there was something funky with my blueprints so I tested it with new ones and got it working. I’m sure it will now work with my pre-existing blueprints.