Game Development Community

Pick a random object from simset

by Isaac Barbosa · in Torque Game Builder · 07/08/2008 (11:10 am) · 4 replies

Hello,

I've tried this unsuccsesfully. I want to pick up a single object from my simset but when echoing the picked object, I get multiple echoes.

I'm using this code:

%pickanobjectfromSimSet = getRandom($GameObject.getCount()-1);
echo(%pickanobjectfromSimSet SPC "que numero vamos a elegir");
%objeto1 = $GameObject.getObject(%pickanobjectfromSimSet);

so I really don't know if this is the better/only way to pick up an object from a simset. I'm confused right now and i don't kow why I'm getting so many echoes if I just want to have one object position echoed.

Thanks for the help

#1
07/08/2008 (11:23 am)
function SimSet::getRandom( %this )
{
   %idx = getRandom( 0, %this.getCount() - 1 );
   return %this.getObject( %idx );
}

function testGetRandom()
{
  %set = new Simset();
  %set.add( new ScriptObject(Obj1) );
  %set.add( new ScriptObject(Obj2) );
  %set.add( new ScriptObject(Obj3) );
  %obj = %set.getRandom();
  echo( "Got object" SPC %obj.getName() );
  %obj = %set.getRandom();
  echo( "Got object" SPC %obj.getName() );
  %obj = %set.getRandom();
  echo( "Got object" SPC %obj.getName() );
  %obj = %set.getRandom();
  echo( "Got object" SPC %obj.getName() );
}
#2
07/08/2008 (11:37 am)
@James,

Thanks for the code, but it doesn't works for me since I'm adding my objects using this code when a row of objects is created every time. So, at beggining of game the $GameObject SimSet has only 7 objects and every 10 seconds it has 14 objects since a new line was created and son on... then 21, then 28...

$GameObject.add(%objecto);

maybe that can works but I don't know how to implement it right now.

Thanks
#3
07/08/2008 (11:43 am)
Solved. That 0, at the begginning of the code did the trick

Thyanks
#4
07/08/2008 (11:51 am)
Yeah I think if you call getRandom and pass it only 1 paramater, it generates a float from 0.0 to the number passed. Calling SimSet::getObject and passing a float would probably not be good.