PDA

View Full Version : Recursion code


Parlianment
26/06/2015, 08:49 PM
All welcome. Recursion The compiler found in the code, need help fixing.

foreach(new x: Player)
{
if(!IsPlayerConnected(x)) continue;
if(GetPlayerState(x) == PLAYER_STATE_SPECTATING && gSpectateID[x] == playerid)
{
AdvanceSpectate(x);
}
}

The full code

stock StartSpectate(playerid, specid)
{
SetPVarInt(playerid,"PlayerSpec",1);
if(GetPlayerState(playerid) != PLAYER_STATE_SPECTATING)
{
ShowMenuForPlayer(OdminMeny,playerid);
GetPlayerPos(playerid, TeleportDest[playerid][0],TeleportDest[playerid][1],TeleportDest[playerid][2]);
TeleportDestNoFloat[playerid][0] = GetPlayerInterior(playerid);
TeleportDestNoFloat[playerid][1] = GetPlayerVirtualWorld(playerid);
}
foreach(new x: Player)
{
if(!IsPlayerConnected(x)) continue;
if(GetPlayerState(x) == PLAYER_STATE_SPECTATING && gSpectateID[x] == playerid)
{
AdvanceSpectate(x);
}
}
if(IsPlayerInAnyVehicle(specid))
{
SetPlayerInterior(playerid,GetPlayerInterior(speci d));
SetPlayerVirtualWorld(playerid, GetPlayerVirtualWorld(specid));
TogglePlayerSpectating(playerid, 1);
PlayerSpectateVehicle(playerid, GetPlayerVehicleID(specid));
gSpectateID[playerid] = specid;
gSpectateType[playerid] = ADMIN_SPEC_TYPE_VEHICLE;
}
else
{
SetPlayerInterior(playerid,GetPlayerInterior(speci d));
SetPlayerVirtualWorld(playerid, GetPlayerVirtualWorld(specid));
TogglePlayerSpectating(playerid, 1);
PlayerSpectatePlayer(playerid, specid);
gSpectateID[playerid] = specid;
gSpectateType[playerid] = ADMIN_SPEC_TYPE_PLAYER;
}
SpecAd[playerid] = specid;
SpecID[specid] = playerid;
PlayerTextDrawShow(playerid, FULLRECON[playerid]);
return true;
}

Nero_3D
26/06/2015, 10:08 PM
How should we help you ?

If you compiler found a recursion than AdvanceSpectate is probably calling StartSpectate

Parlianment
26/06/2015, 10:12 PM
How should we help you ?

If you compiler found a recursion than AdvanceSpectate is probably calling StartSpectate

stock AdvanceSpectate(playerid)
{
if(ConnectedPlayers() == 2)
{
StopSpectate(playerid);
return true;
}
if(GetPlayerState(playerid) == PLAYER_STATE_SPECTATING && gSpectateID[playerid] != INVALID_PLAYER_ID)
{
for(new x = gSpectateID[playerid]+1; x <= MAX_PLAYERS; x++)
{
if(x == MAX_PLAYERS)
{
x = 0;
}
if(IsPlayerConnected(x) && x != playerid)
{
if(GetPlayerState(x) == PLAYER_STATE_SPECTATING && gSpectateID[x] != INVALID_PLAYER_ID ||
(GetPlayerState(x) != 1 && GetPlayerState(x) != 2 && GetPlayerState(x) != 3))
{
continue;
}
else
{
StartSpectate(playerid, x);
break;
}
}
}
}
return true;
}

Nero_3D
27/06/2015, 01:28 AM
I am still not sure what you actually want, if you simply want to avoid the recursion you can split the StartSpectate function into a Start function and into a SpectateTarget function


stock StartSpectate(playerid, specid)
{
if(GetPlayerState(playerid) == PLAYER_STATE_SPECTATING || !SpectateTarget(playerid, specid))
{
return false;
}
SetPVarInt(playerid,"PlayerSpec",1);
ShowMenuForPlayer(OdminMeny, playerid);
GetPlayerPos(playerid, TeleportDest[playerid][0], TeleportDest[playerid][1], TeleportDest[playerid][2]);
TeleportDestNoFloat[playerid][0] = GetPlayerInterior(playerid);
TeleportDestNoFloat[playerid][1] = GetPlayerVirtualWorld(playerid);

foreach(new x: Player)
{
if(gSpectateID[x] == playerid)
{
AdvanceSpectate(x);
}
}
PlayerTextDrawShow(playerid, FULLRECON[playerid]);
return true;
}

