1. Trouble with the game?
    Try the troubleshooter!

    Dismiss Notice
  2. Issues with the game?
    Check the Known Issues list before reporting!

    Dismiss Notice
  3. Before reporting issues or bugs, please check the up-to-date Bug Reporting Thread for the current version.
    0.32 Bug Reporting thread
    Solutions and more information may already be available.

LUA help

Discussion in 'Troubleshooting: Bugs, Questions and Support' started by Capkirk, Aug 7, 2018.

  1. Capkirk

    Capkirk
    Expand Collapse

    Joined:
    Nov 19, 2017
    Messages:
    673
    So, I'm working on some LUA for a mod, and I can't figure out how to do something. What I want to go is be able to change Jbeam values on the fly. However, I can't figure out what values to actually change, or how. I'm pretty sure it's possible, other mods do it, I just can't figure out how to edit the variables.

    The relevant code is this, which is just a test which should change the shift point on a transmission and the max speed limiter.

    Code:
    if electrics.values['transMode'] == 1 then  --Sport
      topSpeedLimit = 67
      gearbox.lowShiftUpRPM = 3000
      log('I', "gearbox", "sport")
      end
     
  2. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    I think that editing jbeam values needed reloading of vehicle, but maybe there could be some other way around that?
     
  3. Capkirk

    Capkirk
    Expand Collapse

    Joined:
    Nov 19, 2017
    Messages:
    673
    I don't necessarily need to change the Jbeam, I just want to change the what the ingame LUA sees. Although thinking about it, I might need to just make a copy of the vehicle controller LUA with my custom code in it to hijack the Jbeam values as they are read?
     
  4. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    Wish I would know answers for this, sadly LUA is something I sometimes manage to get working after banging head against wall enough long :p

    Now if you set sport mode, then gearbox uses only high shift up and down, so technically I think you only need to set your high shift points to what you want shift points to be for sport mode, which would leave only speedlimiter problem.

    Do you need speed limiter in sports mode? It might be simpler to turn speed limiter off?

    I'm offering here bit different approach, just because I know when I'm dealing with such problem, I tend to lock up in certain pattern to solve issue, when there might be some another better way to achieve what I want.

    ESC can be toggled off, I wonder what code that uses, would figuring that out help finding way to change speed limiter?
     
  5. Capkirk

    Capkirk
    Expand Collapse

    Joined:
    Nov 19, 2017
    Messages:
    673
    Well, if all I wanted was shift points that would work, but I want to be able to change stuff like torque converter locking too, which won't work with sport mode. And my inspiration for this was the active diff mod, which seems to change the diff locking coefficients on the fly, and I was hoping to be able to do the same.
     
  6. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    Do you have a link to that mod, I might look it tomorrow if I could figure out anything useful?
     
  7. Capkirk

    Capkirk
    Expand Collapse

    Joined:
    Nov 19, 2017
    Messages:
    673
  8. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    The trick here is to ascertain where the values you want are stored. A few things just can't be changed on the fly, but I'd guess that this stuff will be fine. Once you learn the correct place to make the adjustments you can probably get your ideas to work. As I recall, some values do work the way that fufsgfen suggested - I think I ran into that when working on the lua for Heavy Delivery Pack:Utah by The Gas Station. Hint: the scenario I cooked up a bunch of lua for may do something like what you want, but there's a good chance that there's voodoo in play to get it working between GE lua and Vehicle lua! I also think that I might have done something weird to achieve what The Gas Station wanted since what I'd initially planned to adjust on the fly didn't work that way.

    Back to the Active Differential for Sunburst mod...

    I assume that you understand the parts of the lua like these:
    Code:
    local throttle = electrics.values['throttle_input']
    transfercase = powertrain.getDevice(jbeamData.transfercaseName)
    
    What we can see there are two things:
    In the first example, we are simply using the global variable/datastructure/whatever "electrics". There is a ton of important stuff in there, so I recommend that take a close look inside. Trial and error is a huge pain, the painless way to learn is to run a command like this from the lua console (~):
    Code:
    dumpToFile("electrics_test_output.json",electrics)
    This should produce a json file in your 'Documents\BeamNG.drive' directory which you can more easily review using Notepad++ or whatever you like.

    Same thing may apply to powertrain or it may not. Try dumping it the same way and see what happens. I either don't remember or don't know exactly what powertrain is. Eg it might just be a bunch of functions rather than exposing variables: we can see that it exposes at least one function, the "getDevice()" function. What's important to understand in that example is the variable "jbeamData" and where it came from. Gtpdzbiz grabbed that when it was passed to the init function like this:
    Code:
    local function init(jbeamData)
    blah blah blah
    end
    
    M.init = init
    
    Obviously you could do the same thing to understand that data structure. Something like this:
    Code:
    local function init(jbeamData)
    dumpToFile("jbeamdata_test_thing.json",jbeamData)
    end
    
    M.init = init
    Hopefully these tidbits will help you achieve your goals.
     
    • Like Like x 2
  9. Capkirk

    Capkirk
    Expand Collapse

    Joined:
    Nov 19, 2017
    Messages:
    673
    Ok, the electrics dump to file just gives me nil :/. Otherwise thanks for the advice, I'll try it out.
     
  10. fufsgfen

    fufsgfen
    Expand Collapse

    Joined:
    Jan 10, 2017
    Messages:
    6,782
    Maybe electrics.values or whatever it was that gave all electrics values with print code?

    @torsion, thanks, that gives lot of insight to LUA, there has not been LUA tutorial yet, do you think you would have time or interest to craft one?
     
  11. torsion

    torsion
    Expand Collapse

    Joined:
    May 31, 2015
    Messages:
    1,600
    'electrics' definitely isn't empty. Make sure you're running the dump in vehicle lua and not ge lua. There are multiple lua engines in the game! There's a dropdown menu next to the command prompt inside the console.
    upload_2018-8-9_18-38-24.png
    You're welcome. I try to spread the knowledge around when I have the opportunity.

    On the topic of quickly finding variables which have actual contents (rather than just nil), using print() is fine, but sometimes dump() works better. Structures which contain a lot of data won't fit on the screen, so that's why you use dumpToFile() to get their contents.

    I'm not sure what I would write in such a tutorial. I may have enough time or interest to write up something. It's tricky though. Many lua projects are somewhat unique. What needs to be done also depends on the task at hand. The game is very poorly documented. Usually when I want to know how to do something I look closely at the game's official lua content (or my own lua, or another mod's).

    Basic lua or programming tips are outside of the scope of my interest: you can get that stuff on the internet just like basic Blender, GIMP, etc tutorials.

    With all that said - I'm open to your suggestions about a lua tutorial. An alternative might be a write-up which dissects a couple of mods (or portions of them). Explaining the structure and what is happening might be useful to some people. OTOH it's tricky because there are so many different skill levels. :-|
     
  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