Author Topic: Loading Vehicles using Script  (Read 1254 times)


  • Full Member
  • ***
  • Posts: 267
  • Karma: +54/-73
    • View Profile
Loading Vehicles using Script
« on: June 08, 2011, 05:15:45 pm »
Hello everybody. I am new to Squirrel, but have used a few other languages similar to it, like Lua and such.

My question is this:
I am wanting to load vehicles using the script so I can add more data to it (like the owner of a vehicle, for example). As I am used to Lua, which I can create a table to store this data, I am not sure how this works in Squiirel. I am able to make the tables and such, but im not sure how to index them and create the vehicles. Here is what I have so far:

Code: [Select]
function LoadVehicleFromSQL()
sqlite_query(database,"ALTER TABLE vehicles AUTO_INCREMENT=1");
local amount = sqlite_query(database,"SELECT count(*) FROM vehicles");
local vehicle = 0;
for(i = 1;i = amount;i++)
local query = sqlite_query(database,"SELECT model,x,y,z,a,color1,color2,owner,faction,job,locked,engine,lights FROM vehicles WHERE id = " + i);
vData[i] <- {};
vData[i].Model <- sqlite_column_data(query,0);
vData[i].X <- sqlite_column_data(query,1);
vData[i].Y <- sqlite_column_data(query,2);
vData[i].Z <- sqlite_column_data(query,3);
vData[i].A <- sqlite_column_data(query,4);
vData[i].Color1 <- sqlite_column_data(query,5);
vData[i].Color2 <- sqlite_column_data(query,6);
vData[i].Owner <- sqlite_column_data(query,7);
vData[i].Faction <- sqlite_column_data(query,8);
vData[i].Job <- sqlite_column_data(query,9);
vData[i].Engine <- sqlite_column_data(query,10);
vData[i].Lights <- sqlite_column_data(query,11);
vehicle = CreateVehicle(vData[i].Model,Vector(vData[i].X,vData[i],Y,vData[i].Z),vData[i].A,vData[i].Color1,vData[i].Color2);
vehicle.Locked = vData[i].Locked;
vehicle.SetEngineState = vData[i].Engine
vehicle.LightState = vData[i].Lights

I am not sure how to index this table (should it be by the increment in the for loop, or by the instance created from the vehicle?)

The only conclusion I have about this is:
I create the tables, and index them by ID, not the instance, then I could use vehicle.ID in the index like:

Any help or information on this is greatly appreciated. I am probably doing it wrong, but Im not sure how to add other info to the vehicle instance (so it could be like vehicle.Owner, or vehicle.Price or something)



  • Guest
Re: Loading Vehicles using Script
« Reply #1 on: June 09, 2011, 04:35:07 pm »
Squirrrel tables are not indexable with integer ids like arrays.
You want to use an array to contain table instances which then contain necessary information in
Code: [Select]
vData[i].Property form. (Which is what you are already doing)

The only problem I see is that you're using the newslot operator (->) instead of the assignment operator (=) for
Code: [Select]
vData[i] <- {};The newslot operator is for tables like the one used in your code, and the roottable which is where all global functions and variables are stored in.
Change the code to:
Code: [Select]
vData[i] = {};
Apart from that, the code looks alright.
There doesn't seem to be much point though in adding in your vehicles like this since this makes the usage of the console command, "reload_scripts", obsolete. It would make your vehicles get added for every script load instead of every server start.
I would rather add vehicles into the main config, Content.xml and add extra data into the db which then gets applied onScriptLoad.

Also, Vehicle.SetEngineStatus or any Set* function is a method not a variable.
« Last Edit: June 09, 2011, 04:36:47 pm by Windlord »


  • Tester
  • Sr. Member
  • ****
  • Posts: 531
  • Karma: +27/-16
    • View Profile
Re: Loading Vehicles using Script
« Reply #2 on: June 09, 2011, 05:31:19 pm »
Also spotted a bug in your query/code.

You are selecting `locked` from the database on ID 10.
And you are assigning the property Engine to that. While Engine is selected as ID 11.
I think you forgot .Locked before that ;)


© Liberty Unleashed Team.