Community Forum

Communication between LUA and Python (or other languages)

Forum Overview >> Scripting

CategoryScripting
Created18.08.2020 20:16


Robsicky 18.08.2020 20:16
Hi

I'm fairly new to both Lua and Farming Simulator, and was wondering if there is a way to communicate with Lua/ FS19 using some sort of API to another language. I think there is a Lua package to talk with python, but I'm not sure if I can add packages?

My goal is to pass some model based sensor data (GPS, IMU) from a Lua script, to an external controller in python, which returns control inputs to my vehicle in-game.

I'm not even sure if this is possible. Appreciate any info that could help! In the meantime I'll do some more reading on Lua and check out mods that might be relevant.

Thanks,
Rob

Bilbo Beutlin (BBeutlin) 19.08.2020 02:31
The FS LUA engine works in a sandbox. It can not interact with external programs, not even system I/O.

The only way is indirect communication via data files. Preferable XML, since this is well supported by the engine.

Robsicky 19.08.2020 13:12
Ah, I suspected something like that might be the case.

I could probably look into doing more of my stuff in Lua, but I'm not sure how much computation is possible without slowing down the game.

Sending necessary data through files could work. I'll check that out first.

Thanks a lot for the info!

Timothy Biblocque (timobib) 26.08.2021 16:07
Hi Robsicky, Hi Bilbo,

I saw your message and I write last week a script in LUA to send data in XML File.

I use a Python Script to read thoses values in live.

It works, but sometimes, I have a permission denied when i try to read values ( I think that Game Write at the same time that python Read)

I add a timer in Python script ( 0,5 s) , but do you know how I can make a timer in my Lua to try to resolve this problem?

Thanks,

Timothy

Bilbo Beutlin (BBeutlin) 26.08.2021 20:18
Easiest way is using the 'update(dt)' or 'onUpdate(dt)' function.
This parameter 'dt' (delta ticks) is the time [ms] since last call.

----- example code -----
myMod = {};
myMod.timer = 0;
myMod.tLimit = 500; -- delay in ms

function myMod.onUpdate(dt)
myMod.timer = myMod.timer + dt;
if myMod.timer >= myMod.tLimit then
myMod.timer = myMod.timer - myMod.tLimit;
-- write file
end;
-- other update function stuff
end;
----- end code -----



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