Car Spawning

Started by aadb, Jan 25, 2015, 02:39 PM

Previous topic - Next topic

aadb

I've seen to get a strange problem when spawning a car by just using the /spawncar or my modifiied /v command.

Underneath is the code which has been used in several default script.
else if ( cmd == "v" )
    {
         if ( text )
{
local veh = FindVehicle( text.tointeger() );
veh.Pos = Vector( ( player.Pos.x + 2 ), player.Pos.y, ( player.Pos.z ) );
     MessagePlayer( "[#ffffff][INFO][#00ff00]---> You have spawned car ID : [ " + veh.ID + " ).", player );
}
else MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v <car id >.", player );
     return 1;
    }

For some reason spawning a car with ID 111 doesnt spawn the car that has ID 111 it just spawns randomly and not even all models sometimes 111 to 120 seem to be the same spawned car.

Anyone know's how to solve this?

Thanks in advance

DizzasTeR

Weired, the command seems to be okay... wait for someone else to test it using his very own command (those who made one for themselves). If they have this issue as well it will be a bug otherwise we are not catching something.

(I can't test it coz i am on phone :p)

EK.IceFlake

How is text identified? write the code
example onPlayerCommand(command, text); or local text = [?];

.

#3
That's because you are using FindVehicle() which searches for an already created vehicle. And if the player doesn't create 111 vehicles then there's no vehicle instance with the ID 111. If the player indeed created more than 111 vehicle then you will simple get the 111'th created vehicle that was assigned the ID 111 (or whatever number you request). You must use CreateVehicle() instead. I believe that was your intention. Also, vehicle IDs start from 130+ AFAIK

You people make it so easy for someone to break your code :-\

EDIT:

I believe this is what you wanted to achieve:
// ------------------------------------------------------------------------------------------------
_CMD <- {
function V(player, args)
{
if (args) args = ::strip(args);

if (!args) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] Missing arguments.", player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
} else {
local ex = ::regexp(@"\d+");
if (!ex.match(args)) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] Invalid arguments.", player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
}
}

local veh_id = null;

try {
veh_id = args.tointeger();
} catch (e) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] " + e, player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
}

local vehicle = ::CreateVehicle(veh_id, player.World, player.Pos + Vector(4, 2, 4), 0.0, 0x00FF00FF, 0x0000FFFF);

if (vehicle == null) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] Unable to create vehicle.", player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
}

::MessagePlayer( "[#ffffff][INFO][#00ff00] You have spawned car ID : [ " + vehicle.ID + " ).", player );
}
}

// ------------------------------------------------------------------------------------------------
function onPlayerCommand(player, cmd, args)
{
switch (cmd)
{
// ----------------------------------------------------------------------------------------
case "v": _CMD.V(player, args); break;
}
}

If you want to use the old code then here you go:
// ------------------------------------------------------------------------------------------------
_CMD <- {
function V(player, args)
{
if (args) args = ::strip(args);

if (!args) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] Missing arguments.", player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
} else {
local ex = ::regexp(@"\d+");
if (!ex.match(args)) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] Invalid arguments.", player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
}
}

local veh = null;

try {
veh = args.tointeger();
} catch (e) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] " + e, player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
}

veh = ::FindVehicle(veh);

if (!veh) {
::MessagePlayer( "[#ffffff][ERROR][#00ff00] Unknown vehicle.", player );
::MessagePlayer( "[#ffffff][INFO][#00ff00] Type /v < car id >.", player );
return;
}

veh.Pos = player.Pos + Vector(4, 2, 4);

::MessagePlayer( "[#ffffff][INFO][#00ff00] You have spawned car ID : [ " + veh.ID + " ).", player );
}
}

// ------------------------------------------------------------------------------------------------
function onPlayerCommand(player, cmd, args)
{
switch (cmd)
{
// ----------------------------------------------------------------------------------------
case "v": _CMD.V(player, args); break;
}
}
.

Thijn

@S.L.C. The regex is probably (Not tested) taking longer then the functin IsNum provided by the squirrel module.

