Bool value
#1

it should spawn/unspawn vehicle but it spawn only.

Код:
new bool:vehspawned[MAX_PLAYERS];

                    if(vehspawned[playerid] == false)
                    {
	                    vehspawned[playerid] = true;
						GetXYInFrontOfPlayer(playerid, x, y, 10.0);
						GetPlayerPos(playerid, x2, y2, z);
						GetPlayerFacingAngle(playerid, angle);
						CreateVehicle(Veh1[playerid], x, y, z, angle+90, 0, 0, -1);
					}else
					{
					    vehspawned[playerid] = false;
					    DestroyVehicle(Veh1[playerid]);
					}
Reply
#2

In variable 'Veh1[playerid]' the model is stored, not vehicle ID.
pawn Код:
// on top
new vehid[MAX_PLAYERS];
// if veh's not spawned
vehid[playerid] = CreateVehicle(Veh1[playerid], x, y, z, angle+90, 0, 0, -1);
//else
DestroyVehicle(vehid[playerid]);
Reply
#3

still
Код:
new vehid[MAX_PLAYERS];

                    if(vehspawned[playerid] == false)
                    {
	                    vehspawned[playerid] = true;
						GetXYInFrontOfPlayer(playerid, x, y, 10.0);
						GetPlayerPos(playerid, x2, y2, z);
						GetPlayerFacingAngle(playerid, angle);
						vehid[playerid] = CreateVehicle(Veh1[playerid], x, y, z, angle+90, 0, 0, -1);
					}else
					{
					    vehspawned[playerid] = false;
					    DestroyVehicle(vehid[playerid]);
					}
Reply
#4

Function CreateVehicle() returns the new ID of newly created vehicle and that ID is stored into vehid[playerid]. So when we want to destroy the vehicle, we need to use its ID, so we do DestroyVehicle(vehid[playerid]);. I guess, there's something wrong maybe with boolean (vehspawned[MAX_PLAYERS]) or something else, but not with it.
Reply
#5

so whats wrong
Reply
#6

bump
Reply
#7

anyone?
Reply
#8

Lets say 'vehspawned[playerid]' starts off as 'false'.
When it reaches 'if(vehspawned[playerid] == false)', the statement is true so it will continue and will do this:
'vehspawned[playerid] = true', so vehspawned[playerid] is now true.
Then it reaches the 'else' statement which is called if 'vehspawned[playerid] != false'. Seeing as vehspawned[playerid] is 'true' at this point and NOT false, the code under this will now execute, hence destroying everything that was just created.

The solution to this, is to use an 'else if' statement rather than an 'else' statement.

Example:
pawn Код:
//
                    if(!vehspawned[playerid])
                    {
                        vehspawned[playerid] = true;
                        GetXYInFrontOfPlayer(playerid, x, y, 10.0);
                        GetPlayerPos(playerid, x2, y2, z);
                        GetPlayerFacingAngle(playerid, angle);
                        CreateVehicle(Veh1[playerid], x, y, z, angle + 90, 0, 0, -1);
                    }
                    else if(vehspawned[playerid])
                    {
                        vehspawned[playerid] = false;
                        DestroyVehicle(Veh1[playerid]);
                    }
Or another way to do it is to set the variables outside the statements, like so:
pawn Код:
//
                    if(!vehspawned[playerid])
                    {
                        GetXYInFrontOfPlayer(playerid, x, y, 10.0);
                        GetPlayerPos(playerid, x2, y2, z);
                        GetPlayerFacingAngle(playerid, angle);
                        CreateVehicle(Veh1[playerid], x, y, z, angle + 90, 0, 0, -1);
                    }
                    else DestroyVehicle(Veh1[playerid]);
                    vehspawned[playerid] = (vehspawned[playerid]) ? (false) : (true);
Reply
#9

its not work, still only spawn without unspawn
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)