Help with Counting Strings
#1

I have never tried to do this before:

pawn Код:
PlayerInfo[playerid][soldier]
PlayerInfo[playerid][sniper]
PlayerInfo[playerid][assault]
These are 3 player classes, by default, the player has the first one, that does mean total classes = 1, so we will get this:

pawn Код:
PlayerInfo[playerid][soldier] = "Soldier"
PlayerInfo[playerid][sniper] = ""
PlayerInfo[playerid][assault] = ""
When the player makes progress in-game, he can unlock more classes.
For example, he will unlock sniper class, which does mean that total classes are equal to 2, so we will get this:

pawn Код:
PlayerInfo[playerid][soldier] = "Soldier"
PlayerInfo[playerid][sniper] = "Sniper"
PlayerInfo[playerid][assault] = ""
ok, now im gonna send the total classes number in a client message to the player.
My problem is how to count them?

SOLUTION:
[QUOTE=Konstantinos;2966974]I wouldn't use:
pawn Код:
PlayerInfo[playerid][sniper] = "";
but:
pawn Код:
PlayerInfo[playerid][sniper][0] = EOS;
// OR
PlayerInfo[playerid][sniper][0] = '\0';
When you want to make the string empty, use the above instead of "".
Now it's null so:
pawn Код:
new
    count; // it doesn't need to be an array as you said in your above post.

if (!isnull(PlayerInfo[playerid][soldier])) ++count;
if (!isnull(PlayerInfo[playerid][sniper])) ++count;
if (!isnull(PlayerInfo[playerid][assault])) ++count;

// format(...);
// Using "count" as argument will give the total classes.
and
pawn Код:
#if !defined isnull
    #define isnull(%1) \
                ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif
Reply
#2

u can use like this --
pawn Код:
new totalclass=0;
if(PlayerInfo[playerid][soldier] != "")totalclass++;
if(PlayerInfo[playerid][sniper] != "")totalclass++;
if(PlayerInfo[playerid][assault] != "")totalclass++;
/*so now in totalcalss variable the total number of calsses are loaded*/
not tried it yet but this may work
Reply
#3

First of all the variable totalclass must be for each player, so it has to be totalclass[playerid]
second i don't really want to use much player variables in my GM, and im sure there is another way to count the strings :S
Thanks for your suggestion anyways, any help?
Reply
#4

I wouldn't use:
pawn Код:
PlayerInfo[playerid][sniper] = "";
but:
pawn Код:
PlayerInfo[playerid][sniper][0] = EOS;
// OR
PlayerInfo[playerid][sniper][0] = '\0';
When you want to make the string empty, use the above instead of "".
Now it's null so:
pawn Код:
new
    count; // it doesn't need to be an array as you said in your above post.

if (!isnull(PlayerInfo[playerid][soldier])) ++count;
if (!isnull(PlayerInfo[playerid][sniper])) ++count;
if (!isnull(PlayerInfo[playerid][assault])) ++count;

// format(...);
// Using "count" as argument will give the total classes.
and
pawn Код:
#if !defined isnull
    #define isnull(%1) \
                ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
#endif
@BroZeus: It's not integer to use "!=".
Reply
#5

Of course i would use that, but in this situation i found myself obliged to use PlayerInfo[playerid][soldier] = "Soldier"
because i will need that string later xD
Reply
#6

this maybe -
pawn Код:
new totalclass[MAX_PLAYERS]=0;
if(PlayerInfo[playerid][soldier] != "")totalclass[playerid]++;
if(PlayerInfo[playerid][sniper] != "")totalclass[playerid]++;
if(PlayerInfo[playerid][assault] != "")totalclass[playerid]++;
/*so now in totalcalss[playerid] variable the total number of calsses are loaded*/
Reply
#7

Nice try, but I told you i don't want to use much player variables :/
Reply
#8

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
this maybe -
pawn Код:
new totalclass[MAX_PLAYERS]=0;
if(PlayerInfo[playerid][soldier] != "")totalclass[playerid]++;
if(PlayerInfo[playerid][sniper] != "")totalclass[playerid]++;
if(PlayerInfo[playerid][assault] != "")totalclass[playerid]++;
/*so now in totalcalss[playerid] variable the total number of calsses are loaded*/
have you even tried the code before?
Reply
#9

Quote:
Originally Posted by BroZeus
Посмотреть сообщение
this maybe -
pawn Код:
new totalclass[MAX_PLAYERS]=0;
if(PlayerInfo[playerid][soldier] != "")totalclass[playerid]++;
if(PlayerInfo[playerid][sniper] != "")totalclass[playerid]++;
if(PlayerInfo[playerid][assault] != "")totalclass[playerid]++;
/*so now in totalcalss[playerid] variable the total number of calsses are loaded*/
Using "!=" will not work for strings, read the above posts.

Quote:
Originally Posted by Battlezone
Посмотреть сообщение
Of course i would use that, but in this situation i found myself obliged to use PlayerInfo[playerid][soldier] = "Soldier"
because i will need that string later xD

You didn't understand. You should make null those you used "" instead. To those you want to add a name, do it. It will count only those which are not null; hence I told you to reset the strings.
Reply
#10

Quote:
Originally Posted by Battlezone
Посмотреть сообщение
Nice try, but I told you i don't want to use much player variables :/
You have to keep it otherwise one player variable will mix with other and will caz bugs there is no other way of creating a seprate variable for each player
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)