Community Forum

Export Vehicle Speed in txt

Forum Overview >> Scripting

Created04.08.2021 18:58

Timothy Biblocque (timobib) 04.08.2021 18:58
hi Everyone,

I would like to recover the speed of the vehicle in a text file.

Do you know if a LUA exists, that launch at the beginner of the game to recover the speed of each Vehicle ?

I develop a script but this script is launch in an other mod.
I have few problems to use this script in all Vehicle.

My Script :

local file = ('C:/***********/filename4.txt' , 'w')
file:write ( spec.vehicleInRange:getLastSpeed() , '\n')

Do You know what name i have used in th place of "spec.vehicleInRange" ?

Thanks !

Bilbo Beutlin (BBeutlin) 04.08.2021 19:37
I don't know what you mean, what is your plan?

1. The variable ".vehicleInRange" is not part of any spec(ialization), it occurs merely in the "VehicleSellingPoint.lua" to determine vehicles in the range of a selling point.

2. What do you mean with "recover the speed of each vehicle" ? At game start all vehicles are stopped, there's nothing to "recover".

3. Working with text files is nonsense. Use XML instead, this is supported by the FS as well as nearly all external programs.

Timothy Biblocque (timobib) 04.08.2021 20:04

My Objectf :

Tractor ( In game ) -> extract Speed of the vehicle ( In game) -> Write this value in txt or XML file -> export this value with Arduino to my other Display ( I make a real simulator with Tractor Component)

I need to realize a program who read Vehicle Speed when I am in a Tractor. That is why I said that i need a script who is activated for all Vehicle.

I am a beginner with LUA, If I understand, I create a mod folder with XML who call this LUA.

So, I try this code in my LUA :

if self.isMotorStarted then
local speed = self:getLastSpeed()
local file = ('C:/*************/filename4.txt' , 'w')
file:write ( speed , '\n')

I see that I have a problem with "self.isMotorStarted" and self:getLastSpeed() as it doesn't know the function ..
Part for write in Txt works correctly !

thanks for your Help !

Bilbo Beutlin (BBeutlin) 04.08.2021 20:37
You can only use "self:getLastSpeed()" if the "self" construct is related to a vehicle. In your code the "self" might be anything other.

----- example to get current player controlled vehicle
local vhcl = g_currentMission.controlledVehicle
if vhcl ~= nil then
local speed = vhcl:getLastSpeed()

I'd urgently recommand to use a timer which writes the output file only a few times per second, let's say all 0.1 - 0.5 sec, depending on usage. Writing every update() cycle is not needed, eats much resources and hinders reading by external programs.

Timothy Biblocque (timobib) 04.08.2021 21:08
this is my code .lua :


local function init()

local file = ('C:/*****/filename4.txt' , 'w')
local vhcl = g_currentMission.controlledVehicle
if vhcl ~= nil then
local speed = vhcl:getLastSpeed()


file:write ( speed , '\n')




The program blocks on this line "loal vhcl = g_currentMission.controlledVehicle" because the file was created but there are nothing inside and he is always open by Giant.

Maybe my program must not be in Init Function ? I don't know why this line doesn't work...

For the Timer Issue , it's interesting and i will make a timer to eat less ressources ! but firstly I would like that my program works.

Bilbo Beutlin (BBeutlin) 04.08.2021 21:54
I don't know when and from where you call your init() function. But if it is in a very early stage, the system tables might be not setup yet. And of course there's no player controlled vehicle, not a vehicle at all.
Case you refer to the Vehicle.lua "Vehicle.init()" : this is indeed an early pre-game stage where you can't access yet individual vehicle data.
The usual way to get evaluable datas is using the system 'update()' or 'onUpdate()' functions.

Also certainly if the variable 'vhcl' is 'nil', the following 'speed' is 'nil' and your code "file:write ( speed , '\n')" runs into error.

Sorry to say, but looks like you have no idea about programming at all. Better give up your plans until you have learned some basics about general LUA and FS scripting.
No offense meant.

Timothy Biblocque (timobib) 04.08.2021 22:33

thanks for your help,

For your Information, I am an Engineer in programmation specially in Agricultural Embedded System and it's new for me to use LUA. I am eager to learn this language. As I said, I 'm a beginner and I try to make this little code to begin with LUA.

I worked on old script in other mods to begin but I need now to compile my own code to advance...

I don't find a basic code in LUA to understand when and where functions were calling. If I understand Vehicle

I will continu to analize other script !

Note: Log in to post. Create a new account here.