Raised This Month: $ Target: $400
 0% 

Randomly read from a text file?


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
yuppicide
Member
Join Date: Dec 2013
Old 10-18-2014 , 02:09   Randomly read from a text file?
Reply With Quote #1

I coded a simple plugin and tested it works on my server.

Could someone show me how to randomly read a line from a text file and PrintToChatAll?

Let's say I have the text file textfile.txt

line1
line2
line3
line4

It would randomly show one of those 4.
yuppicide is offline
splewis
Veteran Member
Join Date: Feb 2014
Location: United States
Old 10-18-2014 , 03:39   Re: Randomly read from a text file?
Reply With Quote #2

Something like this:


Code:
new Handle:lines = CreateArray(1024);
new Handle:file = OpenFile("textfile.txt", "r");
while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof(buffer))) {
    PushArrayString(lines, buffer, sizeof(buffer));
}
CloseHandle(file);


new n = GetArraySize(lines);
if (n != 0) {
    new index = GetRandomInt(0, n - 1);
    GetArrayString(lines, index, buffer, sizeof(buffer));
    PrintToChatAll(buffer);
}

CloseHandle(lines);
__________________

Last edited by splewis; 10-18-2014 at 19:30. Reason: int -> new
splewis is offline
yuppicide
Member
Join Date: Dec 2013
Old 10-18-2014 , 12:10   Re: Randomly read from a text file?
Reply With Quote #3

Not sure where exactly to put.. I get like 6 or 7 error messages:

Code:
   #include <sourcemod>
   #include <colors>

   public Plugin:myinfo = {
	name = "Troll",
	author = "Yuppicide",
	version = "1.0.3",
	url = "http://www.serverofdeath.com/"
   }

   new Handle:lines = CreateArray(1024);
   new Handle:file = Openfile("troll.txt", "r");
   while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof    (buffer))) {
    PushArrayString(lines, buffer, sizeof(buffer));
}
CloseHandle(file);


   public OnPluginStart()
   {
	RegConsoleCmd("sm_troll", Command_Troll);
   }

   public Action:Command_Troll(client, args)
int n = GetArraySize(lines);
if (n != 0) {
    int index = GetRandomInt(0, n - 1);
    GetArrayString(lines, index, buffer, sizeof(buffer));
    PrintToChatAll(buffer);
}

CloseHandle(lines);
As you can see above, I tried putting them in various locations.
yuppicide is offline
hamilton5
Veteran Member
Join Date: Oct 2012
Location: USA
Old 10-18-2014 , 12:51   Re: Randomly read from a text file?
Reply With Quote #4

various locations :facepalm: why wouldn't you put it in your command!?
hamilton5 is offline
yuppicide
Member
Join Date: Dec 2013
Old 10-18-2014 , 13:04   Re: Randomly read from a text file?
Reply With Quote #5

I tried that first and got error messages. No matter where I stick the code I get 5 or 6 error messages.

error 003: declaration of a local variable must appear in a compound block
error 010: invalid function or declaration
error 010: invalid function or declaration
error 010: invalid function or declaration
error 021: symbol already defined "PrintToChatAll"
error 021: symbol already defined "CloseHandle"
yuppicide is offline
hamilton5
Veteran Member
Join Date: Oct 2012
Location: USA
Old 10-18-2014 , 13:09   Re: Randomly read from a text file?
Reply With Quote #6

obviously you don't even script bro... why would you #include <colors>

I think this might be for the new sourcemod after looking at it... there are int's instead of new's

just search scripting or google for what your looking for it has been done many times

Last edited by hamilton5; 10-18-2014 at 13:15.
hamilton5 is offline
yuppicide
Member
Join Date: Dec 2013
Old 10-18-2014 , 13:59   Re: Randomly read from a text file?
Reply With Quote #7

Yeah I'm including colors to change the color of chat. I can do white, red, blue, green, etc. That works fine.
yuppicide is offline
splewis
Veteran Member
Join Date: Feb 2014
Location: United States
Old 10-18-2014 , 19:30   Re: Randomly read from a text file?
Reply With Quote #8

Quote:
Originally Posted by hamilton5 View Post
obviously you don't even script bro... why would you #include <colors>

I think this might be for the new sourcemod after looking at it... there are int's instead of new's

just search scripting or google for what your looking for it has been done many times
Whoops, that was my bad writing that int.

It should like look this:

Code:
#include <sourcemod>
#include <colors>

public Plugin:myinfo = {
    name = "Troll",
    author = "Yuppicide",
    version = "1.0.3",
    url = "http://www.serverofdeath.com/"
}  

public OnPluginStart()
{
    RegConsoleCmd("sm_troll", Command_Troll);
}

public Action:Command_Troll(client, args) 
{
    new Handle:lines = CreateArray(1024);
    new Handle:file = Openfile("troll.txt", "r");
    while (!IsEndOfFile(file) && ReadFileLine(file, buffer, sizeof (buffer))) {
        PushArrayString(lines, buffer, sizeof(buffer));
    }
    CloseHandle(file);

    new n = GetArraySize(lines);
    if (n != 0) {
        new index = GetRandomInt(0, n - 1);
        GetArrayString(lines, index, buffer, sizeof(buffer));
        PrintToChatAll(buffer);
    }
    CloseHandle(lines);
}

If you ever want help, you really need to learn how to format and indent code. It's unreadable otherwise. That colors include does nothing in what I posted, but maybe you're doing something different.
__________________

Last edited by splewis; 10-18-2014 at 19:31.
splewis is offline
micazoid
Veteran Member
Join Date: Oct 2010
Location: Munich - Germany
Old 10-21-2014 , 06:08   Re: Randomly read from a text file?
Reply With Quote #9

Advertisementplugins are doing this
__________________
micazoid is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:37.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode