Raised This Month: $32 Target: $400
 8% 

Solved Read from file.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-10-2020 , 14:18   Read from file.
Reply With Quote #1

Hi,

I've got a few questions.. I'm not really familiar with reading from files or writing them.

1) I'm planning to make a menu created from a file. The file should look like this
  • Text1 [Number 1, Number2, Number3] // This is line 1
  • Text2 [Number 1, Number2, Number3] // Option line 2.

while the menu should look like this
  • Text1
  • Text2

And the menu goes on depending on how much lines there are in the file.
2) Eventually, when the player made a choice of the menu, the Numbers in the menu will be used later on in the menu handler, but without the brackets ([]), but not shown in the menu.

Basicly, i wanna create the menu at plugin_init() and later on when a command is used such as "say /command", the menu should pop up. I don't wanna destroy the menu as changes aren't made every map change.

Also i don't know what kind of file i should use, .txt or .ini (what's the difference?).

Anyone who could help me out with this?

I've got this from a post from 2011, just wondering if this is done the right way, and if it is, how to use it.

PHP Code:
       // Register global menu, restart server when file has been changed.
    
iMenu menu_create("\r[\wAdvanced Glow Menu\r]""GlowHandler");

    new 
szPath[128], FileHandleszCache[128];
    
get_configsdir(szPathcharsmax(szPath));

    
formatex(szPathcharsmax(szPath), "%s%s"szPath"/%s"szFileName);

    if(!
file_exists(szPath)) {
        
log_amx("Error: File does not exist (%s)."szPath);
        return;
    }

    
FileHandle fopen(szPath"rt");

    if(!
FileHandle) {
        
log_amx("Error: Could not open file (%s)."szPath);
        return;
    }

    new 
iCounter;
    while(!
feof(FileHandle)) {
        
fgets(FileHandleszCachecharsmax(szCache));
        
iCounter++;
        
log_amx("Loaded line number %d: %s"iCounterszCache);
    }

    for(new 
isizeof(szCache); i++) {
        
menu_additem(iMenuszCache[i]);
    }

    
fclose(FileHandle);

__________________

Last edited by Napoleon_be; 04-11-2020 at 15:41.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 04-10-2020 , 14:57   Re: Read from file.
Reply With Quote #2

This is the general code I use in all my plugins.
It's pretty much the same as above. I added comments explaining what each line does.

