Released Help with Lua Trigger in BeamNG.drive

Discussion in 'Programming' started by Finers, Sep 30, 2024.

  1. Finers

    Finers
    Expand Collapse

    Joined:
    Sep 30, 2024
    Messages:
    1
    Hi everyone,

    I’m working on a Lua trigger in BeamNG.drive, and I need some assistance. I want to create a trigger that, when a vehicle enters it, will cause another specified vehicle to "press" the spacebar.
    Can anyone help me figure out what might be going wrong? Any suggestions or insights would be greatly appreciated!

    Thank you!
     
  2. Riccarr

    Riccarr
    Expand Collapse

    Joined:
    Jul 28, 2024
    Messages:
    21
    You can create your trigger with code like this ...

    Code:
    local function createTrigger(name, position, scale, triggerEvent)
        local trigger = createObject("BeamNGTrigger", name)
        trigger:setPosition(position)
        trigger:setScale(scale)
    
        trigger:setField("triggerType", 0, "Sphere")
        trigger:setField("triggerMode", 0, "Contains")
    
        trigger:setField("luaFunction", 0, String(triggerEvent))
    
        trigger:registerObject(name)
    
        return trigger
    end
    Call the function to pass needed parameters, for example ...

    Code:
    local scale = vec3(x, y, z)
    local position = vec3(x, y, z)
    
    -- I found have had to prefix the trigger name with the script name
    triggerEvent = "your_mod_name.myTriggerEvent1"
    createTrigger("mytrigger1", position, scale, triggerEvent)
    You would also then need your lua trigger event "myTriggerEvent1" ... or whatever you name it, like this ...

    Code:
    -- note this function should be positioned in your lua above the createTrigger()
    local function myTriggerEvent1(data)
       local playerVehicleID = be:getPlayerVehicleID(0)
    
        if data.subjectID == playerVehicleID then -- only act on player vehicle, not ai vehicles
      
            local triggerName = (data and data.triggerName) or "no data"
            local triggerId = (data and data.triggerID) or ""
            local eventName = (data and data.event) or "no event"
    
            if eventName == 'enter' then
               -- your code here
           end
    
            if eventName == 'exit' then
               -- your code here
           end
    end
    You also need to add the event name to the module, at the bottom of your script ...
    Code:
    M.myTriggerEvent1 = myTriggerEvent1
    And finally, your probably going to want some visible marker/icon to see where your trigger is, so create a TSStatic object and place it at the same position as your trigger.

    Code:
    local marker =  createObject('TSStatic')
    Use the WorldEditor to find your position coordinates, its helpful. Press F11 in game.

    There's a lot of code to find examples from, in the steam install folder for BeamNG. Search here ...

    ... \Steam\SteamApps\common\BeamNG.drive\lua\ge\extensions

    Good luck!
     
    • Like Like x 1
    • Agree Agree 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