This site has been archived and made available for preservation purposes. No edits can be made.
Command processor
(Difference between revisions)
Return to current revision
-
This script uses OnPlayerChat and processes chat input given by players if the first character is a forward slash.
[h="2"] Code [/h]
[highlight=cpp]
#include "vaultscript.h"
#include <cstdlib>
#include <cstring>
State VAULTSCRIPT OnPlayerChat(ID player, RawString message) noexcept
{
// Player object
Player pplayer(player);
// it's a command when the first character is a /
if (*message == '/')
{
// we can safely tokenize because we won't send the message
std::vector<String> tokens;
RawString cmd = std::strtok(message + 1, " ");
// collecting tokens
while (cmd)
{
-
-
tokens.push_back(String(cmd));
cmd = std::strtok(NULL, " ");
-
+
tokens.emplace_back(cmd);
cmd = std::strtok(nullptr, " ");
-
}
if (!tokens.empty())
{
if (!tokens[0].compare("additem"))
{
// baseID given?
if (tokens.size() > 1)
{
// get the hexadecimal baseID of the item
-
-
Base item = static_cast<Base>(std::strtoul(tokens[1].c_str(), NULL, 16));
-
+
Base item = static_cast<Base>(std::strtoul(tokens[1].c_str(), nullptr, 16));
-
// give item to player with notification
pplayer.AddItem(item, 1, 100.0, False);
}
}
// more commands...
else
{
// send error message
std::strncpy(message, String("Unrecognized command: " + tokens[0]).c_str(), static_cast<size_t>(Index::MAX_CHAT_LENGTH));
return True;
}
}
return False;
}
return True;
}
[/highlight]
[category]Script examples[/category]