playMaker

Author Topic: How do I get 5 game objects at random out of 10?[SOLVED]  (Read 4545 times)

Slater

  • Full Member
  • ***
  • Posts: 123
How do I get 5 game objects at random out of 10?[SOLVED]
« on: October 09, 2012, 10:10:25 AM »
Hi,

I have 10 game objects that I randomly want to activate into 5 objects and move into place. I've been looking at "select random game object", but that only gives me one object to save in a variable out of the 10. I need to get 5 out of the 10.

Thanks/
Freddy
« Last Edit: October 22, 2012, 03:23:55 AM by jeanfabre »

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: How do I get 5 game objects at random out of 10?
« Reply #1 on: October 09, 2012, 10:26:16 AM »
First thought: ArrayMaker by JeanFabre
Put all your objects into a list, then do the following 5 times (use an int compare and add 1 each turn until the compare says x>=5 )
1) Array List Count, subtract one, and save as "Index" (int)
2) random int : min : 0 , max : "Index"  ; and save that again as Index
3) Array List Get at index "Index" save to "Target" (gameObject)
4) Array List Remove At : "Index" ;
5) XXX
6) add int to X
7) compare X to 5
8) send event : "repeat" (or whatever)


Now whatever you want to do with that game object you can put in the same state at the bottom (where I put an XXX). Then use a send event to redo the state for the next game object. If your int x is higher or equal to 5 the loop is finished.

PS: If you need to run this multiple times you can create 2 array lists fill one and then use Array List Copy To to copy the content of the one to the other. Then you can work on the second "temporary" list.
« Last Edit: October 09, 2012, 10:29:43 AM by kiriri »
Best,
Sven

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How do I get 5 game objects at random out of 10?
« Reply #2 on: October 09, 2012, 10:55:54 AM »
Hi,

 yep, arrayMaker can be found here:
https://hutonggames.fogbugz.com/default.asp?W715

bye,

 Jean

Slater

  • Full Member
  • ***
  • Posts: 123
Re: How do I get 5 game objects at random out of 10?
« Reply #3 on: October 21, 2012, 08:30:13 AM »
Thank you for your help. Just got to the point that I needed to get this working and I got it working. What I do is that I create a list with materials. Then I have 6 gameobjects in the scene that gets a random material from that list/array.

I didn't use send event though, I use Int Switch instead. So "first round" it goes to a state01 where it sets the 01object material. Then add 1 int, finish and then "second round" it "ends" on state02 set material to 02object and add 1 int and then finish etc..until state06 where I let it stop, have no finish.

Slater

  • Full Member
  • ***
  • Posts: 123
Re: How do I get 5 game objects at random out of 10?[SOLVED]
« Reply #4 on: October 22, 2012, 08:32:52 AM »
I have come across a problem though.

If I look at one of the simpler randomised arrays I have created, it sometimes get stuck and doesn't work as intended. In the array I have set it to Prefill type: Material and Prefill count: 13. Live update is not active. I have filled 0-12 with materials. In my FSM it looks like this:
State 1: Array List Count, Use owner, Store: Globals/amountToys (I have it global so that I can run 2 copies with same materials but have them in 2 different game objects in scene)

State 2: Random Int, min 0 ,max Globals/amountToys, Store in Globals/amountToys. Inclusive Max active.

State 3: Array List Get. At Index Globals/amountToys. Data Storage: Get Material Data, pickedMaterial. (This is where the FSM stops when it fails.

State 4: Array List Remove At Globals/amountToys.

State 5: Set Material, Specific Game Object: 01item. Material: pickedMaterial

When I run the game it sometimes stop on State 3. When I look in the array it says Start Index 0, Max Rows 10. And it seams to remove rows in the array. What is going on with that?

Any ideas?

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: How do I get 5 game objects at random out of 10?[SOLVED]
« Reply #5 on: October 22, 2012, 09:00:17 AM »
for one, your array list count is one too high. You see, there may be 13 items in the list, but the highest index is 12. So your max index should be the number of items - 1... always :D
Did that fix it?
Best,
Sven

Slater

  • Full Member
  • ***
  • Posts: 123
Re: How do I get 5 game objects at random out of 10?[SOLVED]
« Reply #6 on: October 22, 2012, 09:03:22 AM »
Just got it sorted. If I don't have "Inclusive Max" it worked. Now all the small randomisers work that randomise the material. I have a bigger one that randomise the target object for the material. I had to add a 0.2 sec wait after each repeat in that one in order for it to play nice. Now it all works perfect!

Thanks all for the help. Now I have random material to random target object, yey!

Slater

  • Full Member
  • ***
  • Posts: 123
Re: How do I get 5 game objects at random out of 10?[SOLVED]
« Reply #7 on: February 24, 2013, 11:00:54 AM »
So I decided to update playmaker to latest version and also have latest Unity (3) and for some reason this array thing is giving me problems. I then also updated to the latest array maker, but still got the error.

The problem that I get is that the at index 0 (doing a randoize vector thing) is not picked up. It works when I play it on my computer(mac osx), but when I build it to Ipad, it doesn't work. So the first item in the list, at index 0 is not placed in the correct spot in the scene. It gets a 0, 0, 0 value. Any ideas to why this is happening?

The only workaround I can think of now is to have an empty object in at index 0 but that is dumb :)

Edit: I found a solution. I added a state before everything that just have a 1 sec wait in it. For some reason it was needed for it to play nice.
« Last Edit: February 24, 2013, 11:41:12 AM by Slater »