How to literally just create a script and read it?

Discussion in 'Content Creation' started by StaceyBee, Dec 31, 2021.

  1. StaceyBee

    StaceyBee
    Expand Collapse

    Joined:
    Dec 31, 2021
    Messages:
    1
    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.
     
  2. el_ferrito

    el_ferrito
    Expand Collapse

    Joined:
    Sep 30, 2020
    Messages:
    504
    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...
     
  3. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    585
    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)
     
    • Like Like x 2
  4. lellolillili

    lellolillili
    Expand Collapse

    Joined:
    Nov 29, 2021
    Messages:
    136
    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).
     
    #4 lellolillili, Jan 4, 2022
    Last edited: Jan 5, 2022
    • Agree Agree x 1
  5. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    585
  6. Gamergull

    Gamergull
    Expand Collapse
    BeamNG Team

    Joined:
    Jun 3, 2018
    Messages:
    460
    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.
     

    Attached Files:

    • Like Like x 5
  7. ffzero58

    ffzero58
    Expand Collapse

    Joined:
    Nov 18, 2018
    Messages:
    54
    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!
     
  8. Gamergull

    Gamergull
    Expand Collapse
    BeamNG Team

    Joined:
    Jun 3, 2018
    Messages:
    460
    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.
     
    • Like Like x 1
  9. ffzero58

    ffzero58
    Expand Collapse

    Joined:
    Nov 18, 2018
    Messages:
    54
    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)
     
  10. ffzero58

    ffzero58
    Expand Collapse

    Joined:
    Nov 18, 2018
    Messages:
    54
    Just wanted to follow up on the below and if there is some code to make offsets work here? Thanks!

     
  11. ffzero58

    ffzero58
    Expand Collapse

    Joined:
    Nov 18, 2018
    Messages:
    54
    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.
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice