AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Read models in .txt (https://forums.alliedmods.net/showthread.php?t=184171)

rodrigo286 05-01-2012 17:33

Read models in .txt
 
I must make my script read information from a. Txt.

example:

A. Txt models.txt was named in a folder and it would be listed SourceMod model by model:

models/player/slow/aliendrone_v3/slow_alien.mdl;oldmodel
models/player/slow/aliendrone_v3/slow_alien_hs.mdl;hs
models/player/slow/aliendrone_v3/slow_alien_head.mdl;head
player.mdl ...; oldmodel
...

oldmodel for default model
hs for headless model
head for head model

And everything was being set up, out instead to be done in the script, as below:

PHP Code:

        if (StrEqual(oldmodel"models/player/slow/aliendrone_v3/slow_alien.mdl"false))
        {
            
bodymodel "models/player/slow/aliendrone_v3/slow_alien_hs.mdl";
            
headmodel "models/player/slow/aliendrone_v3/slow_alien_head.mdl";
        } 

With support for putting more than one model.

How can I do this?

Thanks.

TheAvengers2 05-01-2012 18:37

Re: Read models in .txt
 
I would suggest using a KeyValue file, however, if you want to do something custom and write your own parser, then you can do so with these functions. You can then load the data into arrays and iterate through it.

rodrigo286 05-01-2012 19:23

Re: Read models in .txt
 
In the case I only need:

PHP Code:

ReadFileString
buildpath 

To read a text from .txt file that the user will create.

Correct?

TheAvengers2 05-01-2012 20:44

Re: Read models in .txt
 
I would suggest using ReadFileLine and looping through. Something like: while(ReadFileLine(hndl, sBuffer, sizeof(sBuffer))) {}

You'll then need to break up the string and figure out which array it goes into. :P

rodrigo286 05-01-2012 21:08

Re: Read models in .txt
 
You can make this more directed to my plugin?

Still do not understand.

Thanks ^^

TheAvengers2 05-01-2012 21:32

Re: Read models in .txt
 
Code:

new String:g_sOldModel[64][128], g_iOldModelPos;
new String:g_sBodyModel[64][128], g_iBodyModelPos;
new String:g_sHeadModel[64][128], g_iHeadModelPos;

public OnPluginStart()

    new Handle:hndl = OpenFile("models.txt", "r");
    decl String:sBuffer[128], String:sBufferArray[2][128];

    while (ReadFileLine(hndl, sBuffer, sizeof(sBuffer)))
    {
        if (ExplodeString(sBuffer, ";", sBufferArray, sizeof(sBufferArray), sizeof(sBufferArray[]))
        {
            if (StrEqual(sBufferArray[1], "oldmodel"))
            {
                strcopy(g_sOldModel[(g_iOldModelPos++)], sizeof(g_sOldModel[]), sBufferArray[0]);
            }
            else if (StrEqual(sBufferArray[1], "hs"))
            {
                strcopy(g_sBodyModel[(g_iBodyModelPos++)], sizeof(g_sBodyModel[]), sBufferArray[0]);
            }
            else if (StrEqual(sBufferArray[1], "oldmodel"))
            {
                strcopy(g_sHeadModel[(g_iHeadModelPos++)], sizeof(g_sHeadModel[]), sBufferArray[0]);
            }
        }
    }

    if (g_iOldModelPos != g_iBodyModelPos || g_iBodyModelPos != g_iHeadModelPos) // a problem?

    CloseHandle(hndl);
}

Code:

for (new i = 1; i <= g_iOldModelPos; i++)
{
    if (StrEqual(oldmodel, g_sOldModel[i], false))
    {
        strcopy(bodymodel, sizeof(bodymodel), g_sBodyModel[i]);
        strcopy(headmodel, sizeof(headmodel), g_sHeadModel[i]);
        // do something?
    }
}

I think this is right... I haven't tested, nor am I familiar with the functions.

rodrigo286 05-01-2012 21:40

Re: Read models in .txt
 
Thanks, I'll test this model.txt should be in the folder addons / SourceMod?

Thanks again.

TheAvengers2 05-01-2012 21:43

Re: Read models in .txt
 
I'd recommend using BuildPath to getting it to where you want it. It's currently using /cstrike/models.txt

rodrigo286 05-02-2012 20:58

Re: Read models in .txt
 
Change here to Head right?

Before
PHP Code:

            else if (StrEqual(sBufferArray[1], "head"))
            {
                
strcopy(g_sHeadModel[(g_iHeadModelPos++)], sizeof(g_sHeadModel[]), sBufferArray[0]);
            } 

After
PHP Code:

            else if (StrEqual(sBufferArray[1], "head"))
            {
                
strcopy(g_sHeadModel[(g_iHeadModelPos++)], sizeof(g_sHeadModel[]), sBufferArray[0]);
            } 

Standing as below with build patch:

PHP Code:

new String:g_sOldModel[64][128], g_iOldModelPos;
new 
String:g_sBodyModel[64][128], g_iBodyModelPos;
new 
String:g_sHeadModel[64][128], g_iHeadModelPos;

public 
OnPluginStart()

    
    new 
String:configPath[PLATFORM_MAX_PATH];
    
BuildPath(Path_SMconfigPathsizeof(configPath), "configs/models.txt");

    new 
Handle:hndl OpenFile("models.txt""r");
    
decl String:sBuffer[128], String:sBufferArray[2][128];

    while (
ReadFileLine(hndlsBuffersizeof(sBuffer)))
    {
        if (
ExplodeString(sBuffer";"sBufferArraysizeof(sBufferArray), sizeof(sBufferArray[]))
        {
            if (
StrEqual(sBufferArray[1], "oldmodel"))
            {
                
strcopy(g_sOldModel[(g_iOldModelPos++)], sizeof(g_sOldModel[]), sBufferArray[0]);
            }
            else if (
StrEqual(sBufferArray[1], "hs"))
            {
                
strcopy(g_sBodyModel[(g_iBodyModelPos++)], sizeof(g_sBodyModel[]), sBufferArray[0]);
            }
            else if (
StrEqual(sBufferArray[1], "head"))
            {
                
strcopy(g_sHeadModel[(g_iHeadModelPos++)], sizeof(g_sHeadModel[]), sBufferArray[0]);
            }
        }
    }

    
CloseHandle(hndl);


PHP Code:

for (new 1<= g_iOldModelPosi++)
{
    if (
StrEqual(oldmodelg_sOldModel[i], false))
    {
        
strcopy(bodymodelsizeof(bodymodel), g_sBodyModel[i]);
        
strcopy(headmodelsizeof(headmodel), g_sHeadModel[i]);
        
// do something?
    
}



TheAvengers2 05-02-2012 23:18

Re: Read models in .txt
 
yeah, that's right. silly copy & paste always making problems. :P

edit:
Quote:

Originally Posted by rodrigo286 (Post 1700830)
public OnPluginStart()
{
new String:configPath[PLATFORM_MAX_PATH];
BuildPath(Path_SM, configPath, sizeof(configPath), "configs/models.txt");

new Handle:hndl = OpenFile(configPath, "r");
decl String:sBuffer[128], String:sBufferArray[2][128];

You might want to do that as well. The evil bracket managed to escape somehow. :grrr:


All times are GMT -4. The time now is 11:33.

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