Reading random liines from a file
#1

So I have an idea, I want to setup a command in such a way that it would read random lines from a file and display them to the server through use of client messages, or whatever I choose. Thing is, I don't know how to actually going about getting the lines from the file, help
Reply
#2

Why don't you just make an array within your script and make them read it right from the script? It'd be much easier.
Reply
#3

Quote:
Originally Posted by _Xerxes_
Why don't you just make an array within your script and make them read it right from the script? It'd be much easier.
Yes true, but I also would like the capability to edit the lines in-game, you know .. is this possible?
Reply
#4

For the main code he doesn't need to search, he just needs a random generator, which is easy.

I assume you know how to create files and define cells as strings. For our purposes let's suppose the file is called "LineFile". Define the number of lines in LineFile at the top of your script.

Код:
new MAX_LINES = 20; // example
Then make a command (or function, whatever) that generates a random string from that file:

Код:
if(strcmp(cmd, "/tellme", true) == 0) 
{
  new RandomLine = random(MAX_LINES);  // picks a random number within the file's range
  format(string,sizeof(string),"%s",LineFile[RandomLine][0]); // formats string from the file
  SendClientMessage(playerid,COLOR_WHITE,string);   // gives randomized message to player
}
Now every time you type that command you will get a different string. Editing the lines ingame will easy too.
Reply
#5

Thanks for the info. For now I just slapped an array in there .. will get jiggy with files tomorrow :P
Reply
#6

Quote:
Originally Posted by [ ŠǾǖŦĦЗŁΛẄ
~ [ HellFire ] ]
Quote:
Originally Posted by » ραωпsтαг «
will get jiggy with files tomorrow :P
rofl!


k, tomorrow is here .. I'll give it a blast later .. when I'm awake D:
Reply
#7

Have a search for "dtel", I use files to store all my teleport data but I use a buffer in 'Init()' to read all the data from the file (sscanf) and store it in the buffer. I use add/remove commands also so all the examples you need are in that script really and I don't mind if you use the code aslong as you learn from it and don't just c&p it.

If you want to read the number of lines in a file then run a loop over the array (message buffer) and count how many valid strings are present in it, no point reading a file again as you can do that or you could just use a global:

pawn Код:
//top of script
new
  gFileLines;

//where you read into the buffer
while ( fread( f, string ) )
{
  .......
  gFileLines ++;
}
Then the random like the guy above posted but with some security:

pawn Код:
if ( gFileLines > 0 )
{
  new
    r = random( gFileLines );

  while ( isnull( gMessages[ r ] )
  {
    r = random( gFileLines );
  }

  format( string, sizeof( string ), "%s", gMessages[ r ] );
  .......
You could use better security I'm sure but that's a quick example.
Reply
#8

Hmm, random text strings are my favorite but I still have a lot to learn.

Donny, when you say security do you mean how the script checks to make sure there is a string formatted on the line first, and if there isn't one it generates another random number?

I have a question, what if none of the lines have strings? Will the script execute endlessly?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)