Hi I finally got around to buying this game and I love it, but I have one quick question. How do you create a script, just a script, that can be restarted independently from anything else? I've followed a few tutorials to learn the game and get me on my way and have created things like scenarios and stuff which have scripts running inside of them but you have to restart\load the whole scenario just to reload a script that you are working on, which sucks and is very time consuming when creating and testing a script. I've tried: ¬ Just throwing a LUA script in the mods/unpacked folder ¬ Creating a new folder tree mods/unpacked/myscript/myscript.lua Oh and just so we are clear I am not asking how to code a script, I have a basic understanding on scripting LUA. I just don't know much about BeamNG's file structure and how the game reads the scripts. I really want to create stuff for this game, whether it be models, maps and scripts. I guess the first step will be learning the file structure and scripting. I'm not entirely sure what I am going to make yet, I just want a script that I can add code to so that I can learn and test out BNG's functions. Thanks.
There are triggers you can use in the world editor. For example a trigger zone, where a script happens if you enter that zone. Might help. Sorry I can't provide more detail...
You can simply create an empty Lua file anywhere in the user folder path (I'd suggest putting it in the root path written below) and write in the functions you want to execute. You can execute the file by executing this function in the console: Code: dofile(lua_file_path) where lua_file_path is the path of the Lua file in the user folder path (root folder is Documents\BeamNG.drive\0.24 or whatever your custom user folder path is)
You hit the nail in the head when you mentioned folder structure: it's not obvious. By far, the path of least resistance is to download a bunch of mods, unpack them and look at the lua files and how they are organised. Darthbob's code is very well written. Also use the dump function a lot (see in utils.lua).
Also here's a collection of BeamNG functions I managed to find for both Game Engine Lua and Vehicle Lua enviroments: https://angelo234.github.io/List-of-BeamNG-Functions-And-Fields/index.html
Hi! There are a couple of other ways to load and test Lua scripts. The best way for automatic usage is to have the file in <user folder>/scripts/ (or <user folder>/mods/unpacked/modName/scripts/ , it works the same way) . Then, it will be loaded whenever the game starts. With the console (key: ~), you can now use extensions.myScript to interface with it (replace myScript with whatever file name you gave it). You can try out the attached Lua file as a demo! Move it to the mentioned folder, and in the console, type: extensions.myScript.active = true . Spawn in more vehicles to see what the code really does. The other way is to have the file in <user folder>/mods/unpacked/modName/lua/ge/extensions/core/ . Then, you can load it manually by using the console: extensions.load("core/myScript") . Once again, you can use extensions.core_myScript for stuff. This can be a useful way to create multiple scenarios that use the one file (I can provide details about this if needed). By the way, don't forget to use Ctrl+L to reload Lua whenever you make changes to the script while the game is running. As for the documentation, I'd love to add to it. There are so many features and details that need to become more known.
Sorry to bring up this old post but I did give this script a shot and it works great. One thing I did notice was that the marker started to "lag" as the car picked up speed then normalized as the car slowed down. It seems that when the car position data is fed to the marker in the script, it is not catching up to the actual vehicle position. It lags worse when the environment gets more complex. Is there a way to have this more in sync and follow the vehicle better? Thanks for the script!
Thanks! Ah yes, there is a tiny oversight that I had, but it is easy to fix. Change all Code: onUpdate to Code: onPreRender , and it should stay in sync.
Thanks so much, this is working great! If say I want to offset the cylinder position but still follow the car rotations, how would I go about this? I tried adding this code but if I try to add offsets to the position, the cylinder rotates about the origin of the offset. The original code I came up with that makes the object follow the veh with rotations and position. But no offset that works. Thanks again! Code: -- rotation local dir = vec3(v:getDirectionVector()):normalized() local up = vec3(v:getDirectionVectorUp()):normalized() local rot = quatFromDir(dir, up) rot = rot:toTorqueQuat() object:setField('rotation', 0, rot.x .. ' ' .. rot.y .. ' ' .. rot.z .. ' ' .. rot.w) -- position local pos = v:getPosition() object:setPosition(pos)
Hi Gamergull, thanks again for the help - I'm looking for a way to change the script so I can offset the marker to any point on the car and have it stick there. I tried adding this code but if I try to add offsets to the position, the cylinder rotates about the origin of the offset.