One general-string or a string for each command?
#1

Hey dear,

what would you recommend? One string for each command (example below), or only one general string for ALL commands (example below)? Which method eats more RAM? Which is better and more ressource-friendly?

example 1:

pawn Код:
COMMAND:givemoney(playerid, params[])
{
    if (IsPlayerAdmin(playerid))
    {
        new toplayerid,
            amount;
        if (!sscanf(params, "ii", toplayerid, amount))
        {
            if (toplayerid != INVALID_PLAYER_ID)
            {
                new message[40];
                GivePlayerMoney(toplayerid, amount);
                format(message, sizeof(message), "You got $%d from admin!", amount);
                SendClientMessage(toplayerid, 0x00FF00FF, message);
            } else SendClientMessage(playerid, 0xFF0000FF, "That player is not connected");
        } else SendClientMessage(playerid, 0xFFFFFFFF, "Usage: /givemoney <playerid> <amount>");
    } else SendClientMessage(playerid, 0xFF0000FF, "Only admins can use this command!");
    return 1;
}

COMMAND:money(playerid, params[])
{
    if (IsPlayerAdmin(playerid))
    {
        new toplayerid,
            amount;
        if (!sscanf(params, "ii", toplayerid, amount))
        {
            if (toplayerid != INVALID_PLAYER_ID)
            {
                new message[40];
                GivePlayerMoney(toplayerid, GetPlayerMoney(toplayerid) + amount);
                format(message, sizeof(message), "Admin set your money to $%d!", amount);
                SendClientMessage(toplayerid, 0x00FF00FF, message);
            } else SendClientMessage(playerid, 0xFF0000FF, "That player is not connected");
        } else SendClientMessage(playerid, 0xFFFFFFFF, "Usage: /givemoney <playerid> <amount>");
    } else SendClientMessage(playerid, 0xFF0000FF, "Only admins can use this command!");
    return 1;
}
Or example 2:

On top of the script:

new message[128];

pawn Код:
COMMAND:givemoney(playerid, params[])
{
    if (IsPlayerAdmin(playerid))
    {
        new toplayerid,
            amount;
        if (!sscanf(params, "ii", toplayerid, amount))
        {
            if (toplayerid != INVALID_PLAYER_ID)
            {
                GivePlayerMoney(toplayerid, amount);
                format(message, sizeof(message), "You got $%d from admin!", amount);
                SendClientMessage(toplayerid, 0x00FF00FF, message);
            } else SendClientMessage(playerid, 0xFF0000FF, "That player is not connected");
        } else SendClientMessage(playerid, 0xFFFFFFFF, "Usage: /givemoney <playerid> <amount>");
    } else SendClientMessage(playerid, 0xFF0000FF, "Only admins can use this command!");
    return 1;
}

COMMAND:money(playerid, params[])
{
    if (IsPlayerAdmin(playerid))
    {
        new toplayerid,
            amount;
        if (!sscanf(params, "ii", toplayerid, amount))
        {
            if (toplayerid != INVALID_PLAYER_ID)
            {
                GivePlayerMoney(toplayerid, GetPlayerMoney(toplayerid) + amount);
                format(message, sizeof(message), "Admin set your money to $%d!", amount);
                SendClientMessage(toplayerid, 0x00FF00FF, message);
            } else SendClientMessage(playerid, 0xFF0000FF, "That player is not connected");
        } else SendClientMessage(playerid, 0xFFFFFFFF, "Usage: /givemoney <playerid> <amount>");
    } else SendClientMessage(playerid, 0xFF0000FF, "Only admins can use this command!");
    return 1;
}
Will the data in Example 1 permanently saved in the string and consumed resources? I've over 200 commands which use strings and i want to know which method is better for us (server has over 100 users). I do not want that the server lags. Which one eats more RAM? The same thing with floats. Three floats (X,Y,Z) on the top or three floats directly in the affected commands? Will the variables free up/become deleted after the command proceded/ended?

Thanks for help.

Greets
Reply
#2

One string for all the commands works fine, just name it something other than "string".
Reply
#3

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
One string for all the commands works fine, just name it something other than "string".
But what will happen if 2 players use a command at the same time that use the same string-var? :S Is it really recommended?
Reply
#4

What would be the chance of that happening? Formatting the message and sending it takes less than a millisecond.
Reply
#5

Quote:
Originally Posted by Vince
Посмотреть сообщение
What would be the chance of that happening? Formatting the message and sending it takes less than a millisecond.
Oh, okay. Also will there be no difference in the performance if I give each command one new string[128];? Will the ram not became overloaded? :S
Reply
#6

I think you're forgetting that Pawn is single threaded.
Reply
#7

In a command you are allocating space for just one single moment. If you are declaring the space all the way at the top you will have that space allocated the whole time just for when a player types something or you need it.

The pros of the first one (single moment) is that you don't constantly save space that you do not use.
The pros of the second one is that you always have that space ready (but what are the chances of you running out of space?).

So I'd just allocate space for a string in the command itself. Your space isn't always 128 chars anyways. It can also be just a name + hai (which is 24 + 3 (and a null terminator))
Reply
#8

Thanks for help to all. :*
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)