Random Names

Posted By: Impaler

Random Names - 04/04/07 00:32

hi everyone,
I am making a first person shooter set in WWII in Normandie, and I want to be able to have a big library of names for soldiers, so that messages can come up like "You killed <name>" and that. The only problem is, I want to give random names to soldiers without having to script:

my.skill1 = int(random(100))
if(my.skill1 == 1)
{my.name = <something>;}
if(my.skill1 == 2)
{my.name = <something else>;}


and so on until 100. That would be really long and ugly code. is there an easier way to do this?
Posted By: xXxGuitar511

Re: Random Names - 04/04/07 01:33

You could load them from a random line in a text file, but you'd still be doing the same thing.
Posted By: ISG

Re: Random Names - 04/04/07 04:35

You could randomize a number, and that random number be == to a variable temp.

EX:
random(100); //get number
random == temprand; //something that works like this..

Then after it's set to a variable you can then open up a text document (like Guitar said) and have it go to the line that the random number said, so something like this:

Go To Line Number #TempRand //Not in code form but you can interpret it somehow

Then it can pick up that name randomly and simple...all you have to do mainly is place all the names in a text. If I had more time and wasn't so tired, or in a rush to get to sleep before waking up for work, I would do a sample for you.

Sorry if I wasn't much help.
Posted By: Xarthor

Re: Random Names - 04/04/07 10:40

Well I would read the whole text file into a string array at game start, so you don't have to read from the file during the game.
Code:

var names_max = 100;
string name_file = <my_names.txt>;

text names { strings=100; }

function names_store
{
var i;
var fhandle;

fhandle = file_open_read(name_file);
if(fhandle)
{
i = 0;
while(i < names_max)
{
file_str_read(fhandle,names[i]);
i += 1;
}
file_close(fhandle);
}
}

//Now in the game:
var randtemp;
...
randtemp = random(names_max);
str_cpy(my.string1,names[randtemp]);
//Now my.string1 contains a random name from the list


Posted By: Impaler

Re: Random Names - 04/05/07 06:46

Thanks for those ideas! Storing the names in an array at the start of the game sounds like the best option
© 2024 lite-C Forums