playMaker

Author Topic: Playmaker Non-Repeatable Random Generated Objects  (Read 9711 times)

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Playmaker Non-Repeatable Random Generated Objects
« on: May 17, 2015, 12:52:07 PM »


Hey, guys!

I'm basically trying to Create a bunch of Randomly selected Objects (from the project folder) at Random Spawn Points using Playmaker. I've managed that in the following manner:

 - A Game Object is targeted as a Spawn Points (each Spawn Point spawns one prefab object)

 - Select Random Game Object picks one from a prefab project folder and Spawns it.

The problem however is that the objects repeat and sometimes one prefab is spawned twice or more times and I would like each of them to have just one copy generated in the scene when I start the game. I've tried logic with setting boolean values on spawn or int add, but I can't get it right. Maybe I should prepare the prefabs themselves before they are spawned? I'm not sure. I'm new to Unity and Playmaker. Any ideas would be helpful. Thanks! :)

flora

  • Playmaker Newbie
  • *
  • Posts: 47
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #1 on: May 18, 2015, 06:48:58 AM »

hello :)

i am new to this too, but i think you need to put the booleans on the prefab objects :

=>Select Random Game Object
=>check the boolean of the selected gameobject :
     if false : create object => set boolean to true => loop back to begining
     if true  : loop back to Select Random Game Object

i have the feeling that the right way of doing this involves arrays, but i have no competences there yet :)


HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #2 on: May 22, 2015, 03:52:09 PM »
Hey, thanks for the quick response, flora. :)

You've suggested that I should first check if the boolean is true and then, depending on the result create the object or not. But there's a problem with this logic. See, the way I pick the object and the spawn point is through a Manager that's in the scene. Each prefab in the project folder has a Set Bool Value action. The bool of each prefab is set to false initially.
The prefab spawns, and only then, when that happens it sets its own boolean variable to True. Therefore, the prefab must be spawned first in order for it's bool variable to be checked by the Manager. Or atleast that'a the only way I can do it... :)
So I think your suggestion is pretty good and it might be just the right way to do it, but in this state I can't utilize it....maybe I should set up the bool variables in another manner? Thank you for your time. :)
« Last Edit: May 22, 2015, 04:00:31 PM by HyperExc »

Tedthebug

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #3 on: May 22, 2015, 09:33:50 PM »
Can you have the prefabs in an array & loop through it assigning each to a random spawn point.  Just check for collisions on spawn so they don't spawn in the same spots. Or does the number of items exceed the number of spawn points?

If that's the case you could randomly spawn the objects & assign their array position # into a new list & check that the number doesn't exist in the new list before it is spawned on screen?

Just guessing here, hopefully something might help.

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #4 on: May 23, 2015, 02:30:56 AM »
Thank you for your time, Tedthebug.

The number of spawn points matches the number of prefabs.
Also, I was wondering how not to repeat the spawn point as well, and the collision test might be just the trick. :)

I am not familiar with arrays in Playmaker yet, but I'll look more into it. The only information I've found so far involves the use of another plug-in, called ArrayMaker.
Is there any way to setup arrays via Playmaker without additional plug-ins and coding? If not, is ArrayMaker the best choice for such type of situations as this one?
Thanks. :)

And here I thought this will be an easy algorithm to set up. :D
« Last Edit: May 23, 2015, 02:37:37 AM by HyperExc »

