i am making a script for BeamNG.Drive and i am using LUA. Is there a way to run something onUpdate or something like that. setInterval would also work Thanks
Welcome to the forums, could you be more specific about the aim of your script ? Most of the game's LUA modding is done by the use of a module which gets directly recognized by the game's 'LUA architecture'. Such module exists of a 'table', also called an 'array' if you will. The module starts with opening a table called 'M' and the table is returned at the end : M = {} //bunch of code return M In this table you should write a function called updateGFX(dt). ( Other names might work as well but if I'm correct the updateGFX as name is predefined by core built in assets to be applied in the most useful way, 'dt' refers to delta time or difference in time) local function updateGFX(dt) //bunch of code end before returning the table you declare the function as : M.updateGFX = updateGFX Everything in this function will get updated by graphics steps (GFX) which is frame rate, also a built in steady value unless defined different. I'm not sure if setInterval would work, LUA is a very flexible scripting language that can intertwine or share similar syntax with different languages but the boundaries are less known to me since I mainly use BeamNG's LUA which is very customized and expanded with built in functions, libraries and tools. BeamNG's core LUA has quite some cross language code going on. setInterval sounds to me something more like Java(script) and C(+/+..) . In this case not very useful to persist since the updateGFX function is especially designed for applying code on update steps. If you look for a lua folder and file inside a vehicle folder, for instance, you will find a typical example of the way the module works and in virtually all cases find the updateGFX function in the same file too. There's always more to say but perhaps this should give you an idea ?
My goal is to recreate the Chaos Mod for GTA V but in Beam. I currently have the ability to do a random thing(switch the car, change the gravity etc.) by running a function. I want that function the be ran every 60seconds. Sleep freezes the whole game.
This is so cool! If I were you I would have it controlled by a frame counter variable. Since updateGFX is called every frame (correct me if I'm wrong), you should increase the value of the counter variable every frame, and once it reaches 60 times your FPS count it will mean that 60 seconds have passed, then it activates the randomness and resets the value of the variable to 0. Assuming that getting the FPS count is possible, but it should be.
Does this apply to both Game engine LUA as well as Vehicle LUA? --- Post updated --- I ask this because we are currently writing this mod in GE lua. We would however want to implement things like updateGFX as well as running vehicle functions such as changing engine variables, etc. We are new to BeamNG LUA programming so any insight would be helpfull. Thanks! --- Post updated --- We figured out how to send vehicle LUA commands to the players vehicle via local veh = scenetree.findObject(be:getPlayerVehicleID(0)) veh:queueLuaCommand("vehicle code")
yes cool https://www.beamng.com/threads/lua-delay.74227/#post-1236444 I believe using 'dt' by default results in seconds if you iterate 'your update timer' with 1, so that if timer > 1 roughly 1 second has passed given you have stable frame rates. It's always going to leap every once and then because it's not an internal clock but code waiting for frame counts but it works and it works really well.
Oh yeah I forgot that you can use the dt, it's much easier with it, it's not exactly 1 second but very close, no noticable difference.
Yeah the difference is quite noticeable if you really fix on the consistency, because frames get skipped quite often, but it doesn't harm at all and if you really really want clock work it's possible with counting a combination of how long the operating system works and what the current time is ( ostime, osclock etc..) but this sort of code gets quite advanced
I am having trouble getting this to work. (the example in the link). Is this supposed to work in Game Engine Lua? i run the script using the following command: Lua:exec("scripts/BeamNG-Chaos-Mod/updateGFXTest.lua") here you can see the code: https://github.com/superfroggman/BeamNG-Chaos-Mod/blob/main/updateGFXTest.lua
Sorry I didn't catch this earlier. So this means you are using this command in a LUA editor or have you actually created a modScript for the game because, if the case, I really don't think that is necessary, though not sure it might depend. Running the module outside of the game, in an editor like zerobrain etc, is not going to work, you need the 'dedicated dependencies' for it to run. The code from your link seems solid to me and should be working IF your LUA file is in the right location, where did you place it ? Also, using the user folder to make LUA run (for testing purpose or private use) is not working for all locations, often you need to pack it and put it in the mods folder or you can put it directly into the core directory in question of course, again for testing or private use . If you're using a vehicle as location it should work in the user folder. An example would look like Your_user_folder/vehicles/pickup/lua/your_lua_file.lua . In case of a vehicle you would also have to spawn the vehicle in question for the lua to be loaded. In case of GE (game engine) it might depend on its location whether you need to pack it as a mod or not. An example would be zipped file with following content > lua/ge/extentions/ your_lua_file.lua . I suppose that should be working too.
I am able to run GE Lua from JS file now, i created a mod and put the zip in the beamng mods folder. The mod shows upp and i can run GE Lua with: bngApi.engineLua(); and print things. but when i try to run a function from a file like this: bngApi.engineLua("extensions.modScript.runRandom()"); it doesnt work. The file is located from root in lua/ge/extensions/modScript.lua and the js file in ui/modules/apps/ChaosMod/app.js
So this means you are trying to run the code via the in-game console ? I've got less experience with that, I'd love to learn more about that but currently a little short on time. If you are trying to achieve anything else mod-wise feel free to specify the aim of your mod and maybe I can help filling in a blank here and there