More pointed towards aadb: "Type /v < car id >" Car id probably isn't the right word. I would say it's a model id.

.

#5
Quote from: Thijn on Jan 25, 2015, 10:48 PM@S.L.C. The regex is probably (Not tested) taking longer then the functin IsNum provided by the squirrel module.

Ahh, crap. I always forget about the IsNum function :D Although in this case the performance impact is insignificant. If that were in an update function called frequently then yes it would make a difference. But in a command that gets called every couple of seconds/minutes I'm not even going to bother about it.
.

aadb

thanks for the input, hopefully i can put it in my script file in between all the other crap.

Cant seem to get it to work without having the server saying no gameload loaded due unkown stuff.

My script can be downloaded here:
http://www.4shared.com/file/WzONhqjYba/aad.html


.

Quote from: aadb on Jan 27, 2015, 06:41 PMCant seem to get it to work without having the server saying no gameload loaded due unkown stuff.

You need to edit server.cfg and add this line somewhere " sqgamemode your_script.nut ". I hope you realized that you have to replace "your_script.nut" with the name of your actual script.
.

aadb

offcourse i've already added a script load on the server.cfg and it seems to work but there is still a strange problem

typing for example /v 111 doesnt spawn the car with ID 111 it just spawns a random car.

Looking at the vehicle ids mentioned on the wiki the IDs start from 130 but somehow ID 90 , or 30  etc will also spawn a car,

Hope someone can help me to spawn the right car for the right ID.

thanks for now.


.

Quote from: aadb on Jan 27, 2015, 08:29 PMoffcourse i've already added a script load on the server.cfg and it seems to work but there is still a strange problem

typing for example /v 111 doesnt spawn the car with ID 111 it just spawns a random car.

Looking at the vehicle ids mentioned on the wiki the IDs start from 130 but somehow ID 90 , or 30  etc will also spawn a car,

Hope someone can help me to spawn the right car for the right ID.

thanks for now.


You haven't read my reply to see why that happens. That's because you're not creating a vehicle! And somehow you are confusing the vehicle id with model id!
.

jayant

@aadb -

else if ( cmd == "veh" )
    {
        if ( !text ) PrivMessage( player, "Syntax, /c " + cmd + " <ID>" );
        else
        {
if(!IsNum(text))PrivMessage(player, "ID should be a number");
else
{
local veh = FindVehicle( text.tointeger() );
if ( !veh ) PrivMessage( player, "Error: Wrong ID of the vehicle.");
else
{
PrivMessage( player, "Getting a vehicle " + veh + "." );
veh.Pos = Vector( ( player.Pos.x + 2 ), player.Pos.y, ( player.Pos.z ) );
}
}
}
}
Try it.. :)

aadb

 
Quote from: jayant on Jan 28, 2015, 03:40 PM@aadb -

else if ( cmd == "veh" )
    {
        if ( !text ) PrivMessage( player, "Syntax, /c " + cmd + " <ID>" );
        else
        {
if(!IsNum(text))PrivMessage(player, "ID should be a number");
else
{
local veh = FindVehicle( text.tointeger() );
if ( !veh ) PrivMessage( player, "Error: Wrong ID of the vehicle.");
else
{
PrivMessage( player, "Getting a vehicle " + veh + "." );
veh.Pos = Vector( ( player.Pos.x + 2 ), player.Pos.y, ( player.Pos.z ) );
}
}
}
}
Try it.. :)

thanks code i will try it, hopefully it works if i type for example /veh 130 it does spawn a landstalker and not a banshee or any random car.  http://wiki.vc-mp.org/wiki/Vehicles i would like that these ID correspond with the typed ID number

Thijn

Then that code wont do what you want either.

Use the CreateVehicle function, not FindVehicle.

aadb

Thanks all i know got it working that when i type vehicle id or model id no matter what you call it the actual car that i want does spawn.

topic can be closed i guess now, integrating it into my server script was easier then i thought just placing it on top and setting it all into logical order was just the case im getting there.