Tedthebug

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #5 on: May 23, 2015, 03:13:42 AM »
No worries.  I've not used arraymaker (I'm still new to playmaker) but I did a game for a course of study (using c#) with spawn points exceeding the number of items I had. I used a bool & a while loop to spawn the items (every square in the unity play area had a spawn point on it that could have an object spawned on it up to the number required.  Not all spawnpoints had an item spawned into it).  Of those objects (trees in my case) some could then be assigned an answer to a maths problem but the answers had to be a minimum distance apart so that they could be read & could only be calculated (canCalc was true) after the correct number of trees were in the gamespace.

I had an empty gameobject that had all the spawn points attached to it.  The below script was attached to the empty gameobject & all the spawn points were assigned in the inspector to the list. 

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TreeSpawn : MonoBehaviour
{

      public List<Transform>spawnSpots = new List<Transform> ();

   
      // set the number of trees on screen & list of trees
      public GameObject treePrefab;
      

      // Use this for initialization
      void Start ()
      {
            

      }
   
      // Update is called once per frame
      void Update ()
      {
            // manage spawning of trees

            if (TreeScript.trees.Count < Manager.instance.treeNum) {
                  // find a random spawn spot
         int spawnPos;
         bool spotFree;
         do {
            spotFree = true;
            spawnPos = Random.Range (0, spawnSpots.Count - 1);
            // check for trees on top of each other
            foreach (TreeScript t in TreeScript.trees) {
               if(t.transform.position == spawnSpots [spawnPos].position)      
                  spotFree = false;
            }
         }
         while(spotFree==false);


         // instantiate the tree in this spot
                  Instantiate (treePrefab, spawnSpots [spawnPos].position, spawnSpots [spawnPos].rotation);
                  
            }
            else if (TreeScript.trees.Count == Manager.instance.treeNum) {
                  Manager.instance.canCalc = true;

            }
      }
}


Hopefully something like that might prevent items spawning on the same spawnpoint,  If you looped through each item, assigning it to a random spawnpoint like above while checking that the bool for being a free spot was true it might work.  You could maybe do it for each object in the object list find a random spot, check it is free & if it is instantiate the object.

Sorry, clutching at straws here.  Hope it sort of makes sense & wasn't to long winded.
« Last Edit: May 23, 2015, 03:56:08 AM by Tedthebug »

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #6 on: May 26, 2015, 10:42:17 AM »
Hey, thanks, man.

I haven't had much time, but will have another go with it soon and I'll post the solution whenever I get it right. Cheers! :)

flora

  • Playmaker Newbie
  • *
  • Posts: 47
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #7 on: May 26, 2015, 09:38:57 PM »

hey  :)

here is another idea :
create a INT global variable for each gameobjects, then :

=>Select Random Game Object
=>check the global variable of the selected gameobject :
     if=0 => create object => global variable=global variable+1 => loop back to begining
     if=1 => loop back to Select Random Game Object

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #8 on: May 27, 2015, 02:51:51 AM »
Hey, flora.

Thanks again. I will try this idea as well.
I just have to break free from work to get on with it,  arghhh. >:(   :D

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #9 on: May 28, 2015, 05:12:25 AM »
Actually I've found that probably it would be just easier to go through the prefab objects in an order and assign them random positions, check if that position is already taken and if not, spawn the object there and do the same for the next, until all prefabs are spawned. This works for me, because I need to spawn all prefabs every time, I just need them to be in different positions each time. :)
« Last Edit: May 28, 2015, 03:22:43 PM by HyperExc »

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #10 on: May 28, 2015, 03:33:58 PM »
Okay, so I got them to spawn in different points by adding colliders on the prefab object and the spawn point object. When a prefab spawns on a certain point it sets off a trigger which flips a Boolean variable on the spawn point object which is later checked before spawning the second object.

If true => pick another random spawn point => loop back until False
If False => spawn prefab

Sounds right to me. The problem this time is that the checking of the bool variable is not working properly and still sometimes spawns two or more objects on top of each other. The reason I think for this is that the Action Bool Test doesn't check all the variables, but only the first and sends an event. I would like it to check all the bools first and then to fire off the event, but I cannot set it up. I cannot find a proper action for this... I've posted a screenshot for the FSM to show what I mean. :(

Tedthebug

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #11 on: May 29, 2015, 07:50:15 AM »
Can you add the loop so that it's something like

For each spawnpoint where the bool is false, instantiate an object & set the bool to true?

I'm hoping to try this on the weekend if the kids give me some peace & quiet. If I can get it working I will post what I did.

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #12 on: May 29, 2015, 09:11:34 AM »
Well, that's what I'm trying to do, Tedthebug. :)
But the more I look at it, the more it seems obvious to me, now that I have a better understanding of the situation, that I should definitely use arrays as you have first suggested. Even if I manage to do it the way I'm trying right now, I have a feeling, that it's going to be very consumptive memory wise. So I'll go at the problem, using arrays this time, and if I have a solution, I'll post it right way. :)

And again, thank you for your time! You've helped me tremendously! :)
« Last Edit: May 29, 2015, 09:13:27 AM by HyperExc »

Tedthebug

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #13 on: May 29, 2015, 06:37:22 PM »
Good luck. Remember to set the list or array onto an empty game object that references each spawn point & object. I think that will make life easier.

HyperExc

  • Playmaker Newbie
  • *
  • Posts: 21
Re: Playmaker Non-Repeatable Random Generated Objects
« Reply #14 on: June 03, 2015, 03:03:03 AM »
Hey, guys! I've got it to work!! I'll post the solution with screenshots and everything soon. Cheers! :)