Link a carkey to a car
#1

Okay i was just wondering if i could link a key to a vehicle just by using the vehicles id or should i make a new value
for that.

So basicly ..would this work ?

pawn Code:
new Key[MAX_VEHICLES];

(somewhere in the script)

Key[myvehicle] = 23;//  (23 is just an example and in this case should also be the 23st vehicle id in the script)

if(Key[vehicleid] == vehicleid)
{
 //this should be your car
}
else
{
 //the vehicle id and the Key id wont match so its not your car.
}
Reply
#2

When most servers do it, they have a key variable on the player, that matches the car id.
Reply
#3

Thanks yea i actually meant Keys[MAX_PLAYERS]; instead of MAX_VEHICLES but anyways you cleared my doubt.
Reply
#4

This is for someone, so I won't share the code. Well, you can just say that I came to show it off, :





P.S. It's not made how you mentioned you are going to make it as you can have multiple vehicle keys and you can also duplicate vehicle keys at a business. You can also duplicate keys to businesses and businesses have keys too, well of course.

Anyway, in all means, there are different ways of accomplishing this. And good luck!
Reply
#5

i see..thats just different slots in a players variable so you can have more keys than one right ?
I dont think i will use that but yea its a good idea.
Reply
#6

Code:
new pVehicle[MAX_PLAYERS];

pVehicle[playerid] = CreateCar(...);

if(pVehicle[playerid] != GetPlayerVehicleID(playerid))
   return ... // do something here. i.g: return SendClientMessage(playerid, -1, "You do not have a key.");

/* if the script reaches this line, their pVehicle variable equals the vehicleid.
    you could turn the engine on/off now with GetVehicleParamsEx(...) and SetVehicleParamsEx(...)
*/
If you need further explaining, reply and I will answer.
Reply
#7

Lets say you want 3 keys for each vehicle. So we declare a 2D array with the last as the keys limit.

pawn Code:
new key[MAX_VEHICLES][3];
So whenever you create a vehicle, set the array to INVALID_PLAYER_ID which means no one has the key of the specific vehicle.
So you understand we are putting player ids in "key" variable.
pawn Code:
new i = CreateVehicle(..);

key[i][0] = INVALID_PLAYER_ID;
key[i][1] = INVALID_PLAYER_ID;
key[i][2] = INVALID_PLAYER_ID;

So if suppose a user "Gammix" id is "0", "AIped" id is 1 and "Third" id is 2.
Lets give id 0 and 1 i.e. "Gammix" and "AIped" the key for vehicle id 0.
pawn Code:
key[0][0] = 0;
key[0][1] = 1;
Here is a breakdown:
Code:
key[*][**] = ***;
* - is the vehicle id for which the keys will be given.
** - is the key slot, 0 means fist key and so on.
*** - is the player's id who will be having the key.

In order to check if the player has the key of vehicleid 0:
pawn Code:
for (new i; i < 3; i++)
{
    if (key[0][i] == playerid)
    {
        // player has the key
        break;
    }
}
Reply
#8

Quote:
Originally Posted by Gammix
View Post
Lets say you want 3 keys for each vehicle. So we declare a 2D array with the last as the keys limit.

pawn Code:
new key[MAX_VEHICLES][3];
So whenever you create a vehicle, set the array to INVALID_PLAYER_ID which means no one has the key of the specific vehicle.
So you understand we are putting player ids in "key" variable.
pawn Code:
new i = CreateVehicle(..);

key[i][0] = INVALID_PLAYER_ID;
key[i][1] = INVALID_PLAYER_ID;
key[i][2] = INVALID_PLAYER_ID;

So if suppose a user "Gammix" id is "0", "AIped" id is 1 and "Third" id is 2.
Lets give id 0 and 1 i.e. "Gammix" and "AIped" the key for vehicle id 0.
pawn Code:
key[0][0] = 0;
key[0][1] = 1;
Here is a breakdown:
Code:
key[*][**] = ***;
* - is the vehicle id for which the keys will be given.
** - is the key slot, 0 means fist key and so on.
*** - is the player's id who will be having the key.

In order to check if the player has the key of vehicleid 0:
pawn Code:
for (new i; i < 3; i++)
{
    if (key[0][i] == playerid)
    {
        // player has the key
        break;
    }
}
That's bad, there's no need for a loop. And what if I want to give 5,000 players the key to the vehicle? Then that would required an extra load on the connection of a player.
Reply
#9

Quote:
Originally Posted by SickAttack
View Post
That's bad, there's no need for a loop. And what if I want to give 5,000 players the key to the vehicle? Then that would required an extra load on the connection of a player.
5000 players isn't possible. And if you are talking about database, SQL can handle this in a few second(s).

And i guess how would you search key holders of a specific vehicle from a 5000 player database, to make it faster than a vehicle based key system?
Reply
#10

Quote:
Originally Posted by AIped
View Post
i see..thats just different slots in a players variable so you can have more keys than one right ?
I dont think i will use that but yea its a good idea.
All it will be is a Keys[CarID][MaxPlayers]

Try maybe 3-5 at first, it's not that hard. It's a 3 dimensional array or something.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)