SA-MP Forums Archive
Bool value - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Bool value (/showthread.php?tid=523959)



Bool value - cnoopers - 04.07.2014

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]);
					}



Re: Bool value - kamiliuxliuxliux - 04.07.2014

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]);



Re: Bool value - cnoopers - 04.07.2014

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]);
					}



Re: Bool value - kamiliuxliuxliux - 04.07.2014

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.


Re: Bool value - cnoopers - 04.07.2014

so whats wrong


Re: Bool value - cnoopers - 04.07.2014

bump


Re: Bool value - cnoopers - 05.07.2014

anyone?


Re: Bool value - Threshold - 05.07.2014

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);



Re: Bool value - cnoopers - 06.07.2014

its not work, still only spawn without unspawn