Game Development Community

Using and updating a random list?

by Isaac Barbosa · in Torque Game Builder · 03/16/2009 (12:39 pm) · 6 replies

Sorry, don´t know how to title this thread properly, but I want to do this:

Let´s say I want to pick an item ten times calling with intervals:
itemPicker.schedule(10,pickAnItem);
itemPicker.schedule(20,pickAnItem);
itemPicker.schedule(30,pickAnItem);
itemPicker.schedule(40,pickAnItem);
itemPicker.schedule(50,pickAnItem);
itemPicker.schedule(60,pickAnItem);
itemPicker.schedule(70,pickAnItem);
itemPicker.schedule(80,pickAnItem);
itemPicker.schedule(90,pickAnItem);
itemPicker.schedule(100,pickAnItem);

function itemPicker::pickAnItem(%this)
{
   %selection = getRandom(9);
   //I know I can use this to pick a random item from a list, but this way an item will be duplicated if random gets a same number several times, and I want to have those 10 items non repetable but picked in different order:
   //I was thinking in something that obviously doiesnt exists:
   %selection = getRandom(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
   //The point is that if the first time 1 is "grabbed" with the getRandom, next time I call the function, if for example, 3 was picked, then next call is something like this:
   %selection = getRandom(0, 1, 2, 4, 5, 6, 7, 8, 9); //where 3 doesn´t exist since it was already picked
}

I hope this makes any sense since I´m lost right now


#1
03/16/2009 (12:56 pm)
The schedule function must be used with a power of 2. Not 10-100.

2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, etc. Are all valid timings.

Also, the getRandom function is suppose to be used like this:

getRandom(0, 9);

It will automatically go through 0-9 without any more input. It does not work the other way.

Lastly, you will want to make the itemPicker a global scope. So replace the one at the schedule with:

$itemPicker
#2
03/16/2009 (12:59 pm)
Thanks Tyler for the clarification. So it seems I should try another approach.

If someone else have suggestions all are welcomed :)

#3
03/16/2009 (1:23 pm)
Tyler,

You gave an idea. So I´m using another approach that will works. It will take some hours to be implemented but will work.

The other think I want to ask: why schedule must be used with a power of two? I never read on the docs or tutorial and in fact they uses timings as 1000 and 500. Will that affect the performance of a game?

Thanks
#4
03/16/2009 (2:07 pm)
Well from what I read from TDN, you need to use a power of 2. Yet I have not yet tried using 10 or 20, and I haven't been on TDN for a bit. So you could try it.

The computer always reads a power of 2. Which is why when you buy RAM, you must buy it in increments of 2, 4, 8, or 16.

I have also learned from previous knowledge that using a power of 2 can save you alot of computer usage. After I turned all my sprites into 128x128 pixels, my game took much less graphic power then it use to.
#5
03/16/2009 (2:24 pm)
You don't have to use power of two. If you use 100, the event will be executed after 100ms, or the closest tick.
#6
03/16/2009 (2:44 pm)
> Well from what I read from TDN, you need to use a power of 2.

tyler, could you point out where that is ? if it's in TDN it should be corrected.