Set slot
#1

Yo
I just gotta set players slot in event. I did it like that but i totally sucks and dont work properly, should use loop but how exactly i got no idea.
Anyone?

Код:
	            if(Player0[0] == -1)
	            {
	                Player0[0] = playerid;
	            }else if(Player1[0] == -1)
	            {
	                Player1[0] = playerid;
	            }else if(Player2[0] == -1)
	            {
	                Player2[0] = playerid;
				}else if(Player3[0] == -1)
	            {
	                Player3[0] = playerid;
				}else if(Player4[0] == -1)
	            {
	                Player4[0] = playerid;
				}else if(Player5[0] == -1)
	            {
	                Player5[0] = playerid;
				}else if(Player6[0] == -1)
	            {
	                Player6[0] = playerid;
				}else if(Player7[0] == -1)
	            {
	                Player7[0] = playerid;
				}else
{
//no slots info
}
Reply
#2

You would do it with an array:
pawn Код:
new EventPlayer[7];
// set it to -1 under OnGameModeInit or somewhere else
for(new i; i < 7; i++)
    EventPlayer[i] = -1;

// joining the event
new slot = -1;
for(new i; i < 7; i++)
{
    if(EventPlayer[i] == -1)
    {
        slot = i;
        break;
    }
}
if(slot == -1) return SendClientMessage(playerid, -1, "There are no empty slots.");
EventPlayer[slot] = playerid;
If you are not familiar with the for loops, read about them here:
https://sampwiki.blast.hk/wiki/Loops
Reply
#3

I cant really get it right now.

Here if we cant join?
Код:
new slot = -1;
for(new i; i < 7; i++)
{
    if(EventPlayer[i] == -1)
    {
        slot = i;
        break;
    }
}
and under this if there are available slots and we are joined?
Код:
if(slot == -1)
im showin there textdraws and many others, kinda confused right now about correct workin of this stuff

this code is with no sense to me
Reply
#4

Okay, so you have an array index. It's a value from 0 to 6 because you have seven fields in your array.
You want to get an index, a place in the array, which is empty, which means no player ID is stored there.
That's why you need to set slot to -1 because that is an invalid index. Go through the array and check if some value is equal to -1, which means it's empty and assign that field to the slot variable.
After the loop you want to see if you've got an empty slot. If not, player won't be able to join the event.

If you would do this:
new slot; // slot is by default zero
and you go through the array and you don't find any empty slot, your slot variable would still be zero and you would override EventPlayer[0] to current player's ID.
Reply
#5

u helped me a lot, thanks. catch some rep!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)