Find empty slot in a two dimensional array
#1

Hey everyone.
I want to find the next empty slot in a two dimensional array. The function from the samp website does not work for me - i get a argument type mismatch (argument 1).

Here's the code:
Код:
new EmptySlot = FindEmptySlot(ClanInfo); // ClanInfo is my array
Код:
stock FindEmptySlot(array)
{
	new
		i = 0;
	while (i < sizeof (array) && array[i])
	{
		i++;
	}
	if (i == sizeof (array)) return -1;
	return i;
}
Reply
#2

Код:
new EmptySlot = FindEmptySlot(ClanInfo[clanid]); // ClanInfo is my array

stock FindEmptySlot(array[], size=sizeof(array))
{
	new
		i = 0;
	while (i < size && array[i])
	{
		i++;
	}
	if (i == size) return -1;
	return i;
}
Is this what you need?
Reply
#3

Nearly, I think.
But I still do not know how to find the empty slot properly.

Look - this is what I have.

My clan system is divided in two parts. This is the part, in which i want to save things such as clan name, clan stats, and so on. My MAX_CLANS is 20 at the moment.
Код:
enum ClanDaten
{
	cID,
	cName
}
new ClanInfo[MAX_CLANS][ClanDaten];
But know I want to find the next empty slot of MAX_CLANS. This is what I have...
Код:
stock CreateClan(playerid, clanname[])
{
    if(IsPlayerConnected(playerid) && !IsPlayerNPC(playerid))
    {
	new EmptySlot = FindEmptySlot(ClanInfo[HERE IS THE PROBLEM]);
        ClanInfo[EmptySlot][cID] = EmptySlot;
        ClanInfo[EmptySlot][cName] = clanname;
    }
}
I don't know what to write in FindEmptySlot(ClanInfo[HERE]). If I leave it blank or use a variable I get a error 029: invalid expression, assumed zero and a error 006: must be assigned to an array....
Reply
#4

In that case you don't need cID, because its value is the same as ClanInfo array index. Also, cName should be an array, because only arrays can hold text. I suggest doing in this way:
Код:
enum ClanDaten
{
	bool:cAvailable=true,
	cName[20]
}
new ClanInfo[MAX_CLANS][ClanDaten];

stock CreateClan(playerid, clanname[])
{
    if(IsPlayerConnected(playerid) && !IsPlayerNPC(playerid))
    {
	new EmptySlot = FindEmptySlot();
        ClanInfo[EmptySlot][cAvailable] = false;
        format(ClanInfo[EmptySlot][cName], sizeof ClanInfo[EmptySlot][cName], clanname);
    }
}

stock FindEmptySlot()
{
	new
		i = 0;
	while (i <= MAX_CLANS)
	{
                if(!ClanInfo[i][cAvailable])
		    i++;
                else break;
	}
	if (i == MAX_CLANS) return -1;
	return i;
}
Reply
#5

Yes, good idea.
But it still does not work. The mistake seems to be in the FindEmptySlot Function...
Код:
stock CreateClan(playerid, clanname[])
{
	print("debug 1");
    if(IsPlayerConnected(playerid) && !IsPlayerNPC(playerid))
    {
        print("debug 2");
		new EmptySlot = FindEmptySlot();
		print("debug 3");
        ClanInfo[EmptySlot][cAvailable] = false;
        print("debug 4");
        format(ClanInfo[EmptySlot][cName], MAX_CLAN_NAME_LENGTH, "%s", clanname);
        print("debug 5");
        
        printf("EmptySlot: %d || Name: %s", EmptySlot, ClanInfo[EmptySlot][cName]);
    }
    return 1;
}
Debug 2 gets printed - Debug 3 does not....the function breaks an returns a SERVER: UNKNOWN COMMAND....I used exactly your FindEmptySlot()...
Reply
#6

Код:
while (i <= MAX_CLANS)
to
Код:
while (i < MAX_CLANS)
Reply
#7

Thank you - but there's already the next thing....
Now he stops after debug 3.
Код:
ClanInfo[EmptySlot][cAvailable] = false;
Here is a mistake - but I do not find it.

/edit: i know why - FindEmptySlot returns -1...but this shouldn't be, because there are empty slots in the array.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)