Commands with arguments - 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: Plugin Development (
https://sampforum.blast.hk/forumdisplay.php?fid=18)
+--- Thread: Commands with arguments (
/showthread.php?tid=611152)
Commands with arguments -
devStyle - 02.07.2016
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.
Re: Commands with arguments -
CodeStyle175 - 02.07.2016
dont reinvent the wheel, just use zcmd and go on with your life.
Re: Commands with arguments -
devStyle - 02.07.2016
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!
Re: Commands with arguments - NewbProgrammer - 03.07.2016
Are you using C or C++?
Re: Commands with arguments -
devStyle - 03.07.2016
С++.
Re: Commands with arguments -
iggy1 - 03.07.2016
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::string> Params{};
/** Construct stringstream with cmdtext. **/
std::stringstream ss{ cmdtext };
/** Loop through stream extracting params. (space delimiter)**/
while ( std::getline(ss, Buffer, ' ') )
{
Params.push_back( std::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]);
}