Easiest way to randomize an array in script?
by Ean Huddleston · in Torque Game Engine · 01/08/2007 (5:36 pm) · 3 replies
I have an array that contains the simple text names of 12 different .dts objects. This array is then accessed in my game in a sequential manner, so that these 12 objects are presented to a player in the order they appear in the array. But, using script, I'd like to be able to randomize the positions of these objects within the array during the middle of a game (to change the order the objects are presented to the player). From what I've read, it doesn't look like Torque script has this functionality. So can somebody recommend the easiest way to do this? I think I need to create a randomizing function in C++ that I will pass the array to using script, but am not clear on the specifics of how I will access that C++ function from script.
Thanks!
Thanks!
#2
an example:
01/08/2007 (5:54 pm)
I dunno if it's rigourously uniformly random, but it's pretty close:// assuming that %numElements is the number of elements in the array,
// and that %array is the array you want randomized.
for (%n = 0; %n < %numElements; %n++)
{
%src = getRandom(0, %numElements - 1);
%tmp = %array[%src];
%array[%src] = %array[%n];
%array[%n] = %tmp;
}an example:
function randomFun()
{
%numElements = 30;
// set up some test data
for (%n = 0; %n < %numElements; %n++)
%array[%n] = %n;
// print the array
%text = "";
for (%n = 0; %n < %numElements; %n++)
{
%text = %text SPC %array[%n];
}
echo(%text);
// randomize the array
for (%n = 0; %n < %numElements; %n++)
{
%src = getRandom(0, %numElements - 1);
%tmp = %array[%src];
%array[%src] = %array[%n];
%array[%n] = %tmp;
}
// print the array
%text = "";
for (%n = 0; %n < %numElements; %n++)
{
%text = %text SPC %array[%n];
}
echo(%text);
}
#3
01/09/2007 (9:09 am)
Thanks much for both your suggestions!
Employee Michael Perry
ZombieShortbus
You can implement that resource, then add your own random function to the new array object.