SA-MP Forums Archive
Help with strtok and tmp etc. - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Help with strtok and tmp etc. (/showthread.php?tid=279039)



Help with strtok and tmp etc. - Jack_Leslie - 25.08.2011

Hi guys, so I'm having trouble with strtok (I'm using strcmp, not DCMD or anything, and that's what I wanna use so please don't try and convince me otherwise). I don't know how to define the first entered number, then the second, and then the third etc.

So an example of a command I'm looking for is e.g. "/example 1 3 4 6", and so that would send the client a message or whatever, with "1 3 4 6".. do you understand? I don't know how to define each thing after the first "strval(tmp)".


Re: Help with strtok and tmp etc. - =WoR=Varth - 25.08.2011

pawn Code:
tmp = strtok(cmdtext, idx);
tmp1 = strtok(tmp,idx);
Maybe?


Re: Help with strtok and tmp etc. - Jack_Leslie - 25.08.2011

Quote:
Originally Posted by varthshenon
View Post
pawn Code:
tmp = strtok(cmdtext, idx);
tmp1 = strtok(tmp,idx);
Maybe?
pawn Code:
new color = strtok(cmdtext, idx);
new color2 = strtok(tmp, idx);
Code:
error 006: must be assigned to an array
error 033: array must be indexed (variable "-unknown-")



Re: Help with strtok and tmp etc. - Bakr - 25.08.2011

Quote:
Originally Posted by Jack_Leslie
View Post
Hi guys, so I'm having trouble with strtok (I'm using strcmp, not DCMD or anything, and that's what I wanna use so please don't try and convince me otherwise).
No offense, but that's just idiotic. Why would you WANT to use an outdated and inefficient method when you have faster options available, that are twice as easy to learn?

Anyway, it helps if you actually understood what the strtok function did. It simply removes white spaces found in a string, based on an index to start searching at.
pawn Code:
new
   tmp1[ 128 ],
   tmp2[ 128 ],
   tmp3[ 128 ],
   idx;

tmp1 = strtok( cmdtext, idx ); // Finds the first parameter
tmp2 = strtok( cmdtext, idx ); // Finds the second parameter
tmp3 = strtok( cmdtext, idx ); // Finds the third parameter
Note that this method is not very flexible, and you will have to use the functions in those orders to retrieve the desired data. The variable we created, idx, will store the index of the current white space. That's why when you call the strtok function again, it doesn't return the same white space.


Re: Help with strtok and tmp etc. - Jack_Leslie - 25.08.2011

Quote:
Originally Posted by Bakr
View Post
No offense, but that's just idiotic. Why would you WANT to use an outdated and inefficient method when you have faster options available, that are twice as easy to learn?

Anyway, it helps if you actually understood what the strtok function did. It simply removes white spaces found in a string, based on an index to start searching at.
pawn Code:
new
   tmp1[ 128 ],
   tmp2[ 128 ],
   tmp3[ 128 ],
   idx;

tmp1 = strtok( cmdtext, idx ); // Finds the first parameter
tmp2 = strtok( cmdtext, idx ); // Finds the second parameter
tmp3 = strtok( cmdtext, idx ); // Finds the third parameter
Note that this method is not very flexible, and you will have to use the functions in those orders to retrieve the desired data. The variable we created, idx, will store the index of the current white space. That's why when you call the strtok function again, it doesn't return the same white space.
I know it's stupid, but it's just what I wanna do at the moment.

I got 2 errors with that.
Code:
H:\Real RP - V2 Project\gamemodes\rlrp.pwn(28606) : error 006: must be assigned to an array
H:\Real RP - V2 Project\gamemodes\rlrp.pwn(28607) : error 006: must be assigned to an array
pawn Code:
new
            color[22],
            color2[22],
            idx;
        color = strtok( cmdtext, idx ); // Finds the first parameter
        color2 = strtok( cmdtext, idx ); // Finds the second parameter



Re: Help with strtok and tmp etc. - Bakr - 25.08.2011

