AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reading from file error (https://forums.alliedmods.net/showthread.php?t=102202)

BOYSplayCS 08-30-2009 20:28

Reading from file error
 
Whenever I try using this code I wrote to read from a file and then randomly pick a line (map) from that file, it doesn't work. I tested it and whenever I use the command it always displays "[AMXX] Denied Command Access: 'amx_map_random'!"

PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "amx_map random"
#define VERSION "1.0"
#define AUTHOR "BOYSplayCS"

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)

    
register_concmd("amx_map_random""eventChangeMap"ADMIN_SLAY"- Randomly change map");
}

public 
eventChangeMap(idlevelcid)
{
    if (
cmd_access(idlevelcid2))
    {
        new 
mapCycle[64];
        
copy(mapCycle63"mapcycle.txt");
        
        if (
file_exists(mapCycle))
        {
            new 
file fopen(mapCycle"rt");
            
            new 
mapData[64];
            
            while (!
feof(file))
            {
                
fgets(filemapData63);
                
                
server_cmd("changelevel %s"mapData);
            }
        }
    }
    
client_print(idprint_chat"[AMXX] Denied Command Access: 'amx_map_random'!");
    return 
PLUGIN_HANDLED;



Bad_Bud 08-30-2009 21:09

Re: Reading from file error
 
Well... that print is executed every time eventChangeMap is called, it's not inside any conditional.

Exolent[jNr] 08-30-2009 21:11

Re: Reading from file error
 
PHP Code:

#include <amxmodx>
#include <amxmisc>

#define PLUGIN "amx_map random"
#define VERSION "1.0"
#define AUTHOR "BOYSplayCS"

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)

    
register_concmd("amx_map_random""eventChangeMap"ADMIN_SLAY"- Randomly change map");
}

public 
eventChangeMap(idlevelcid)
{
    if (
cmd_access(idlevelcid2))
    {
        new 
szMap64 ];
        if( 
GetRandomMap"mapcycle.txt"szMap63 ) )
            
server_cmd"changelevel %s"szMap );
        else
            
console_printid"There were no maps to change to." );
    }
    else
        
client_print(idprint_chat"[AMXX] Denied Command Access: 'amx_map_random'!");
    
    return 
PLUGIN_HANDLED;
}

GetRandomMap( const szMapFile[ ], szReturn[ ], const iLen )
{
    new 
iFile fopenszMapFile"rt" );
    if( !
iFile )
    {
        return 
0;
    }
    
    new Array:
aMaps ArrayCreate64 );
    new 
Trie:tArrayPos TrieCreate( );
    new 
iTotal 0;
    
    static 
szData128 ], szMap64 ];
    while( !
feofiFile ) )
    {
        
fgetsiFileszData127 );
        
parseszDataszMap63 );
        
strtolowerszMap );
        
        if( 
is_map_validszMap ) && !TrieKeyExiststArrayPosszMap ) )
        {
            
ArrayPushStringaMapsszMap );
            
TrieSetCelltArrayPosszMapiTotal );
            
            
iTotal++;
        }
    }
    
    
TrieDestroytArrayPos );
    
    if( !
iTotal )
    {
        
ArrayDestroyaMaps );
        
        return 
0;
    }
    
    
ArrayGetStringaMapsrandomiTotal ), szReturniLen );
    
    
ArrayDestroyaMaps );
    
    return 
1;



Mxnn 08-30-2009 21:34

Re: Reading from file error
 
Its the same if i don't use ";" or don't use it?
In C++ is required but in the .sma default plugins did not appears.

fezh 08-30-2009 21:50

Re: Reading from file error
 
Quote:

Originally Posted by Mxnn (Post 918493)
Its the same if i don't use ";" or don't use it?
In C++ is required but in the .sma default plugins did not appears.

I think is an habit. Lot of people who write scripts in C++ use semi-colons in they AMXX plugins.

Exolent[jNr] 08-30-2009 22:07

Re: Reading from file error
 
In Pawn, semicolons are not required unless "#pragma semicolon 1" is at the top of the plugin.

xbatista 08-31-2009 02:58

Re: Reading from file error
 
Nice example, btw can you show example without Arrays?

BOYSplayCS 08-31-2009 08:30

Re: Reading from file error
 
Bad_Bud: Woops, I put it in the wrong brackets.

Mxnn: I've scripted C++ before and Java, so for me it's a habit that is not good to break.

Exolent[jNr]: Ahhh, thank you!

Exolent[jNr] 08-31-2009 09:22

Re: Reading from file error
 
Quote:

Originally Posted by xbatista (Post 918634)
Nice example, btw can you show example without Arrays?

It's better with Arrays and Tries, but sure.

Code:
GetRandomMap( const szMapFile[ ], szReturn[ ], const iLen ) {     new iFile = fopen( szMapFile, "rt" );     if( !iFile )     {         return 0;     }         const MAX_MAPS = 100;         static szMaps[ MAX_MAPS ][ 64 ];     new iTotal = 0;         static szData[ 128 ], szMap[ 64 ], i, bool:bExists;     while( !feof( iFile ) && iTotal < MAX_MAPS )     {         fgets( iFile, szData, 127 );         parse( szData, szMap, 63 );         strtolower( szMap );                 if( is_map_valid( szMap ) )         {             bExists = false;             for( i = 0; i < iTotal; i++ )             {                 if( equal( szMaps[ i ], szMap ) )                 {                     bExists = true;                     break;                 }             }                         if( !bExists )             {                 copy( szMaps[ iTotal++ ], 63, szMap );             }         }     }         if( !iTotal )     {         return 0;     }         copy( szReturn, iLen, szMaps[ random( iTotal ) ] );         return 1; }

xbatista 08-31-2009 09:22

Re: Reading from file error
 
I'm confused with those arrays

thanks
Quote:

Originally Posted by Exolent[jNr] (Post 918821)
It's better with Arrays and Tries, but sure.

Code:
GetRandomMap( const szMapFile[ ], szReturn[ ], const iLen ) {     new iFile = fopen( szMapFile, "rt" );     if( !iFile )     {         return 0;     }         const MAX_MAPS = 100;         static szMaps[ MAX_MAPS ][ 64 ];     new iTotal = 0;         static szData[ 128 ], szMap[ 64 ], i, bool:bExists;     while( !feof( iFile ) && iTotal < MAX_MAPS )     {         fgets( iFile, szData, 127 );         parse( szData, szMap, 63 );         strtolower( szMap );                 if( is_map_valid( szMap ) )         {             bExists = false;             for( i = 0; i < iTotal; i++ )             {                 if( equal( szMaps[ i ], szMap ) )                 {                     bExists = true;                     break;                 }             }                         if( !bExists )             {                 copy( szMaps[ iTotal++ ], 63, szMap );             }         }     }         if( !iTotal )     {         return 0;     }         copy( szReturn, iLen, szMaps[ random( iTotal ) ] );         return 1; }



All times are GMT -4. The time now is 14:59.

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