Create vehicles ingame
#1

hey all,

Could anyone explain me how a integer is saved when i use:

TaxiCar = AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);

because when i spawn vehicles like this in game, they sometimes get mixed up and it brings huge bugs to my code

thanks in advance!

Jop9888
Reply
#2

pawn Code:
TaxiCar =   AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar will hold the id of the created vehicle

so if this is the first vehicle to be created TaxiCar will = 1


so doing something like
pawn Code:
TaxiCar =   AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar =   AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar =   AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar =   AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
will alter the value each time
pawn Code:
TaxiCar =   AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar2 =  AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar3 =  AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar4 =  AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
this is more how it will be done.

or even use an array

pawn Code:
new TaxiCar[4];
TaxiCar[0] =    AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar[1] =    AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar[2] =    AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
TaxiCar[3] =    AddStaticVehicleEx(420,2044.8854,1473.2106,10.4494 ,181.3339,6,1,-1);
Reply
#3

But how does this explain they can get mixed up?
i often destroy one and create another and so on, is there any way to prevent them from mixing up?

i use for example:

pInfo[i][PlayerCar] = CreateVehicle(pInfo[i][CarModel], pInfo[i][CarX], pInfo[i][CarY], pInfo[i][CarZ], pInfo[i][AngX], pInf [i][CarColor1], pInfo[i][CarColor2], 60);

and define them to players, that they are only accesable for them, at first everything works fine,

but after a few times destroying and creating the script mixes them up
Reply
#4

when you destroy the vehicle then set

pInfo[i][PlayerCar] = 0;
Reply
#5

Gonna try that one out
Reply
#6

i should have explained it like

say you create 4 cars

car1 = id 1
car2 = id 2
car3 = id 3
car4 = id 4

and then you delete car3
now you create another car
its id will = 3 not 5.
thats the first free slot between 1 & MAX_VEHICLES
Reply
#7

CreateVehicle return the vehicleid. Thus, in the examples above, you save the vehicleid to a variable (car1,car2 etc).

To delete a vehicle, simply use DestroyVehicle (under OnVehicleDeath for example):

pawn Code:
#include <a_samp>

new car;

public OnGameModeInit()
{
    car = CreateVehicle(411,.......);
    return 1;
}

public OnVehicleDeath(vehicleid, killerid)
{
    if(vehicleid == car) // if the vehicle that blew up is "car"
    {
        DestroyVehicle(vehicleid); //delete the vehicle (so it won't respawn)
    }
    return 1;
}
Reply
#8

i tried this:

pInfo[i][PlayerCar] = 0;

when i destroy the vehicle, but still sometimes they get mixed up

could it be anything else??
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)