Help with a little mathematics
#1

Supose you have the following array:

pawn Код:
PlayerCars[MAX_PLAYERS][4] = { 0,0,0,0 };
You buy a car and save to the array the modelid:

pawn Код:
for( new i; i<sizeof(PlayerCars[]); i++)
{
    if( PlayerCars[playerid][i] < 400 ) // Slot is empty
        PlayerCars[playerid][i] = GetVehicleModel( vehicleid );
}
Now you have PlayerCars[playerid][0] = modelof(vehicleid)

You go to the modshop and add a nitro to the car. Then save it's data to a file:

pawn Код:
for( new i; i<sizeof(PlayerCars[]); i++)
{
    if( GetVehicleModel(vehicleid) == PlayerCars[playerid][i] )
    {
        // Some function, just suppose it works like this
        Save( "file.txt", i, "modelid:", PlayerCars[playerid][i] );
        Save( "file.txt", i, "nitro:", GetVehicleComponentInSlot( GetPlayerVehicleID(playerid) );
        // You are saving the nitro to the corresponding slot of the car in the array
    }
}
So now you have in "file.txt":

Код:
slot0: modelid: 506 nitro: 1008 // a 2x nitro for car in slot 0 inside PlayerCars
slot1:
slot2:
slot3:
Now you buy two more cars and add some nitros to them, so they occuppy slot1 and slot2. You sell car in slot1 so now only slot0 and slot2 cotain some data.

"file.txt" now looks like this:

Код:
slot0: modelid: 506 nitro: 1008
slot1:
slot2: modelid: 400 nitro: 1008
slot3:
Now I try to create a menu to only show the cars i have:

pawn Код:
new string[24];
   
new Menu:mycarsmenu = CreateMenu("Your cars", 2, 2, 150.0, 100.0, 20.0);   

for( new i=0; i<sizeof(PlayerCars[]); i++ )
{
    if( PlayerCars[playerid][i] >= 400 )
    {      
        format(string,sizeof(string),"%s", GetVehicleNameByModel(PlayerCars[playerid][i]) );   
        AddMenuItem( mycarsmenu, 0, string);
    }
}
This will show in the menu:

Код:
Landstalker
Super GT
So, what's the problem?

The problem is that Landstalker corresponds to row 0 in the menu ( ok, no problem ), but Super Gt corresponds to slot 1 in the menu and slot 3
in PlayerCars.

If I select "Super Gt", im selecting a car that has no data stored for that slot inside the file, so nothing is loaded.

I need help figuring out how to list only the cars i have inside a menu and when I selected them to correctly load the data stored in the file.

I don't know i explained myself well.
Reply
#2

looks like a memory leak. one (not so simple, but working) solution:
when you delete (sell) a car (assuming slot [1], then copy the LAST vehicle parameter into that slot.
Код:
[0=landstalker] [1=cheetah] [2=supergt]
sell the [1cheetah], so it becomes clear first (your correct step so far):
Код:
[0=landstalker] [1] [2=supergt]
now its time to fill the empty [1] with the last [2=supergt] to avoid the gap. after copying [2] to [1]...
Код:
[0=landstalker] [1=supergt] [2=supergt]
...delete the [2]:
Код:
[0=landstalker] [1=supergt] [2]
you need to script it for each player car entry, like mods, color etc yourself, but i hope the concept is clear
Код:
PlayerCars[playerid][1]=PlayerCars[playerid][2];
PlayerCars[playerid][2]=0;
advantage: when you buy a car, you may simply add it to the first unused entry. all cars gets rearranged (gap avoid) by copying the last entry to a deleted one.
DISadvantage: the cars' order may get messed up! (only their order, the system will still work)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)