Anything called more often than updateGfx in vehicle lua?

Discussion in 'Programming' started by mjc506, Jan 14, 2020.

  1. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    I'm having trouble with some vehicle lua, which doesn't get called often enough on some maps/settings (50fps is ok, 30fps is marginal).

    As updateGfx is only called once per frame, more complex maps or higher quality settings cause problems.

    Is there a function I can use in the vehicle lua (or somewhere else within a mod) that is called more frequently? If the powertrain controller is called more often, can I stick stuff in there?
     
  2. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    What exactly are you trying to do?
     
  3. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    A rather long term project... Implementing a motorcycle, balancing through steering the front wheel. It works nicely on gridmap on my machine, but doesn't react quickly enough to stay upright without oscillating on some more complex maps (dT steps become too large).

    If I could get that section of code running for each physics step, I think it would work nicely for all maps/machines. Easy enough by modifying core files by the looks of it, but it would be better not to.
     
  4. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    How are you running the relevant code? If you are using a controller for this (the software concept, not some xbox thing), you can get access to physics step.
    HOWEVER!
    You need to use this as rarely as possible, maybe first try not using each physics step but rather only execute your code at a fixed interval. (you say it works fine at 50hz, so convert the physics step into 50hz by waiting 1/50s before executing your code the next time)
    Remember that physics step is 2000hz, so if 50hz is enough, that would be a massive waste and very easily leads to performance issues if you don't know 100% what you are doing.
     
  5. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    It's all currently in a vehicle .lua, within the updategfx function - much of it is fine at low speed, but the central 'steer this much to stay upright' needs to be a bit quicker!
    Appreciate the warning about overuse. I can run the function every X physics steps. (A simple counter comparison would work I guess? Or like you say, waiting on the timer) How would I get a function called each physics step?
    Or are you suggesting I schedule the function to run at a certain frequency independent of the physics? (And again, how?)

    Thanks for your help by the way :)
     
  6. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    Ok, sounds like you are using just a simple lua extension. That's not the best way of doing things. Take a look at how all the stuff within the /lua/controller/ folder is being used, that's the modern way of having vehicle specific logic. If you want to see a vehicle actually making use of specific (rather than shared) controller logic, take a look at the sunburst for example and its tiny activeDiff controller here:
    \vehicles\sunburst\lua\controller\activeDiff.lua

    I can also offer you help to convert stuff to a controller etc, feel free to PM me with the files :)
     
  7. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    Not the best way? Sounds about right haha :) in my defense, I started working on this just after custom lua was implemented...

    That's great, thanks. I had seen the sunburst controller but clearly hadn't appreciated what I was looking at! I'll have a play and update...
     
  8. mjc506

    mjc506
    Expand Collapse

    Joined:
    Oct 13, 2013
    Messages:
    13
    Ok, for future Googlers...

    A vehicle controller can be very similar to a simple lua, but can be called differently by the game engine.

    Put yourVehicleController.lua in /vehicle/lua/controller/ , and add an entry to your jbeam to get the game engine to register it:
    Code:
    "controller" : [
        ["fileName"],
        ["yourVehicleController", {}],
    ],

    To get your 'updatePhysics' lua function called every physics frame, you can use:
    Code:
    M.init = init
    M.onReset = reset
    M.update = updatePhysics --2000Hz
    M.updateGFX = updateGFX --each graphics frame
    (This goes in yourVehicleController.lua)

    As Diamondback warns above, doing too much within this function can cause... issues... Either do something very very simple, or only do it every so often. For example, increment a counter each physics step, and run your code only every 10/20/50/100 steps, or similar.

    Thanks, Diamondback, for your help :D
     
    #8 mjc506, Jan 17, 2020
    Last edited: Jan 17, 2020
    • Like Like x 3
    • Informative Informative x 3
    • Staff Pick Staff Pick x 1
  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