It's also worth a note that you'll need to get the spaces of the original text anyway.
pawn Code:
public OnPlayerCommandText( playerid, cmdtext[ ] )
{
   new
      cmd[ 256 ],
      tmp[ 256 ],
      idx;

   cmd = strtok( cmdtext, idx );
   
   // Inside command
   if( !strcmp( cmd, "/whatever", true ) )
   tmp = strtok( cmdtext, idx );
   new color = strval( tmp );
   tmp = strtok( cmdtext, idx );
   new color2 = strval( tmp );



Re: Help with strtok and tmp etc. - Jack_Leslie - 25.08.2011

Quote:
Originally Posted by Bakr
View Post
It's also worth a note that you'll need to get the spaces of the original text anyway.
pawn Code:
public OnPlayerCommandText( playerid, cmdtext[ ] )
{
   new
      cmd[ 256 ],
      tmp[ 256 ],
      idx;

   cmd = strtok( cmdtext, idx );
   
   // Inside command
   if( !strcmp( cmd, "/whatever", true ) )
   tmp = strtok( cmdtext, idx );
   new color = strval( tmp );
   tmp = strtok( cmdtext, idx );
   new color2 = strval( tmp );
That half worked... it detects "color" aka the first tmp, but it doesn't detect "color2", the second tmp.


Re: Help with strtok and tmp etc. - Bakr - 25.08.2011

Show your whole command.


Re: Help with strtok and tmp etc. - Jack_Leslie - 25.08.2011

pawn Code:
if(strcmp(cmd, "/carcolor", true) == 0)
    {
        tmp = strtok(cmdtext, idx);
        new car = GetCarIDFromPlayer(playerid);
        new vehid = GetPlayerVehicleID(playerid);
        if(!IsPlayerDriver(playerid))
        {
            SendClientMessage(playerid, COLOR_GREY,"   You're not driving a car !");
            return 1;
        }
        if(vehid != car)
        {
            SendClientMessage(playerid, COLOR_GREY,"   This is not your car !");
            return 1;
        }
        if(!strlen(tmp))
        {
            SendClientMessage(playerid, COLOR_GREY,"   USUAGE: /carcolor [color 1] [color 2]");
            return 1;
        }
        if(PlayerInfo[playerid][pCash] < 499)
        {
            SendClientMessage(playerid, COLOR_GREY, "   You need $500 !");
            return 1;
        }
        tmp = strtok( cmdtext, idx );
        new color = strval( tmp );
        tmp = strtok( cmdtext, idx );
        new color2 = strval( tmp );
        new id = GetCarIDFromPlayer_Enum(playerid);
        CarInfo[id][vColorOne] = color;
        CarInfo[id][vColorTwo] = color2;
        DestroyVehicle(car);
        CarInfo[car][ownedvehicle] = CreateVehicle(CarInfo[id][vModel],CarInfo[id][vLocationx],CarInfo[id][vLocationy],CarInfo[id][vLocationz],CarInfo[id][vAngle],CarInfo[id][vColorOne],CarInfo[id][vColorTwo],300000);
        SendClientMessage(playerid, COLOR_GREY,"   Changed color for $500, color changes on respawn !");
        GivePlayerMoney(playerid, -500);
        PlayerInfo[playerid][pCash] -= 500;
        return 1;
    }
color2 always gets set as 0 no matter what the input is.


Re: Help with strtok and tmp etc. - Bakr - 25.08.2011

Remove the very first
pawn Code:
tmp = strtok( cmdtext, idx );
line that is directly under the command.


Re: Help with strtok and tmp etc. - Jack_Leslie - 25.08.2011

Quote:
Originally Posted by Bakr
View Post
Remove the very first
pawn Code:
tmp = strtok( cmdtext, idx );
line that is directly under the command.
Now it doesn't do anything if I type a value into the first and/or second tmp. It replies with:
pawn Code:
if(!strlen(tmp))
        {
            SendClientMessage(playerid, COLOR_GREY,"   USUAGE: /carcolor [color 1] [color 2]");
            return 1;
        }



Re: Help with strtok and tmp etc. - Bakr - 25.08.2011

It's because you check if the tmp has a value before assigning the information to it. Move the check after you use the first strtok function.
pawn Code:
tmp = strtok( cmdtext, idx );
if( !strlen( tmp ) )
{
   // yadada
}
tmp = strtok( cmdtext, idx );
if( !strlen( tmp ) )
{
   // yadada
}



Re: Help with strtok and tmp etc. - Jack_Leslie - 25.08.2011

Quote:
Originally Posted by Bakr
View Post
It's because you check if the tmp has a value before assigning the information to it. Move the check after you use the first strtok function.
pawn Code:
tmp = strtok( cmdtext, idx );
if( !strlen( tmp ) )
{
   // yadada
}
tmp = strtok( cmdtext, idx );
if( !strlen( tmp ) )
{
   // yadada
}
Wow, finally works perfectly. Thankyou.


Re: Help with strtok and tmp etc. - Speed - 25.08.2011

use sscanf it is easier...