Author Topic: Tracking a vehicle and remove it on command  (Read 1694 times)

Csompos

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Tracking a vehicle and remove it on command
« on: January 04, 2016, 12:33:54 pm »
Hi!

I want to create a script which limits the number of spawned vehicles per player to two and removes the firstly spawned one, if the player spawns another one. I "think" I'm on the half way to finish it, but I stucked at the most important point.

My question is the following:
Is it possible to track a player-spawned vehicle and remove it on command? Even if the player is not in the vehicle.

I'm a newbie to Squirrel. If someone can help me with this, it would be awesome. If not, then I'll have to give up for a while with this. :P

Here's the script so far, which may helps you understand what I want to do.

Code: [Select]
local vh1 = 0;
local vh2 = 0;


function onPlayerCommand( pPlayer, szCmd, szParams )
{
if ( szCmd == "spawncar" )
{
if ( szParams )
{
local pTemp = split( szParams, " " ), ID = 90;
if ( IsNum( pTemp[ 0 ] ) ) ID = pTemp[ 0 ].tointeger();

if ( ( ID >= 90 ) && ( ID <= 150 ) )
{


if ( ( vh1 == 0 ) && ( vh2 == 0 ) )
{
local v = pPlayer.Pos;
MessagePlayer( "Spawning a vehicle with model ID " + ID + "...", pPlayer );

local vh01 = CreateVehicle( ID, Vector( v.x + 5, v.y, v.z ), pPlayer.Angle );
print( pPlayer.Name + ": vh01 " + vh01.Pos );

vh01.OneTime = true;

vh1 = 1;

print( pPlayer.Name + ": vh1 = 1, vh2 = 0" );
}

else if ( ( vh1 == 1 ) && ( vh2 == 0 ) )
{
local v = pPlayer.Pos;
MessagePlayer( "Spawning a vehicle with model ID " + ID + "...", pPlayer );

local vh02 = CreateVehicle( ID, Vector( v.x + 5, v.y, v.z ), pPlayer.Angle );
print( pPlayer.Name + ": vh02 " + vh02.Pos );

vh02.OneTime = true;

vh2 = 2;

print( pPlayer.Name + ": vh1 = 1, vh2 = 2" );
}

else if ( ( vh1 == 1 ) && ( vh2 == 2 ) )
{
local v = pPlayer.Pos;
MessagePlayer( "Spawning a vehicle with model ID " + ID + "...", pPlayer );

local vh01 = CreateVehicle( ID, Vector( v.x + 5, v.y, v.z ), pPlayer.Angle );
print( pPlayer.Name + ": vh01 " + vh01.Pos );

vh01.OneTime = true;

vh2 = 1;
vh1 = 2;

print( pPlayer.Name + ": vh1 = 2, vh2 = 1" );
}

else if ( ( vh1 == 2 ) && ( vh2 == 1 ) )
{
local v = pPlayer.Pos;
MessagePlayer( "Spawning a vehicle with model ID " + ID + "...", pPlayer );

local vh02 = CreateVehicle( ID, Vector( v.x + 5, v.y, v.z ), pPlayer.Angle );
print( pPlayer.Name + ": vh02 " + vh02.Pos );

vh02.OneTime = true;

vh2 = 2;
vh1 = 1;

print( pPlayer.Name + ": vh1 = 1, vh2 = 2" );
}
}
}
}
return 1;
}




Ankris

  • Full Member
  • ***
  • Posts: 110
  • Karma: +29/-44
    • View Profile
Re: Tracking a vehicle and remove it on command
« Reply #1 on: January 04, 2016, 11:22:31 pm »
Code: [Select]
const MAX_VEHICLES_PER_PLAYER = 3;
const INVALID_ID = -1;

banned_vehicles <- [122, 150, /* add your banned vehicles here*/];
vehicles_spawned <- array(GetMaxPlayers(), array(MAX_VEHICLES_PER_PLAYER, INVALID_ID));
vehicles_spawnedcount <- array(GetMaxPlayers(), 0);

function onPlayerCommand(player, command, text) {
switch(command.tolower()) {
case "spawncar": case "v": case "vehicle": case "veh": case "car": {
if (!text || !IsNum(text)) return MessagePlayer("Invalid ID. Usage: /"+command+" <90-150>", player);

text = text.tointeger();

if (text < 90 || text > 150) return MessagePlayer("Invalid ID. Usage: /"+command+" <90-150>", player);

foreach(idx, val in banned_vehicles) {
if (val == text) return MessagePlayer("This vehicle is currently banned", player, Colour(255, 0, 0));
}

local vehicle = CreateVehicle(text, Vector(player.Pos.x, player.Pos.y + 5, player.Pos.z), player.Angle);

vehicle.OneTime = true;

local id = vehicles_spawned[player.ID];
if (vehicles_spawnedcount[player.ID] == 0) id = vehicles_spawned[player.ID].len() - 1;

local tmp = FindVehicle(id);
if (tmp) tmp.Remove();

vehicles_spawned[player.ID][vehicles_spawnedcount[player.ID]] = vehicle.ID;
vehicles_spawnedcount[player.ID]++;

break;
}
}

MessagePlayer("invalid cmd", player, Colour(255, 0, 0));
}

function onPlayerPart(player, reason) {
foreach(idx, val in vehicles_spawned[player.ID]) {
vehicles_spawned[player.ID][idx] = INVALID_ID;
}

vehicles_spawnedcount[player.ID] = 0;
}

not tested.

