Newbie confounded by the simplest of things

Discussion in 'Programming' started by Q_Hung, Jul 2, 2021.

  1. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Hi all,

    I'm new here, experienced programmer but getting into Beam and lua for the first time, hoping to use Beam as a platform for an idea I've wanted to develop since I was a kid. It's all pretty straightforward and seems like it should be simple to implement via the Beam / lua system, but I'm stuck at the very beginning with a seemingly stupid problem that has totally confounded me for a few days:

    Changing the raws, copying / renaming vehicle folders to duplicate vehicles, adding lua functionality to vehicles, none of it seems to have any effect. Like, nothing changes. I feel like I'm missing something really fundamental, something about the folder structure of the raw files, something. But I can't figure out what the heck is wrong, and the sparse documentation is not offering much help.

    Here's a brief and functional overview of what I want to do: I want to have a routine that runs on the track or higher level, which can offer functionality similar to the currently-active "leaderboard" thread on these forums along with other things. I also want to have a routine, presumably run via a controller on Vehicle lua, which can programmatically operate the driving controls. These systems speaking to each other should, in theory, be able to choreograph multiple vehicles in the same setting together in an organized and "intelligent" way.

    Seems straightforward enough, and I've identified a good number of little tidbits here and there which have allowed me to put together an idea of how to really get this all accomplished within the Beam / lua system. All good so far. So then I try to do a quick "hello world" by installing a routine on a vehicle that simply mashes the throttle to the floor, and I just can't get it to work. The thing that has me most confounded right now is that I am trying to do the "duplicate and modify a stock vehicle" trick by copying and renaming the vehicle raw folders along with info.json, and I cannot see any difference that any of it makes in the game itself. Like the renamed and copied vehicles simply do not appear in the vehicle picker.

    There's two sets of Beam data folders on my drive - one in the steam Beam folder, and one in Documents. I've tried to modify in both places and I just can't get it to work.

    Please help?? Thanks very much.

    Q
     
  2. Cincinnatus

    Cincinnatus
    Expand Collapse

    Joined:
    Aug 25, 2020
    Messages:
    643
    Did you rename the vehicle in the info.json?
     
  3. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Yup. Renamed both the manufacturer and the model name by appending a suffix to both.
    --- Post updated ---
    Wondering whether or not I need to zip up the vehicle files in the Documents folder? I'm typically not doing that. Do I need to archive them in order for them to work in the game engine?
     
  4. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    You either zip them and put in your userfolder > mods > mod name > vehicles, or put in your userfolder > mods > unpacked > mod name > vehicles. No way around this.
     
  5. Cincinnatus

    Cincinnatus
    Expand Collapse

    Joined:
    Aug 25, 2020
    Messages:
    643
    By the way, you shouldn't place your vehicle folder in the beamng folder that's in documents. It should go in C:\Users\yourusername\AppData\Local\BeamNG.drive\0.23\mods\unpacked
    If your vehicle is in the unpacked folder, you shouldn't zip it. (having an unzipped mod folder makes it easier to work on)
    --- Post updated ---
    oops, didn't see agent's post
     
  6. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Thanks so much for this, guys. I will try it out as soon as I get home from work today.
     
  7. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    You explained it better than me
     
    • Like Like x 1
  8. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Alright! Your help worked for me, again thank you so much.

    With apologies I've got another small question, similarly simple but hard for me to figure out without any experience. I'm trying to add a controller to a vehicle (the autobello ATM just for a test case), so I'm putting the

    "controller": [
    ["fileName"],
    ["v1Controller", {}]
    ],

    (v1Controller.lua being the name of my custom controller code)

    I add it to the autobello.jbeam file, immediately under the normal "vehicleController" declaration, and it does not function as expected, while also breaking the existing control and vehicle telemetry/display structure. I feel like I'm putting it in either the wrong place, or putting it in the file in the wrong way somehow.

    Where should the additional controller declaration go? In autobello.jbeam, elsewhere within the file? In a different jbeam file completely? Am I simply going the wrong direction totally?

    Thanks very much. I am encouraged by the help of the community and in the progress it's generating for my project.

    Q
     
  9. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    Hey :)

    Without seeing the complete file I can only guess, but it sounds like you might have added a whole second controller section? That's not what you need to do, instead just add the line that refers to your controller.
    This is what a controller section with multiple controllers looks like:
    Code:
            "controller": [
                ["fileName"],
                ["drivingDynamics/CMU", {"name":"CMU", "debugSettings": {"enableDebugMode": true, "peerIP": "127.0.0.1"}}]
                ["drivingDynamics/sensors/sensorHub" ]
                ["drivingDynamics/sensors/virtualSensors" {"name":"sensors"}]
                ["drivingDynamics/sensors/vehicleData" {"name":"vehicleData"}]
                ["drivingDynamics/supervisors/components/motorTorqueControl" {"name":"motorTorqueControl"}]
                ["drivingDynamics/supervisors/components/brakeControl" {"name":"brakeControl"}]
                ["adaptiveBrakeLights"],
                ["postCrashBrake", {"brakeThreshold": 60}],
                ["twoStepLaunch", {"rpmLimit":2000}],
            ],
    You can also have multiple controller sections, just not within the same part.
    As for where the additional controller goes, it depends. It technically can go anywhere, but you might want it in a specific part. Think of controllers like the microcontrollers in a real car, the ECU, something that controls the transmission, another thing for the navigation unit etc. That's the idea behind controllers. Reusable pieces of code that do a specific job.

    So if ypur functionality is coupled to a specific part in a car, then you probably will want to define the controller in that part. This way, no code is loaded or executed when that part isn't installed.

    As for configuring defined controllers, there's two ways of doing that: inline configuration and explicit configuration. They are technically not different, it's just a matter of how clean it looks.

    Inline:


    Explicit:
     
  10. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    Are you trying to replace the main Jbeam file? This is not the way, you should rather add a separate part in a new slot (for example Additional Modification) and add the controller there instead. But you can also add a normal LUA file that isn't a controller, the difference is that this kind of file goes in a lua folder inside the folder of the vehicle, while a controller goes in a subfolder of it called controller. Just remember that they both act differently and you can't just swap all the code between them.
     
  11. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Yup, thanks Diamondback, I was attempting to add a second controller section instead of just adding a second controller to the one which is already declared in the file. I understand completely! Again will need to try this out when I have time in a few hours.
     
  12. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Morning all,

    Floating through a pleasant morning here, watching Zak Brown discuss Lando's front-row start in Austria and puttering around with my BeamNG project. It's been good so far.

    I'm very excited to report that my "hello world" in vehicle lua is now working, thanks to your help. My Autobello driver is slamming the throttle to the floor as requested - that's good enough for now. I'm confident that I can expand that functionality now that the lua is properly connected to the game engine. Thank you!

    So now I look to creating a "hello world" in the game engine. Ultimately this game engine routine will handle tasks similar to race control - and will hopefully interface with the vehicle lua driver routines to create a manageable race weekend session structure. The connections between game engine / race control lua and the various driver lua routines should be able to solve the "cars don't know where each other are" problem that currently exists when multiple AI vehicles share the road.

    So my questions are twofold this morning. First, can I run game engine lua by simply declaring and implementing functions in a "common" lua file, then calling the function from the lua console? If that's a valid pathway it would work fine for me - I would be able to begin a managed session through the console by manually calling my own code. Fine with me.

    Second question, what is the correct / accepted way to both delete currently-spawned vehicles, and also spawn new ones via the lua API? I've looked around for answers prior to posting here, but the only suggestions I can find seem oddly archaic, and they're also noted in the forum posts to create errors in the game engine when called. Is there an accepted way to get this done that doesn't throw exceptions to the game engine?

    I really can't express how much I appreciate the help you all offer here. Many thanks -

    Q
     
  13. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    Maybe take a look at the spike strip mod, it has some LUA for spawning and removing vehicles/props in gameplay and I don't remember it throwing any errors. I tested some stuff with it once and it is able to spawn and delete anything you set. Link: https://www.beamng.com/resources/spikes-nodegrabber-spikestrip.447/
     
    • Like Like x 1
  14. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Aaaargh! If I were to have made a lua file called "v1.lua", placed in the "/steamapps/common/BeamNG.drive/lua/ge/", with a function called "sessionShakedown(arg1)", how do I go about calling that function from the console? This is the stuff that has always confused me the most about learning a new coding system or style - just connecting all the basics together at the very beginning.

    I've tried a good number of different syntaxes and they're all just chucking errors around.

    Thanks as always -

    Code:
    
    local M = {}
    
    -- code located in steamapps/common/BeamNG.drive/lua/ge/v1.lua
    
    local function sessionShakedown (circuitChoice)
    
       M.clearCanvas()
    
    
    end
    
    
    local function clearCanvas ()
    
       local playerVehicle = be:getPlayerVehicle(0)
        local playerVehicleID = be:getPlayerVehicleID(0)
    
        playerVehicle:delete()
    
        playerVehicle = nil
        playerVehicleID = nil
    
    end
    
    
    M.sessionShakedown = sessionShakedown
    M.clearCanvas = clearCanvas
    
    return M
    
    
    Q
     
  15. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    I also have no idea, but I figured out how to do it with a controller, maybe it's similar:
    Code:
    for _,v in pairs(controller.getControllersByType('CONTROLLER NAME HERE')) do v.FUNCTION NAME HERE(ARGUMENT HERE) end
     
    • Like Like x 1
  16. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    You type that into the console?
     
  17. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    I only tested it being called via input actions, but could work in the console as well, idk.
     
  18. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,957
    Don't worry, it's the same for all of us.

    If you have just random lua code (ie not an extension file, controller, powertrain device etc), you will have to "require" that file. This loads the code and makes sure you can use.
    If you just place it in the folder, nothing will happen by default.

    A quick note on the directories:
    • stuff in lua/vehicle can only be used in vlua
    • stuff in lua/ge can only be used gelua
    • stuff in lua/common can be used in both, watch out for API differences between the two VM types though
    "Can be used" here means that you can "require" the file from the specific VM.
     
    • Like Like x 1
  19. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    Excellent, and once I've got the file placed and required, do I call the function by simply typing it into the console, or do I need a specific prefix (ge., v1., M., etc)?

    And please, I'm so desperate for any tidbit of info I can get, I've got one more that I want to ask of you specifically, Diamondback - how do I get a custom class into the F11 editor, so I can place customized position locators around circuits? I simply want to place information containers in the map, similar to waypoints but containing additional information and functionality from the custom class I define. Is there a place to put the lua file, a "require" procedure, etc?

    Again thank you. Each nugget of info is like a drop of water in the desert.

    Q
     
  20. Q_Hung

    Q_Hung
    Expand Collapse

    Joined:
    Jul 2, 2021
    Messages:
    21
    DUDE IT WORKED. I got "hello world" working in both Vehicle Lua and Game Engine Lua now. I found the "require" statements in /ge/main.lua, "required" my v1 file, and then referenced the function using the name I gave it in the main.lua file - and voila. It worked. I stood there stunned for a moment when it actually made the truck disappear.

    Onwards. Still maybe hoping for a quick word about getting custom classes into the F11 editor? I'm high on progress ATM.

    Q
    --- Post updated ---
    Thanks, dude. This turned out to be a great reference, I'm now able to handle deletes and spawns without errors. Appreciate it greatly.

    Q
     
    • Like Like 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