1. Issues with the game?
    Check the Known Issues list before reporting!

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

Some Help With A Timer LuaTrigger?

Discussion in 'Troubleshooting: Bugs, Questions and Support' started by Danny Werewolf, Mar 7, 2019.

  1. Danny Werewolf

    Danny Werewolf
    Expand Collapse

    Mar 31, 2017
    So, I had the idea of a timer, where once you enter the LuaTrigger, it shows a red light, or no light. After some amount of time, maybe 8 seconds, the light would switch to green, or the other light turns onto green. This would be the beginning of the rally stage. @krallopian helped with the end, being a red light that turns on when in the trigger.

    I more or less know nothing about coding, so a bit of notes or laments terms would be helpful.
    • Like Like x 1
  2. krallopian

    Expand Collapse
    BeamNG Team

    Dec 5, 2013
    Hey sorry I didn't get to this for you yesterday, (few hours ago hah) I was super stressed out with my own code.

    I put this together in notepad, haven't tested it but it *should work fine for you. Lots of comments to help you along! *cheers

    *** I realized I missed a couple things, and will re-write it right now to make it easier for ya =)
    --- Post updated ---
    Oh man, it worked great! Then I changed the name of one thing, and then it wouldn't load. Changed it back and then it wouldn't work. Changed it back again and then it loaded and kept freezing. WUT :D

    Very strange. Even my mainLevel.lua file wasn't working, I deleted it, undeleted it, and suddenly it worked.. that's not possible, but it happened! So yeah, I'll get ya the code soon enough. I'm trying to tie it in to the code I gave you last night.
    --- Post updated ---
    I have no idea what happened so I just started over again. I changed a couple variable names (like, startLightGreen) so just be sure to check those.

    What happens right now is, there are two lights, when you drive into the box a red light turns on, then 8 seconds later it turns off and a green light turns on. Then when you leave the box, the green light goes out. (doesn't HAVE to but..)

    local M = {} -- start of the module
    local myEvent = false -- variable set as off
    local timer = 0 -- Setting the timer to 0 so it has some sort of information to begin with
    local finishLightRed = scenetree.findObject('redLight') -- This says, "create the name finishLightRed and make it the light in the game called, "redLight" (capital L)
    local startLightGreen = scenetree.findObject('greenLight') -- This says, "create the name startLightGreen and make it the light in the game called, "greenLight" (capital L)
    local function onBeamNGTrigger(data) -- the trigger "type" from within the game, and "data" is where it's information is stored.  It could say "franky" but you wouldn't remember why in the future
      if data.event == 'enter' and data.triggerName == 'finishLight' then -- This says, "when you ENTER the trigger named "finishLight" then do the next lines of code.   So you need to name the lua trigger finishLight (capital L)
        finishLightRed.isEnabled = true; -- this says, "enable the red light, wait for the timer to count to 8, then it will be turned off in the timer's code"
        print("hello!"); -- check the console with the ~ key to see that this works
        myEvent = true;
      if data.event == 'exit' and data.triggerName == 'finishLight' then -- This says, "when you EXIT the trigger named "finishLight" then do the next lines of code.   So again, be sure you have named the lua trigger finishLight (capital L)
        finishLightRed.isEnabled = false; -- this says, "turn OFF the red light when you leave the trigger in case you haven't waited the full 8 seconds"
        startLightGreen.isEnabled = false;
        print("Goodbye!"); -- check the console with the ~ key to see that this works
        myEvent = false;
      end  -- this closes the "if" statement
    end -- this closes the entire function
    local function onUpdate(dt)
        if myEvent == false then return -- if you haven't set myEvent to TRUE, then it will never start counting, and just keep looping, "can I count yet? Can I count yet!?"
        if myEvent == true then -- once you set myEvent to true (through a trigger you drive through or any other means) the timer will start
            timer = timer+dt -- this says, "with each frame, the timer is whatever it was +dt.  DT is "delta time" which refers to each frame rendered, so with each frame rendered the timer will increase.  Timer is shown in seconds this way it doesn't matter if you have 5fps or 500fps
                    if timer > 8 then -- 8 second timer once 8 seconds is up, the next line will finally occur, this is where your even magic happens! (maybe. . turn your light green!)
                                    startLightGreen.isEnabled = true -- this says, "enable the green light! GO GO GO!"
                    finishLightRed.isEnabled = false -- this says, "Once the timer is up, the green light is lit, and the red light turns off"
                myEvent = false; -- reset startDrag to prevent further timer counting
                timer = 0 -- reset the timer   
    M.onUpdate = onUpdate
    M.onBeamNGTrigger = onBeamNGTrigger
    return M -- this closes the "module"
    #2 krallopian, Mar 7, 2019
    Last edited: Mar 7, 2019
    • 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