SA-MP Forums Archive
Per-player vehicles - Id's get mixed up (Car stealing problem) - 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: Per-player vehicles - Id's get mixed up (Car stealing problem) (/showthread.php?tid=578139)



Per-player vehicles - Id's get mixed up (Car stealing problem) - denNorske - 16.06.2015

Hello!

I have one global function to spawn 1 car per player online, and it looks like this:

PHP код:
stock SpawnPlayerVehicle(playeridvehicleidFloat:x,Float:y,Float:z,Float:anglecolor1313,color2323respawn = -1,putinvehicle)
{
    
#pragma unused respawn
    
if(IsValidVehicle(AccInfo[playerid][SpawnedCarID])) // If the player who gets a new car, had an old one spawned. If so, despawn it and reset the array.
    
{
         
printf("SpawnPlayerVehicle: Old spawned car ID = %d  BY Playerid = %d (Destroyed)",AccInfo[playerid][SpawnedCarID],playerid);
        
DestroyVehicle(AccInfo[playerid][SpawnedCarID]); //destroying it
        
AccInfo[playerid][SpawnedCarID] = -1;  //resetting array
    
}
    
AccInfo[playerid][SpawnedCarID] = CreateVehicle(vehicleidx,y,zanglecolor1313color2323240); //creating a new vehicle and assigns it to the SpawnedCarID again, so it can be destroyed when a new one is spawned.
     
printf("SpawnPlayerVehicle: New spawned car ID = %d  BY Playerid = %d (Created)",AccInfo[playerid][SpawnedCarID],playerid);
    
SetVehicleVirtualWorld(AccInfo[playerid][SpawnedCarID], GetPlayerVirtualWorld(playerid));
    
LinkVehicleToInterior(AccInfo[playerid][SpawnedCarID], GetPlayerInterior(playerid));
    new 
error;
    new 
string[180], string2[180];
    
AccInfo[playerid][onlinetime] = gettime() - AccInfo[playerid][logintime];
    foreach(
Playeri)
    {
        
error++;
        if(
AccInfo[i][SpawnedCarID] == AccInfo[playerid][SpawnedCarID] && != playerid// if the carID isn't unique, (that someone else already had that car in their array) - Print it and tell it in the log.
        
{
            
format(stringsizeof(string), "DEBUG VEHICLES: Old player: %s (ID %d) || New player: %s (ID %d) || SpawnedCarID Match"PlayerName(i), iPlayerName(playerid), playerid);
            
format(string2sizeof(string2), "Player Info: Gamemode: %d || OnlineTime: %d || Loggedin: %d || VW: %d || Int: %d "AccInfo[playerid][GameModeType],AccInfo[playerid][onlinetime], 
                
AccInfo[playerid][USER_LOGGED_IN], GetPlayerVirtualWorld(playerid), GetPlayerInterior(playerid));
        }
        else if(
PlayerInfo[i][CarID] == AccInfo[playerid][SpawnedCarID])
        {
            
format(stringsizeof(string), "DEBUG VEHICLES: newly spawned car was  a private car.|| Old owner: %s (ID %d) || New player: %s (ID %d)"PlayerName(i), iPlayerName(playerid), playerid);
            
format(string2sizeof(string2), "Player Info: Gamemode: %d || OnlineTime: %d || Loggedin: %d || VW: %d || Int: %d "AccInfo[playerid][GameModeType],AccInfo[playerid][onlinetime], 
                
AccInfo[playerid][USER_LOGGED_IN], GetPlayerVirtualWorld(playerid), GetPlayerInterior(playerid));
        }
        else if(
trailer[i] == AccInfo[playerid][SpawnedCarID])
        {
            
format(stringsizeof(string), "DEBUG VEHICLES: - new car was trailer.  Old owner: %s (ID %d)  || New player: %s (ID %d)"PlayerName(i), iPlayerName(playerid), playerid);
            
format(string2sizeof(string2), "Player Info: Gamemode: %d || OnlineTime: %d || Loggedin: %d || VW: %d || Int: %d "AccInfo[playerid][GameModeType],AccInfo[playerid][onlinetime], 
                
AccInfo[playerid][USER_LOGGED_IN], GetPlayerVirtualWorld(playerid), GetPlayerInterior(playerid));
        }
        if(
ToggleDebug[i] == 1)
        {
            
SendClientMessage(i, -1string);
            
SendClientMessage(i, -1string2);
        }
    }
    if(
error 0)
    {
        
printf(string);
        
printf(string2);
    }
    if(
putinvehicle == 1)
    {
        
PutPlayerInVehicle(playeridAccInfo[playerid][SpawnedCarID], 0);
    }
    return 
1;

But shortly after a server restart; this starts to happen:

Quote:

[01:17:00] SpawnPlayerVehicle: Old spawned car ID = 458 BY Playerid = 5 (Destroyed)
[01:17:00] ><>><><><<<<<>>< DestroyVehicle(458) >><<<<>><<><<<<<><>>
[01:17:00] CreateVehicle(473,-304.647155,1526.395996,75.359375,0.000000,0,1,240) = 458
[01:17:00] SpawnPlayerVehicle: New spawned car ID = 458 BY Playerid = 5 (Created)
[01:17:00] DEBUG VEHICLES: Old player: Veg5 (ID 1) || New player: XxXAgustin123XxX (ID 5) || SpawnedCarID Match
[01:17:00] Player Info: Gamemode: 0 || OnlineTime: 1685 || Loggedin: 1 || VW: 0 || Int: 0

[01:17:08] SpawnPlayerVehicle: Old spawned car ID = 458 BY Playerid = 5 (Destroyed)
[01:17:08] ><>><><><<<<<>>< DestroyVehicle(458) >><<<<>><<><<<<<><>>
[01:17:08] CreateVehicle(450,-305.435272,1522.001220,75.359375,0.000000,0,1,240) = 458
[01:17:08] SpawnPlayerVehicle: New spawned car ID = 458 BY Playerid = 5 (Created)
[01:17:08] DEBUG VEHICLES: Old player: Veg5 (ID 1) || New player: XxXAgustin123XxX (ID 5) || SpawnedCarID Match
[01:17:08] Player Info: Gamemode: 0 || OnlineTime: 1693 || Loggedin: 1 || VW: 0 || Int: 0

[01:17:22] SpawnPlayerVehicle: Old spawned car ID = 458 BY Playerid = 5 (Destroyed)
[01:17:22] ><>><><><<<<<>>< DestroyVehicle(458) >><<<<>><<><<<<<><>>
[01:17:22] CreateVehicle(463,-307.332458,1521.475219,75.359375,0.000000,0,1,240) = 458
[01:17:22] SpawnPlayerVehicle: New spawned car ID = 458 BY Playerid = 5 (Created)
[01:17:22] DEBUG VEHICLES: Old player: Veg5 (ID 1) || New player: XxXAgustin123XxX (ID 5) || SpawnedCarID Match
[01:17:22] Player Info: Gamemode: 0 || OnlineTime: 1707 || Loggedin: 1 || VW: 0 || Int: 0

[01:18:32] (Veg5 used /race)

[01:18:36] SpawnPlayerVehicle: Old spawned car ID = 458 BY Playerid = 1 (Destroyed)
[01:18:36] ><>><><><<<<<>>< DestroyVehicle(458) >><<<<>><<><<<<<><>>
[01:18:36] CreateVehicle(509,2253.867187,-1750.366455,14.100000,269.000000,-1,-1,240) = 458
[01:18:36] SpawnPlayerVehicle: New spawned car ID = 458 BY Playerid = 1 (Created)
[01:18:36] DEBUG VEHICLES: Old player: XxXAgustin123XxX (ID 5) || New player: Veg5 (ID 1) || SpawnedCarID Match
[01:18:36] Player Info: Gamemode: 0 || OnlineTime: 1756 || Loggedin: 1 || VW: 70 || Int: 0

Which indicates that the vehicle-ID that the new player spawned, was a vehicle-id that someone else had assigned as "theirs".
This causes the players to steal cars from eachother, and it's a very annoying problem.


What have i done wrong?
Is there anything else I have to do to prevent this?

~thanks


Re: Per-player vehicles - Id's get mixed up (Car stealing problem) - SickAttack - 17.06.2015

Just give them and make sure that they have a unique id.


Re: Per-player vehicles - Id's get mixed up (Car stealing problem) - denNorske - 17.06.2015

How can I prevent CreateVehicle to create one that is already taken? I can't do much about that part.
Also, that is part of what I'm tryin to achieve in first place, by assigning them in creation.
But it seems the id's mix up :/
Any examples? To prevent destroying old one if not unique?


Re: Per-player vehicles - Id's get mixed up (Car stealing problem) - SickAttack - 17.06.2015

Quote:
Originally Posted by denNorske
Посмотреть сообщение
How can I prevent CreateVehicle to create one that is already taken? I can't do much about that part.
Also, that is part of what I'm tryin to achieve in first place, by assigning them in creation.
But it seems the id's mix up :/
Any examples? To prevent destroying old one if not unique?
I don't think you have understood what "CreateVehicle" returns, there is no way a vehicle created with "CreateVehicle" could have (somehow) the same id. If someone's vehicle is destroyed and their vehicle respawned, then just refresh the player's variable containing the id of the new vehicle's id.


Re: Per-player vehicles - Id's get mixed up (Car stealing problem) - denNorske - 17.06.2015

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
I don't think you have understood what "CreateVehicle" returns, there is no single way a vehicle created with "CreateVehicle" could have somehow the same id. If someone's vehicle is destroyed and their vehicle respawned, then just refresh the player's variable containing the id to the new vehicle's id. They will never be the same.
A dead vehicle is still using the slot of that vehicle id -
And I know that CreateVehicle isn't going to return a used ID, so I need to prevent destroying the old car, in case it is used by someone else.

But why would a car, that someone spawned; suddenly have another owner? Just, w00t?
That is my problem.

Player 0 spawns a car, and have it saved in his "spawnedcarid".
Player 1 Spawns his car, destroys id 0's car, and creates id 0's "spawnedcarid". (Aka; car is stolen)

I can do it the way you say, but wasn't exactly what i was thinking in first place.


Re: Per-player vehicles - Id's get mixed up (Car stealing problem) - SickAttack - 17.06.2015

Quote:
Originally Posted by denNorske
Посмотреть сообщение
A dead vehicle is still using the slot of that vehicle id -
And I know that CreateVehicle isn't going to return a used ID, so I need to prevent destroying the old car, in case it is used by someone else.

But why would a car, that someone spawned; suddenly have another owner? Just, w00t?
That is my problem.

Player 0 spawns a car, and have it saved in his "spawnedcarid".
Player 1 Spawns his car, destroys id 0's car, and creates id 0's "spawnedcarid". (Aka; car is stolen)

I can do it the way you say, but wasn't exactly what i was thinking in first place.
Player 0 spawns a car, and have it saved in his "spawnedcarid".
We're all good for now.

Player 1 Spawns his car, destroys id 0's car, and creates id 0's "spawnedcarid". (Aka; car is stolen)
If player 1's vehicle is destroyed, then why don't you just reset their variable since their car is gone?


Re: Per-player vehicles - Id's get mixed up (Car stealing problem) - denNorske - 17.06.2015

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Player 0 spawns a car, and have it saved in his "spawnedcarid".
We're all good for now.

Player 1 Spawns his car, destroys id 0's car, and creates id 0's "spawnedcarid". (Aka; car is stolen)
If player 1's vehicle is destroyed, then why don't you reset their variable since their car is gone?
Hmm you got a point.

But why would that happen? When that is the only place I physically use "Destroyvehicle" on AccInfo[playerid][SpawnedCarID] ?

Or are there other places, in the server, that happens?