|
3 registered members (AndrewAMD, Grant, Neb),
908
guests, and 6
spiders. |
|
Key:
Admin,
Global Mod,
Mod
|
|
|
random strings from file_str_read
#264796
05/08/09 08:15
05/08/09 08:15
|
Joined: Aug 2002
Posts: 164 Houston
Nicholas
OP
Member
|
OP
Member
Joined: Aug 2002
Posts: 164
Houston
|
I have a txt file with multiple lines. I want to be able to hit a button and show a random line from that file. I have the file read, but how do I tell how many lines I have and how can I show a random one (then replace it after hitting the button again)?
please help. I'd like to also have it so it only shows each line once randomly, but I can probably get that far myself. I guess I also need to be able to delete a string from this file so I don't end up reading it again, this might be the easier way.
thanks
Last edited by Nicholas; 05/08/09 08:23.
Black holes are where God divided by zero.
|
|
|
Re: random strings from file_str_read
[Re: delinkx]
#264888
05/08/09 16:17
05/08/09 16:17
|
Joined: Aug 2002
Posts: 164 Houston
Nicholas
OP
Member
|
OP
Member
Joined: Aug 2002
Posts: 164
Houston
|
I don't mind using some memory, but I can't seem to setup a string array, I would normally try STRING* test_ary[22] = "#55"; but the [] keep giving me problems.
file_seek won't work with each line having a different amount of characters, the random number will have it start in the middle of a line.
file_find() would have worked well, but whenever I use a var in there it just jumps to the last string in the file. var rand; fhandle = file_open_read("list.txt"); file_str_read(fhandle,willkommen_str); rand = random(3); file_find(fhandle,rand); file_str_read(fhandle,willkommen_str);
it also seems to only work well when I have "02," with the comma in there.
I used to be better at programming, but I'm rusty. Thanks for the help. maybe we can still get it.
Black holes are where God divided by zero.
|
|
|
Re: random strings from file_str_read
[Re: Nicholas]
#264892
05/08/09 16:33
05/08/09 16:33
|
Joined: Jul 2008
Posts: 553 Singapore
delinkx
User
|
User
Joined: Jul 2008
Posts: 553
Singapore
|
for file find, do u have the number followed by a comma for all numbers ? Random() returns a float. so u need 2 convert it to integer. do remember to seed the random function so tat its really random. e.g.
//at the start of ur program
random_seed(0);
....
....
int rand;
rand = integer(random(3)); //gives random between 0 and 2
....
since we trimming it to an integer, possible values are 0, 1 and 2. __________________________ if u want to create array of strings, use a struct e.g.
typedef struct
{
STRING * mystring;
}mystruct;
mystruct mySetOfStrings[22]; //defines 22 items
....
...
...
to access use mySetOfStrings[0].mystring mySetOfStrings[1].mystring ... etc.
|
|
|
Re: random strings from file_str_read
[Re: delinkx]
#264957
05/09/09 01:32
05/09/09 01:32
|
Joined: Aug 2002
Posts: 164 Houston
Nicholas
OP
Member
|
OP
Member
Joined: Aug 2002
Posts: 164
Houston
|
I tried the file_find(rand) without using random, setting it as a predefined variable of 2 (var rand = 2;) and it still jumped right to the end.  I've used int(random(3)) with randomize() before, but that was the C-script version apparently. thanks for the update I tried the array, but ran into a problem. TEXT* willkommen_txt = { layer = 10; pos_x = 200; pos_y = 10; size_y = 100; font = arial_font; offset_y = 0; string (mySetOfStrings[0].mystring); flags = VISIBLE; } gives me a startup failure before even launching. I'm assuming this should work for loading the strings (file_str_read(fhandle,mySetOfStrings[0].mystring);), and this doesn't give me any errors, but how can I display it on the screen if my text won't recognize this as a string? I tried str_cpy(willkommen_str, mySetOfStrings[0].mystring); to copy the array to a regular string to be able to show it on the TEXT* but the engine crashes without any errors when using this str_cpy. It looks like I'm getting closer... yay
Black holes are where God divided by zero.
|
|
|
Re: random strings from file_str_read
[Re: Nicholas]
#265009
05/09/09 14:57
05/09/09 14:57
|
Joined: Jul 2008
Posts: 553 Singapore
delinkx
User
|
User
Joined: Jul 2008
Posts: 553
Singapore
|
i really donno y struct of STRING* doesnt work. though a struct of ENTITY* works. I tried searching around but no solution. Someone gave a solution of defining an array using a malloc function to create an array of strings. but later manipulations doesnt give flexibility. so i went back to the random access using file_find() function. here is a tested and running code:
STRING* temp_string = "";
var filehandle;
var rnd_num;
STRING* str_rnd_num = "";
TEXT* welcome_txt =
{
pos_x = 100;
pos_y = 100;
string (temp_string);
flags = CENTER_X | TRANSLUCENT | SHOW;
}
void main()
{
//randomize
random_seed(0);
//random from 0 to 7
rnd_num = integer(random(8));
//convert the random number to a string
str_for_num(str_rnd_num, rnd_num);
//open file
filehandle = file_open_read("myfile.txt");
//find that line
file_find(filehandle, str_rnd_num);
//skip pointer to the next element
file_str_read(filehandle, temp_string);
//read it
file_str_read(filehandle, temp_string);
file_close(filehandle);
}
And myfile.txt contents is as follows:
0,this is zero line
1,this is my first line
2,this is my second line
3,this is my three line
4,this is my four line
5,this is my five line
6,this is my six line
7,this is my seven line
hope this helps.
|
|
|
Re: random strings from file_str_read
[Re: delinkx]
#265078
05/10/09 04:27
05/10/09 04:27
|
Joined: Aug 2002
Posts: 164 Houston
Nicholas
OP
Member
|
OP
Member
Joined: Aug 2002
Posts: 164
Houston
|
thanks, I didn't think of getting the random number and just reading the next string.... seems so simple now. Just to let you know, the string* function didn't work withou: #include <acknex.h> #include <default.c>
and the random_seed didn't do anything either, it was seen as a problem.
I also had to change the line to rnd_num = integer(random(8))+1; (the +1) otherwise I ended up with a blank string sometimes. other than that, it works fine.
to make sure I don't get any repeats I was thinking of using a var array that I could use in a game save
var ary[40];
ary[rnd_num]=1; // this way if a random number is used the array saves it and I can use and if statement loop to see if the array already has that number set to one
any other ideas how to do it, or would this work well
(thanks for all the help)
Black holes are where God divided by zero.
|
|
|
Re: random strings from file_str_read
[Re: delinkx]
#265180
05/11/09 01:05
05/11/09 01:05
|
Joined: Apr 2006
Posts: 737 Ottawa, Canada
Ottawa
User
|
User
Joined: Apr 2006
Posts: 737
Ottawa, Canada
|
Hi Nicholas! I did the same thing, I needed 15 numbers in random order. The array started with all zeros The code found a random number then checked to see if it was in the list At the end I got a list of ramdom numbers and the game was set. Whenever I come back the process starts over and I get a new list. This works very well for me.  Ottawa
|
|
|
|