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"

Spawning pseudo-random BP objects from an array.

I've checked a lot of threads but can't find anything that addresses exactly what I want to do.

I want to create a spawn node which has a pseudo-random chance of spawning 1 of several different types of object.

I'm currently using a multigate which doesn't let me change the chance that something will spawn. So is there a way of setting it so that it has 3 objects. and one object has a 60% chance to spawn and the other 2 have 20% each Also I wanted to create an array of each type of object e.g sword, armour, food, material. so that it would first randomize the type of object it would spawn then choose which object of that type.

e.g: I want it to decide whether to spawn a plant, sword or armour.

if it chooses a plant then I want it to choose from an array of plants. which includes mushrooms, tree or algae.

...does that make sense...

Product Version: UE 4.14
more ▼

asked Dec 13 '16 at 05:52 PM in Blueprint Scripting

avatar image

19 4 3 5

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

2 answers: sort voted first

Well you have random integer and float nodes:

https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Random/RandomIntegerinRange/index.html https://docs.unrealengine.com/latest/INT/BlueprintAPI/Math/Random/RandomFloatinRange/index.html

And you also have stream version of those nodes which let you control seeding


From those you can apply randomness in many way, for starters you can randomize index of array and get random item for it (in random in node obviously you would plug size of array-1 in to max pin)

There switch node which let you control exec flow by variables and there also quite ot documented select node which let you direct specific varable depending on input varbale

It a lot more default to apply odds, you would need to do some math. Common practice in programing is to have random float between 0 and 1 and then apply math to it for needs of your application needs, for example you multiply it to 10 and it will be float between 0-10.

more ▼

answered Dec 13 '16 at 08:22 PM

avatar image

36.7k 932 169 1111

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

You could use data tables to achieve this. Data Tables will allow you to use a CSV file that can be loaded to memory during game play and iterated. While iterating the data table you can use the "Weight" CSV column to determine the "weight" or likelihood of the current row being selected.

The nice thing about this is being able to separate this type of configuration (which is normally tweaked for game balancing) from your code so in the future you only have to change a CSV file instead of code. This can reduce the amount of regression bugs.

Read more about data tables here https://docs.unrealengine.com/latest/INT/Gameplay/DataDriven/

Please note, I didn't actual run this code/blueprints so you may have to iron out kinks. It will get you moving in the right direction.

Step #1 - Create CSV Data Table

Create a CSV file that looks like this (note the header name of the first column should be a blank string, each row thereafter requires a unique value for the first column). UE4 engine uses this column as an "ID" to reference this row.

Also note the BlueprintGeneratedClass'PATH_TO_CLASS' is critical. When the Data Table is loaded from the CSV into the DataTable object, each DataTable row will be loaded into our Structure we create during the next step. This Structure will contain a "ActorClass" variable that is actually of type "Actor" class so we can use this value for the Spawn Actor From Class node.

alt text

Step #2 - Create Data Table and Data Table Row Structure

Create a Structure that represents each row of your data table (CSV column names and Structure variable names must match as well as CSV column value types and Structure variable value types).

Create a new DataTable and select your newly created Structure as the row Structure. Select your CSV file as your Data Table source.

Here's what your Data Table and Structure would look like.

alt text

Step #3 - Load the Data Table

Create a GameState replicated variable of type Array (an array of our Data Table row structure).

alt text

Load the Data Table into our new GameState replicated variable. This should be a performance improvement so we don't have to reload the data table over and over.

NOTE: I cropped out the class name, but this loading is in my GameState::BeginPlay event so it's only run during GameState's BeginPlay (hopefully once during map load). You shouldn't alter this array structure after the data table is loaded.

alt text

Step #4 - Provide a Public Method on the GameState to get a random row

This method can either return an instance to a newly spawned Actor, or you can return the SObjectType structure or the Actor class name. You choose.

(Ran out of available photo upload slots so you have to view the last image on Imgur)


Random Probability reference: https://forums.unrealengine.com/showthread.php?75982-Weighted-random-number&p=329872&viewfull=1#post329872

more ▼

answered Dec 13 '16 at 10:27 PM

avatar image

694 24 24 55

avatar image Demipro Dec 19 '16 at 10:06 PM

I'm using the first answer for the sake of simplicity. But I've made a note on how to do both because both of these answers are amazing and useful.

(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