Solved How to make a Lua function repeat constantly?

Discussion in 'Mod Support' started by Derpitron, Dec 17, 2022.

  1. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    While looking through the `orbit.lua` camera code*, I came across an `update(data)` function which seems to recieve some `data` variable from streams/sensors, and constantly repeats executing the function body.

    function C:update(data)


    I want to make a camera mod which recieves this same `data`, and modifies the camera position constantly, e.g to increase camera distance at high speeds. However, I'm not sure how to "call" this `data` from, and how to make my function run at all/repeat constantly. Currently it does not run at all.

    The BeamNG documentation is nonexistent, and the wiki's Lua Reference is still severely lacking and is bugged out for me. If anyone could point me in the right direction for good documentation, or how to go about solving my problem, it would be a big help.

    *This camera code is located at `\lua\ge\extensions\core\cameraModes\orbit.lua`.
     
  2. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,508
    As far as I know the C: means that it's handled by the game engine so I'm not sure if something like this is even possible, could be wrong though
     
  3. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    According to this official Lua guide https://www.lua.org/pil/5.html, the C: is just a way to declare an object-oriented function, i.e C:foo(x) is equal to C.foo(C, x)
     
  4. Gamergull

    Gamergull
    Expand Collapse
    BeamNG Team

    Joined:
    Jun 3, 2018
    Messages:
    460
    Hey!

    So for custom camera modes, this thread from v0.5.5 (!!!) describes how to set up a camera mod: https://beamng.com/threads/custom-camera-modes-possible-with-0-5-5.24111/ .
    Code:
    function C:update(data)
    This gets called from and receives data from lua/ge/extensions/core/camera.lua, and it gets called every frame automatically if you set it up correctly. The info in the thread is mostly compatible, except you need to add an additional function:
    Code:
    function C:setRefNodes(centerNodeID, leftNodeID, backNodeID)
    I attached an updated example that you can use or copy.

    By the way, while I'm here, this is unrelated to the camera code, but it is the generic way to set up a function in a Lua module so it can get repeated every frame:
    Code:
    local M = {}
    
    local function onUpdate(dtReal, dtSim)
      -- code goes here
    end
    
    M.onUpdate = onUpdate
    
    return M
     

    Attached Files:

    • Like Like x 2
  5. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    Thank you so much for all of this info! Do you have any other threads/ documentation which would be useful or relevant?

    My goal is modifying the currently selected camera's properties to increase CameraDistance or FOV, not to create an entirely new camera mode. Is this possible to do?
     
    #5 Derpitron, Dec 17, 2022
    Last edited: Dec 17, 2022
  6. Gamergull

    Gamergull
    Expand Collapse
    BeamNG Team

    Joined:
    Jun 3, 2018
    Messages:
    460
    Yup, it should be possible. Although, the code in orbit.lua gets pretty complicated; even I need to study it for a while to figure things out.

    The code around line 374 seems to be where the speed-dependent distance and fov get set. See if you can get some changes working there; I'll definitely try to crunch some numbers when I have time.
     
  7. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    I've successfully modified the code of orbit.lua to add new functionality before. But is it possible to modify or add extra functionality to the orbit camera's behavior without changing or replacing the original orbit.lua file? If so, then the camera mod I am making can be uploaded to the repository. Else, it will be a hassle for users to find and replace the vanilla orbit.lua with a modified version from some random source
     
    #7 Derpitron, Dec 17, 2022
    Last edited: Dec 19, 2022
  8. Gamergull

    Gamergull
    Expand Collapse
    BeamNG Team

    Joined:
    Jun 3, 2018
    Messages:
    460
    Ah, let's see... I think you can simply create a copy of orbit.lua, give it a unique name, make sure that it is in lua/ge/extensions/core/cameraModes folder, and it will automatically be usable in the game (i.e. switchable by pressing C or the main number keys). And then you can pack the mod this way.
     
  9. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    This would be quite redundant though. What about "injecting" extra code/functional into the update function of orbit.lua from the mod, so that an entirely new camera mode does not need to be created? Is that possible?

    Perhaps this would mean "implementing" a function from a module and over-writing it from a mod.
     
    #9 Derpitron, Dec 20, 2022
    Last edited: Dec 20, 2022
  10. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    So far looks like my only way is to create a new camera mode and copy orbit.lua's code.

    My main concerns of this way are:
    1. I will have to maintain the custom camera alongside orbit.lua, and merge/fix any conflicts or changes from the orbit.lua.
    2. Leads to redundant, duplicated code (two orbit camera versions). A modular way would be much more cleaner and programmatic.
    3. Might have to manually enable this camera mode in each and every vehicle's jbeam, which is too time consuming
    Will this camera mode be available in BeamNG settings?

    Edit:Seems like the core_camera module is my friend here. Maybe I can use that to modify the camera settings.

    upload_2022-12-20_20-51-1.png

    What does "res" stand for in camera.lua?
     
    #10 Derpitron, Dec 20, 2022
    Last edited: Dec 20, 2022
  11. Derpitron

    Derpitron
    Expand Collapse

    Joined:
    Jun 25, 2017
    Messages:
    252
    How can I create a function which both recieves the camera data AND gets repeated every frame, but is NOT in the cameraModes folder NOR gets called by core_camera (or) camera.lua?

    And where do scripts in the /lua/ge/extensions/<modname subfolder>/ get called from?
     
  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