playMaker

Author Topic: Spawn Random Game Object[SOLVED]  (Read 1449 times)

rodeobravo

  • Playmaker Newbie
  • *
  • Posts: 2
Spawn Random Game Object[SOLVED]
« on: October 29, 2019, 09:11:32 AM »
Very new to Playmaker going through tutorials now, I figured this wouldn't be to hard, but I'm having trouble.

I just have 4 prefabs I would like to spawn 1 out of the 4 into the level. I was doing good spawning 1 with the "Create Object" action, but I can't figure out a way to spawn them from the pool of 4 options.

Any help would be amazing :) Thank you in advance :)
« Last Edit: October 31, 2019, 09:17:17 AM by djaydino »

ransomink

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Spawn Random Game Object
« Reply #1 on: October 29, 2019, 10:39:19 AM »
A quick and easy solution is to use the random event action: it will randomly select an event to transition to. Create an event for each prefab you want to spawn. Link each transition to a state that instantiates/spawns that prefab. Each time you enter the state containing the random event action, it will spawn the prefab linked to the random event chosen

As I mentioned, this is quick solution and is not the best or scalable solution. Will post that below...

ransomink

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Spawn Random Game Object
« Reply #2 on: October 29, 2019, 11:06:52 AM »
The best way to approach this is to place your prefabs in an array. Each prefab is an element of the array, and you access an element using its index. The index is the position of the element within the array. The first element in the array would be array[0], the second element is array[1]. As you've noticed, arrays and lists start with a 0 index, not 1. This is very important to remember.

First, place your prefabs in an array variable. In your state, use a Random Int action; this will select a random integer between a min and max value. The min value, is 0 (first index), and the max value should be the length of your array. Since you have 4 prefabs, the array length is 4. Store the result in a variable. This will be used to select a prefab from the array


TIP: Instead of inserting 4 for max value, create an array length variable and use the Array Length action before the Random Int action, to save the length of the array to the variable. Then use that variable for the max value


Next, use the Array Get action. Place your array variable and use the stored index variable as the index value. This will give you the prefab from that index and store it in the result. Now, use the result as the object to spawn on the Create Object action

This method is better because you don't have to create an event and separate Create Object action for each prefab. But, there is an even better solution with much less work...
« Last Edit: October 29, 2019, 11:12:06 AM by ransomink »

ransomink

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Spawn Random Game Object
« Reply #3 on: October 29, 2019, 11:10:38 AM »
Ok, so, this is really the best and easiest approach. Just create an array variable and place your prefabs inside. Within a state, use the Array Get Random action. This will store the value as a random element from your array. Then use Create Object to spawn it, and Bob's your uncle...
« Last Edit: October 29, 2019, 11:13:23 AM by ransomink »

rodeobravo

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Spawn Random Game Object
« Reply #4 on: October 29, 2019, 11:21:54 AM »
Oooh :D I figued the first one out, I think the quick option is the best for me, but I like both and I have noted them.

I'm not really doing much with it and I believe that will work great :) thank you!!