Gamestudio Links
Zorro Links
Newest Posts
loading historical data 1st time
by AndrewAMD. 04/14/23 12:54
Trade at bar open
by juanex. 04/13/23 19:43
Bug in Highpass2 filter
by rki. 04/13/23 09:54
Adding Limit Orders For IB
by scatters. 04/11/23 16:16
FisherN
by rki. 04/11/23 08:38
AUM Magazine
Latest Screens
SHADOW (2014)
DEAD TASTE
Tactics of World War I
Hecknex World
Who's Online Now
3 registered members (AndrewAMD, Grant, Neb), 908 guests, and 6 spiders.
Key: Admin, Global Mod, Mod
Newest Members
rki, FranzIII, indonesiae, The_Judge, storrealba
18919 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
random strings from file_str_read #264796
05/08/09 08:15
05/08/09 08:15
Joined: Aug 2002
Posts: 164
Houston
Nicholas Offline OP
Member
Nicholas  Offline 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: Nicholas] #264799
05/08/09 08:37
05/08/09 08:37
Joined: Jul 2008
Posts: 553
Singapore
delinkx Offline
User
delinkx  Offline
User

Joined: Jul 2008
Posts: 553
Singapore
ok. i guess u want to do something like displaying a quote or line randomly everytime u load the game or similar. There are 2 approaches of doing this:

1. If u are going to use the file contents more than once inside one run of the game, then u can read the file to an array of strings. Then use the random function - random(var x) - to access the strings randomly. u can delete and add at runtime also.

2. but the above method is memory consuming if u not gonna use the lines again and again within one run. so everytime u want a random line u need to read randomly to the file. u can do this:

- file_seek() function to go randomly in the file to read.

another trick around is have the contents of your file like this:

01,This is my first line text.
02,This is my second line text.
03,This is my third line text.
...
...

then u use the file_find() function along with a random() function to generate a random number. then find that number and ur pointer points at that particular line.


A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
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 Offline OP
Member
Nicholas  Offline 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 Offline
User
delinkx  Offline
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.

Code:
//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.

Code:
typedef struct
{
  STRING * mystring;
}mystruct;

mystruct mySetOfStrings[22]; //defines 22 items
....
...
...


to access use
mySetOfStrings[0].mystring
mySetOfStrings[1].mystring
... etc.


A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
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 Offline OP
Member
Nicholas  Offline 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. frown
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 Offline
User
delinkx  Offline
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:

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:

Code:
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.


A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
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 Offline OP
Member
Nicholas  Offline 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: Nicholas] #265079
05/10/09 05:26
05/10/09 05:26
Joined: Jul 2008
Posts: 553
Singapore
delinkx Offline
User
delinkx  Offline
User

Joined: Jul 2008
Posts: 553
Singapore
this should be ok i guess. its a simple trick around but works perfectly. all the best smile


A7 commercial Team / VC++ 2008 Pro
homepage | twitter | facebook
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
O
Ottawa Offline
User
Ottawa  Offline
User
O

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. smile

Ottawa


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1