AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   how to read and store only what I need (https://forums.alliedmods.net/showthread.php?t=205651)

Menethil 01-13-2013 02:27

how to read and store only what I need
 
Suposing I have this txt file

Code:


* Sub-Title 1 *
- Text line 1
- Text line 2
- Text line 3
- etc.


* Sub-Title 2 *
- Text line x
- Text line y
- Text line z

* Sub-Title 3*
- and so on..

And I need to get & store only the line's from a Sub-Title...
Best example I can Give is if
PHP Code:

 (get_mapname(///) == Sub-Title X ) {
// to set a task wich random prints to clients some particular map rules
// and ofcourse the rulles will be that Text lines from each sub-title(map)


Ty -.-"

Sylwester 01-13-2013 06:00

Re: how to read and store only what I need
 
You can store rules for different maps in separate files.

Menethil 01-13-2013 06:10

Re: how to read and store only what I need
 
Quote:

Originally Posted by Sylwester (Post 1872673)
You can store rules for different maps in separate files.

PHP Code:

#include <amxmodx>
#include <amxmisc>


public plugin_init(){
    
register_plugin("Test","1.0","We think about it")
}

public 
plugin_cfg() {
    new 
mapname[64];
    
get_mapname(mapname,63)

   
// Here I dont know how to do it -.-"
    
new curent_map_file[] = (%s_rules.txtmapname)

    if (!
file_exists(curent_map_file)) {
        
write_file(curent_map_file"; Text your map rules below")
        
console_print(0"%s file not found. Creating new"curent_map_file)
    }
    
}

public 
client_authorized id set_task(1.0"showrules",id,tmp,1)

public 
showrules (pid[]) {
    new 
id pid[0]
    
    if ( 
get_user_team(id) != && get_user_team(id) != ) {
        if (
id) {
            new 
tmp[1]
            
tmp[0] = id
            set_task
(2.0"showrules",id,tmp,1)  // not yet in server
        
}
        return 
PLUGIN_HANDLED
    
}    

    new 
tmp[1]
    
tmp[0] = id
    
    
    set_task
(3.0"printrules"idtmp1)  // not yet in server
    
    
return PLUGIN_HANDLED
}

public 
printrules(pid[])
{
    new 
id pid[0]
    if (
file_exists(curent_map_file))
        {
        
        
console_print(0"[user] printing rules for user %d"id)
        
        for(
i=0read_file(curent_map_fileitext127num); i++) {
            if (
num && text[0] != ';') {
                
client_print(0,print_chat,"%s"text)    
            }
        }
        }
    
    return 
PLUGIN_HANDLED


And also how could I print rules each one by one, randomly.
I guess this will print the whole text at once

fysiks 01-13-2013 15:09

Re: how to read and store only what I need
 
Wow, that is some bad coding.
  • Code:

    formatex(curent_map_file, charsmax(curent_map_file), "%s_rules.ini", mapname)
  • Code:

    set_task(1.0, "mytask", id)

    public mytask(id)
    {
            // stuff
    }

  • The client is in the server when client_putinserver() is called. There is no reason to create an inefficient loop.
  • You should not be reading from the file every time printrules() is executed. You need to read the file in plugin_cfg() into an array of strings and then you can print one of those strings when printrules() is executed.

Menethil 01-14-2013 08:20

Re: how to read and store only what I need
 
Quote:

Originally Posted by fysiks (Post 1872971)
Wow, that is some bad coding.
  • Code:

    formatex(curent_map_file, charsmax(curent_map_file), "%s_rules.ini", mapname)
  • Code:

    set_task(1.0, "mytask", id)

    public mytask(id)
    {
        // stuff
    }

  • The client is in the server when client_putinserver() is called. There is no reason to create an inefficient loop.
  • You should not be reading from the file every time printrules() is executed. You need to read the file in plugin_cfg() into an array of strings and then you can print one of those strings when printrules() is executed.

Thank you, my above code is similar to some that I found to do my needings, and I ripped some parts, cus I had no clue about reading/storing/and printing data from a separate file.
Now I got it, thanks


All times are GMT -4. The time now is 13:42.

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