Csompos

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Tracking a vehicle and remove it on command
« Reply #2 on: January 05, 2016, 12:50:23 am »
Thank you for you help. I tested it:

Error on line 28 -> local tmp = FindVehicle(id);

Code: [Select]

AN ERROR HAS OCCURED [parameter 2 has an invalid type 'instance' ; expected: 'in
teger|float|bool|string']

CALLSTACK
*FUNCTION [onPlayerCommand()] Scripts/Limit/Main.nut line [28]

LOCALS
[id] 2
[vehicle] INSTANCE
[text] 90
[command] "spawncar"
[player] INSTANCE
[this] TABLE


Ankris

  • Full Member
  • ***
  • Posts: 110
  • Karma: +29/-44
    • View Profile
Re: Tracking a vehicle and remove it on command
« Reply #3 on: January 05, 2016, 02:34:25 pm »
Code: [Select]
const MAX_VEHICLES_PER_PLAYER = 3;
const INVALID_ID = -1;

banned_vehicles <- [122, 150, /* add your banned vehicles here*/];
vehicles_spawned <- array(GetMaxPlayers(), array(MAX_VEHICLES_PER_PLAYER, INVALID_ID));
vehicles_spawnedcount <- array(GetMaxPlayers(), 0);

function onPlayerCommand(player, command, text) {
switch(command.tolower()) {
case "spawncar": case "v": case "vehicle": case "veh": case "car": {
if (!text || !IsNum(text)) return MessagePlayer("Invalid ID. Usage: /"+command+" <90-150>", player);

text = text.tointeger();

if (text < 90 || text > 150) return MessagePlayer("Invalid ID. Usage: /"+command+" <90-150>", player);

foreach(idx, val in banned_vehicles) {
if (val == text) return MessagePlayer("This vehicle is currently banned", player, Colour(255, 0, 0));
}

local vehicle = CreateVehicle(text, Vector(player.Pos.x, player.Pos.y + 5, player.Pos.z), player.Angle);

vehicle.OneTime = true;

local id = vehicles_spawned[player.ID][vehicles_spawnedcount[player.ID]];
if (vehicles_spawnedcount[player.ID] == 0) id = vehicles_spawned[player.ID].len() - 1;

local tmp = FindVehicle(id);
if (tmp) tmp.Remove();

vehicles_spawned[player.ID][vehicles_spawnedcount[player.ID]] = vehicle.ID;
vehicles_spawnedcount[player.ID]++;
if (vehicles_spawnedcount[player.ID] == MAX_VEHICLES_PER_PLAYER) vehicles_spawnedcount[player.ID] = 0;

break;
}
}

MessagePlayer("invalid cmd", player, Colour(255, 0, 0));
}

function onPlayerPart(player, reason) {
foreach(idx, val in vehicles_spawned[player.ID]) {
vehicles_spawned[player.ID][idx] = INVALID_ID;
}

vehicles_spawnedcount[player.ID] = 0;
}

Csompos

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Tracking a vehicle and remove it on command
« Reply #4 on: January 06, 2016, 06:28:56 am »
I tested it again, no more error messages. But it doesn't remove the vehicle :(

Ankris

  • Full Member
  • ***
  • Posts: 110
  • Karma: +29/-44
    • View Profile
Re: Tracking a vehicle and remove it on command
« Reply #5 on: January 06, 2016, 12:53:16 pm »
tested

Code: [Select]
const MAX_VEHICLES_PER_PLAYER = 3;
const INVALID_ID = -1;

banned_vehicles <- [122, 150, /* add your banned vehicles here*/];
vehicles_spawned <- array(GetMaxPlayers(), array(MAX_VEHICLES_PER_PLAYER, INVALID_ID));
vehicles_spawnedcount <- array(GetMaxPlayers(), 0);

function onPlayerCommand(player, command, text) {
switch(command.tolower()) {
case "spawncar": case "v": case "vehicle": case "veh": case "car": {
if (!text || !IsNum(text)) return MessagePlayer("Invalid ID. Usage: /"+command+" <90-150>", player);

text = text.tointeger();

if (text < 90 || text > 150) return MessagePlayer("Invalid ID. Usage: /"+command+" <90-150>", player);

foreach(idx, val in banned_vehicles) {
if (val == text) return MessagePlayer("This vehicle is currently banned", player, Colour(255, 0, 0));
}

local vehicle = CreateVehicle(text, Vector(player.Pos.x, player.Pos.y + 5, player.Pos.z), player.Angle);

vehicle.OneTime = true;

local id = vehicles_spawned[player.ID][vehicles_spawnedcount[player.ID]];
local tmp = FindVehicle(id);
if (tmp) tmp.Remove();

vehicles_spawned[player.ID][vehicles_spawnedcount[player.ID]] = vehicle.ID;
vehicles_spawnedcount[player.ID]++;
if (vehicles_spawnedcount[player.ID] == MAX_VEHICLES_PER_PLAYER) vehicles_spawnedcount[player.ID] = 0;

return true;
}
}

MessagePlayer("invalid cmd", player, Colour(255, 0, 0));
}

function onPlayerPart(player, reason) {
foreach(idx, val in vehicles_spawned[player.ID]) {
vehicles_spawned[player.ID][idx] = INVALID_ID;
}

vehicles_spawnedcount[player.ID] = 0;
}

Csompos

  • Newbie
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Tracking a vehicle and remove it on command
« Reply #6 on: January 06, 2016, 08:39:26 pm »
It works like magic!  ;D Thank you very much for your help! 8)

 

© Liberty Unleashed Team.