Code:
ReadFile() {     // Store the file path in the szFilename variable (amxmodx/configs/YourFile.ini)     new szFilename[256]     get_configsdir(szFilename, charsmax(szFilename))     add(szFilename, charsmax(szFilename), "/YourFile.ini")     // Open the file in (r)ead (t)ext mode and store its handle in the iFilepointer variable.     new iFilePointer = fopen(szFilename, "rt")     // Check if the file was successfully opened.     if(iFilePointer)     {         // szData will hold each line read from the file.         new szData[128]         // Execute the code in this loop until the end of the file has been reached.         // This will read every line in the file 1 by 1.         while(!feof(iFilePointer))         {             // Read a line from the file and store in in the szData variable.             fgets(iFilePointer, szData, charsmax(szData))             // Remove whitespace characters from the beginning and end of the string (not necessary, but good to have just in case).             trim(szData)                         // Skip the line if it's empty or starts with a ";"             if(szData[0] == EOS || szData[0] == ';')             {                 continue             }             // We have our final line.             log_amx("The line is %s", szData)         }         // Close the file after we're done using it.                fclose(iFilePointer)     } }

Now that you have your final line stored in "szData", you can do anything you want with it.
The easiest way you can handle creating a menu is using a basic format like:

Code:
"text" "number1" "number2" "number3"

With this, you can just use the "parse" function directly on "szData" to get the arguments from that line.

Code:
parse(szData, szText, charsmax(szText), szNumber1, charsmax(szNumber1), ...)

You can use the format you mentioned in your first post too, but it will require some more lines of code to handle - you can do that after you figure out the basics.

If your menu is static (the items in it don't change), I suggest you create it as a global menu instead of reading the file each time the player wants to use it.
__________________

Last edited by OciXCrom; 04-10-2020 at 14:59.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-10-2020 , 15:04   Re: Read from file.
Reply With Quote #3

Well, that's what i'm doing, i'm creating this menu in plugin_init so i don't have to destroy it and create it every single time again. I'll figure out what you posted and keep you updated, thanks for your help already.

EDIT: Okay, so now i understand how to read from a file, how can i add it to the menu, and use the last number arguments (without [] or ,) in the menu handler? (Keep in mind that the Number arguments can have more than 1 digit.)

Also, i'm getting an error:

error 033: array must be indexed (variable "-unknown-")

It's this line:

PHP Code:
            if(szData[0] == EOS || szData[0] == ";"
__________________

Last edited by Napoleon_be; 04-10-2020 at 15:19.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 04-10-2020 , 15:55   Re: Read from file.
Reply With Quote #4

You're getting that error because you're using double quotes. Use single quotes when checking a character.

I'll give you a more detailed explanation about the rest tomorrow if someone else doesn't.
Just remember that the entire line is a string, so you can do anything you want with it. Try to separate the arguments in different variables so it will be easier to add them in a menu.
__________________

Last edited by OciXCrom; 04-10-2020 at 15:57.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-10-2020 , 16:07   Re: Read from file.
Reply With Quote #5

Quote:
Originally Posted by OciXCrom View Post
You're getting that error because you're using double quotes. Use single quotes when checking a character.

I'll give you a more detailed explanation about the rest tomorrow if someone else doesn't.
Just remember that the entire line is a string, so you can do anything you want with it. Try to separate the arguments in different variables so it will be easier to add them in a menu.
I had some help from a friend i've known for a long time, i'll share the code tomorrow along with your answer, thanks again.
__________________
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
thEsp
BANNED
Join Date: Aug 2017
Old 04-10-2020 , 16:28   Re: Read from file.
Reply With Quote #6

Firstly, I don't really understand your plan. If you want to parse single-line settings from a file (i.e: "napoleon" "won" "52" "battles"), use parse native and convert results into whatever you need.

Quote:
Eventually, when the player made a choice of the menu, the Numbers in the menu will be used later on in the menu handler, but without the brackets ([]), but not shown in the menu.
If you want to hide something in a menu (a string or array of integers, doesn't support 2-dimensional arrays sadly), check 3rd parameter of menu_additem.

Quote:
Also i don't know what kind of file i should use, .txt or .ini (what's the difference?).
You can use whatever file extension you want, but configuration files (.ini) are supposed to save data like this for example:
Code:
[en] ; Section
MSG_HELLO = Hello, world!
Yes, this is the input used for plugin translations. Some people (including me? ) save this kind of files as text, which is wrong. Fyi, there's a header that parses configuration files for AMXX. If you are reading files according your plan, use the text extension.

Last edited by thEsp; 04-10-2020 at 16:42.
thEsp is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 04-10-2020 , 19:52   Re: Read from file.
Reply With Quote #7

Also note that using while( !feof(file) ) is not recommended (actually, it's technically wrong): https://forums.alliedmods.net/showthread.php?t=310086
__________________

Last edited by fysiks; 04-10-2020 at 19:54.
fysiks is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-11-2020 , 08:44   Re: Read from file.
Reply With Quote #8

Okay thanks for the help already, this is what i have so far, and the menu pops up and does everything it has to do. Now one more question, i need to retrieve data from the file in my handler.

PHP Code:
ReadFile()
{
    new 
szFileName[256];
    
get_configsdir(szFileNamecharsmax(szFileName));
    
add(szFileNamecharsmax(szFileName), "/GlowMenu.txt");

    new 
iFilePointer fopen(szFileName"rt");

    if(
iFilePointer)
    {
        while(
fgets(iFilePointerszDatacharsmax(szData)))
        {
            
fgets(iFilePointerszDatacharsmax(szData));
            
trim(szData);

            if(
szData[0] == EOS || szData[0] == ';')
            {
                continue;
            }

            
iColorMenu menu_create("\r[\wAdvanced Glow Menu\r]""ColorMenuHandler");

            new 
szText[128], szNumber1[3], szNumber2[3], szNumber3[3];
            
parse(szDataszTextcharsmax(szText), szNumber1charsmax(szNumber1), szNumber2charsmax(szNumber2), szNumber3charsmax(szNumber3));

            new 
GlowColors[RgbEnum];
            
GlowColors[r] = str_to_num(szNumber1);
            
GlowColors[g] = str_to_num(szNumber2);
            
GlowColors[b] = str_to_num(szNumber3);

            
TrieSetArray(GlowTrieszTextGlowColorsRgbEnum);

            
menu_additem(iColorMenuszText);

            
//log_amx("The line is %s", szData);
        
}

        
fclose(iFilePointer);
    }

How do i retrieve the rgb numbers in the file, and how do i use them in set_user_rendering()? Also, i'd like to use the color name from the file in a chat message. I wanna use TrieGetArray, but i can't seem to figure it out.
__________________

Last edited by Napoleon_be; 04-11-2020 at 08:46.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
Old 04-11-2020, 09:27
thEsp
This message has been deleted by thEsp. Reason: nvm
thEsp
BANNED
Join Date: Aug 2017
Old 04-11-2020 , 09:30   Re: Read from file.
Reply With Quote #9

If you mean choosing the color from menu (when you select an item), read what I said.
Quote:
If you want to hide something in a menu (a string or array of integers, doesn't support 2-dimensional arrays sadly), check 3rd parameter of menu_additem.

Last edited by thEsp; 04-11-2020 at 09:32.
thEsp is offline
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 04-11-2020 , 09:50   Re: Read from file.
Reply With Quote #10

How is the 3rd parameter of menu_additem gonna help me with this? This is how my code looks like atm, first time i ever try using tries.

PHP Code:
enum _:RgbEnum {
    
r,
    
g,
    
b
}

public 
ColorMenuHandler(idiColorMenuitem) {
    if(
is_user_alive(id)) {
        new 
szText[128];
        
formatex(szTextcharsmax(szText), "%i"szData);

        new 
GlowColors[RgbEnum];

        
TrieGetArray(GlowTrieszTextGlowColorsRgbEnum);

        
set_user_glow(idGlowColors[r], GlowColors[g], GlowColors[b]);
    }

__________________

Last edited by Napoleon_be; 04-11-2020 at 09:54.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
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:28.


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