Commands with arguments
#1

Hello. Who uses the sampgdk library, tell me. How to work with commands and arguments? For example, /giveadmin [playerid] [levev]. How to lay cmdtext arguments? Or maybe someone has a mini () command processor?
I use this:
Код:
        char idx[16] = " ";
	char newcmdtext[32];
	strcpy(newcmdtext, cmdtext);
	char *cmd = strtok(newcmdtext, idx);
	if (strcmp(cmd, "/giveadmin") == 0) {
//		if(PlayerInfo[playerid].State < LOGGED_PLAYER || AdminInfo[playerid].AdminLevel < ADMIN_LEVEL_MAX) return 0;
		int id, level;
		if(sscanf(cmdtext, "%s%d%d", cmd, &id, &level) != 3) return PlayerInfo[playerid].SendMessage(0xFFFFFFAA, "ERROR!");
		PlayerInfo[playerid].SendMessage(0xFFFFFFAA, string_format("ID: %d LEVEL: %d", id, level).c_str());
		return true;
	}
But it is a bad option. Because if there is too long a command or something, the server crashed.
Reply
#2

dont reinvent the wheel, just use zcmd and go on with your life.
Reply
#3

Quote:
Originally Posted by CodeStyle175
Посмотреть сообщение
dont reinvent the wheel, just use zcmd and go on with your life.
You mean this? If so, I saw this topic, but I think it's outdated for ~3 years
https://sampforum.blast.hk/showthread.php?tid=436322
Try to use, thank you!
Reply
#4

Are you using C or C++?
Reply
#5

С++.
Reply
#6

One easy way would be using a stringstream and std::getline. There are also many libraries out there that will do this more efficiently/faster like boost::tokenizer.
PHP код:
#include <sstream>
#include <vector>
/** Will be used to push back params into vector **/
std::string Buffer{};
/** Storage for params. **/
std::vector<std::stringParams{};
/** Construct stringstream with cmdtext. **/
std::stringstream sscmdtext };
/** Loop through stream extracting params. (space delimiter)**/
while ( std::getline(ssBuffer' ') )
{
    
Params.push_backstd::move(Buffer) );
}
/** Get an int **/
// you will need to bounds check before indexing params. Params.size() returns number of params supplied
if (Params.size() == 3)//params[0] is the command name
{
    
/** Cast params. **/
    
int PlayerID stoi(Params[1]);
    
int Level stoi(Params[2]);

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)