x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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?

Product Version: UE 4.9
Tags:
more ▼

asked Nov 18 '15 at 08:12 PM in Blueprint Scripting

avatar image

nmm131
170 47 49 60

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered Nov 18 '15 at 08:22 PM

avatar image

Slayemin
824 50 29 73

avatar image nmm131 Nov 18 '15 at 10:38 PM

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?

avatar image Slayemin Nov 18 '15 at 10:43 PM

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).

avatar image nmm131 Nov 18 '15 at 11:05 PM

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

alt text

untitled.png (158.5 kB)
avatar image Slayemin Nov 18 '15 at 11:12 PM

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.

avatar image nmm131 Nov 19 '15 at 12:31 AM

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 alt text

asdf.png (43.9 kB)
avatar image Slayemin Nov 19 '15 at 12:39 AM

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.

avatar image Slayemin Nov 19 '15 at 12:43 AM

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.

avatar image nmm131 Nov 19 '15 at 01:04 AM

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?

avatar image Slayemin Nov 19 '15 at 01:08 AM

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.

avatar image nmm131 Nov 19 '15 at 01:22 AM

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

avatar image nmm131 Nov 19 '15 at 01:23 AM

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?

avatar image Slayemin Nov 19 '15 at 01:28 AM

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.

avatar image nmm131 Nov 21 '15 at 10:05 PM

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?

avatar image nmm131 Nov 21 '15 at 10:12 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question