PDA

View Full Version : Server 'freezes' when using a specific command


Giovanni
31/12/2011, 07:12 PM
I have created a command to unlock a vehicle. Here is what I got so far:


CMD:unlock(playerid,params[])
{
new Float:vx,Float:vy,Float:vz;

foreach(SpawnedVehicle,i)
{
GetVehiclePos(VehicleInfo[i][vID],vx,vy,vz);

if(IsPlayerInRangeOfPoint(playerid,4.0,vx,vy,vz))
{
if(VehicleInfo[i][vHasOwner] == true && strcmp(VehicleInfo[i][vOwner],PName(playerid),false) == 0)
{

if(VehicleInfo[i][vLocked] == 1)
{
VehicleInfo[i][vLocked] = 0;
SetVehicleParamsEx(VehicleInfo[i][vID],MotorOn[i],0,0,VehicleInfo[i][vLocked],0,0,0);
GameTextForPlayer(playerid,"~w~Vehicle: ~g~Unlocked",2500,3);
}else{
SendError(playerid,"This vehicle is already unlocked.");
}
}
}
}
return 1;
}


I am using y_iteratek (newest version) and the iterators are getting filled up properly. But after I use this command the server freezes. That means I can not use any commands, or restart the server (over the console). I guess that's because it has to loop through all vehicles (MAX_VEHICLES), but I don't really know how to do it, so that it works.

(And I am pretty sure that this command causes the error, since the server only freezes when using it)

Dark_Kostas
31/12/2011, 08:41 PM
CMD:unlock(playerid,params[])
{
new Float:vx,Float:vy,Float:vz;

foreach(SpawnedVehicle,i)
{
printf("Looping at /unlock cmd. i = %d", i);

GetVehiclePos(VehicleInfo[i][vID],vx,vy,vz);

if(IsPlayerInRangeOfPoint(playerid,4.0,vx,vy,vz))
{
if(VehicleInfo[i][vHasOwner] == true && strcmp(VehicleInfo[i][vOwner],PName(playerid),false) == 0)
{

if(VehicleInfo[i][vLocked] == 1)
{
VehicleInfo[i][vLocked] = 0;
SetVehicleParamsEx(VehicleInfo[i][vID],MotorOn[i],0,0,VehicleInfo[i][vLocked],0,0,0);
GameTextForPlayer(playerid,"~w~Vehicle: ~g~Unlocked",2500,3);
}else{
SendError(playerid,"This vehicle is already unlocked.");
}
return 1;
}
}
}
SendError(playerid,"You are not close to any vehicle.");
return 1;
}


Type /unlock and check console what happens with the "i" value. I think the problem is an endless loop that's why take a look what values "i" get.

BTW I also added you a "You are not close to any vehicle" message in case you want it.

Giovanni
31/12/2011, 09:30 PM
Yes, it is indeed an endless loop. But how to stop that? Shall I add a break somewhere or what? And in the iterator are only 2 elements, I checked that with another printf.

Dark_Kostas
31/12/2011, 09:44 PM
How do you create and then add to the iterator? And is the "i" going over MAX_VEHICLES(2000) value when using printf?

Giovanni
31/12/2011, 09:49 PM
How do you create and then add to the iterator? And is the "i" going over MAX_VEHICLES(2000) value when using printf?

It always stays on 0, that's what I don't understand.

This is how I add to it:


for(new idx = 0; idx < sizeof(VehicleInfo) ; idx++)
{
if(fexist(VehicleFile(idx)))
{
INI_ParseFile(VehicleFile(idx), "LoadVehicle_%s", .bExtra = true, .extra = idx);

VehicleInfo[idx][vID] = CreateVehicle( ... );
SetVehicleParamsEx(VehicleInfo[idx][vID],0,0,0,VehicleInfo[idx][vLocked],0,0,0);
Iter_Add(SpawnedVehicle,VehicleInfo[idx][vID]);
printf("[log] Spawned Vehicles: %d",Iter_Count(SpawnedVehicle));
}
}

(@ OnGameModeInit)


new Iterator:SpawnedVehicle<MAX_VEHICLES>;



new VehicleInfo[MAX_VEHICLES][E_VEHICLE_DATA];






Note: When I use a normal for-loop for the unlock command it works, but foreach is faster so I prefere using it.

iPeanut
31/12/2011, 10:05 PM
um....what kostas said

Kar
31/12/2011, 10:50 PM
for(new idx = 0; idx < sizeof(VehicleInfo) ; idx++)
{
if(fexist(VehicleFile(idx)))
{
INI_ParseFile(VehicleFile(idx), "LoadVehicle_%s", .bExtra = true, .extra = idx);

VehicleInfo[idx][vID] = CreateVehicle( ... );
SetVehicleParamsEx(VehicleInfo[idx][vID],0,0,0,VehicleInfo[idx][vLocked],0,0,0);
if(!Iter_IsIn(SpawnedVehicle, VehicleInfo[idx][vID]))
{
Iter_Add(SpawnedVehicle,VehicleInfo[idx][vID]);
}
printf("[log] Spawned Vehicles: %d",Iter_Count(SpawnedVehicle));
}
}

//

#define Iter_IsIn(%0,%1) Iter_IsInInternal(YSI_gS%0,YSI_gA%0,(%1))
#define Itter_IsIn Iter_IsIn // Because I got the spelling of iterator wrong originally.

stock Iter_IsInInternal(start, array[], value)
{
while (start != -1)
{
if (start == value)
{
return true;
}
start = array[start];
}
return false;
}

Giovanni
31/12/2011, 11:30 PM
When I use your example code I get following error:

undefined symbol "YSI_gSSpawnedVehicle"

But the Iterator is defined, if I understood the error right. (The function is used after I create the Iterator)