stock SpectateTarget(playerid, specid)
{
if(1 <= GetPlayerState(specid) <= 3)
{
TogglePlayerSpectating(playerid, 1);
SetPlayerInterior(playerid,GetPlayerInterior(speci d));
SetPlayerVirtualWorld(playerid, GetPlayerVirtualWorld(specid));

if(IsPlayerInAnyVehicle(specid))
{
PlayerSpectateVehicle(playerid, GetPlayerVehicleID(specid));
gSpectateType[playerid] = ADMIN_SPEC_TYPE_VEHICLE;
}
else
{
PlayerSpectatePlayer(playerid, specid);
gSpectateType[playerid] = ADMIN_SPEC_TYPE_PLAYER;
}
gSpectateID[playerid] = specid;
// no clue what these two lines \/ does
SpecAd[playerid] = specid;
SpecID[specid] = playerid;
return true;
}
return false;
}

stock AdvanceSpectate(playerid)
{
if(GetPlayerState(playerid) == PLAYER_STATE_SPECTATING)
{
for(new x = gSpectateID[playerid] + 1, y = MAX_PLAYERS; y--; x++)
{
if(x == MAX_PLAYERS)
{
x = 0;
}
if(x != playerid && SpectateTarget(playerid, x))
{
return true;
}
} // Noone found
StopSpectate(playerid);
}
return false;
}

Parlianment
27/06/2015, 10:10 AM
I am still not sure what you actually want, if you simply want to avoid the recursion you can split the StartSpectate function into a Start function and into a SpectateTarget function


stock StartSpectate(playerid, specid)
{
if(GetPlayerState(playerid) == PLAYER_STATE_SPECTATING || !SpectateTarget(playerid, specid))
{
return false;
}
SetPVarInt(playerid,"PlayerSpec",1);
ShowMenuForPlayer(OdminMeny, playerid);
GetPlayerPos(playerid, TeleportDest[playerid][0], TeleportDest[playerid][1], TeleportDest[playerid][2]);
TeleportDestNoFloat[playerid][0] = GetPlayerInterior(playerid);
TeleportDestNoFloat[playerid][1] = GetPlayerVirtualWorld(playerid);

foreach(new x: Player)
{
if(gSpectateID[x] == playerid)
{
AdvanceSpectate(x);
}
}
PlayerTextDrawShow(playerid, FULLRECON[playerid]);
return true;
}

stock SpectateTarget(playerid, specid)
{
if(1 <= GetPlayerState(specid) <= 3)
{
TogglePlayerSpectating(playerid, 1);
SetPlayerInterior(playerid,GetPlayerInterior(speci d));
SetPlayerVirtualWorld(playerid, GetPlayerVirtualWorld(specid));

if(IsPlayerInAnyVehicle(specid))
{
PlayerSpectateVehicle(playerid, GetPlayerVehicleID(specid));
gSpectateType[playerid] = ADMIN_SPEC_TYPE_VEHICLE;
}
else
{
PlayerSpectatePlayer(playerid, specid);
gSpectateType[playerid] = ADMIN_SPEC_TYPE_PLAYER;
}
gSpectateID[playerid] = specid;
// no clue what these two lines \/ does
SpecAd[playerid] = specid;
SpecID[specid] = playerid;
return true;
}
return false;
}

stock AdvanceSpectate(playerid)
{
if(GetPlayerState(playerid) == PLAYER_STATE_SPECTATING)
{
for(new x = gSpectateID[playerid] + 1, y = MAX_PLAYERS; y--; x++)
{
if(x == MAX_PLAYERS)
{
x = 0;
}
if(x != playerid && SpectateTarget(playerid, x))
{
return true;
}
} // Noone found
StopSpectate(playerid);
}
return false;
}

Thanks :) Closed theme.