I have the following code: Code: local function onBeamNGTrigger(data) if data.triggerName == "lane1" then lanes1CarPresent = (data.event == "enter"); lane1CarID = be:getObjectByID(data.subjectID) print("Car 1 Ready");end if data.triggerName == "lane2" then lanes2CarPresent = (data.event == "enter"); lane2CarID = be:getObjectByID(data.subjectID) print("Car 2 Ready");end if data.triggerName == "lane3" then lanes3CarPresent = (data.event == "enter"); lane3CarID = be:getObjectByID(data.subjectID) print("Car 3 Ready");end if data.triggerName == "lane4" then lanes4CarPresent = (data.event == "enter"); lane4CarID = be:getObjectByID(data.subjectID) print("Car 4 Ready");end if data.triggerName == "lane5" then lanes5CarPresent = (data.event == "enter"); lane5CarID = be:getObjectByID(data.subjectID) print("Car 5 Ready");end if data.triggerName == "lane6" then lanes6CarPresent = (data.event == "enter"); lane6CarID = be:getObjectByID(data.subjectID) print("Car 6 Ready");end if lanes1CarPresent == true and lanes2CarPresent == true and lanes3CarPresent == true then startDrag = true; print("Lanes 1, 2, 3 occupied. Ready To Count!"); end if data.triggerName == "lane1" and data.event == "exit" then startDrag = false; print("drag timer has stopped, Car 1 has left the start"); end -- used to prevent drag timer starting over when you exit the trigger if data.triggerName == "lane2" and data.event == "exit" then startDrag = false; print("drag timer has stopped, Car 2 has left the start");end -- used to prevent drag timer starting over when you exit the trigger if data.triggerName == "lane3" and data.event == "exit" then startDrag = false; print("drag timer has stopped, Car 3 has left the start");end -- used to prevent drag timer starting over when you exit the trigger if data.event == 'enter' and data.triggerName == "stopDrag1" then be:getObjectByID(data.subjectID):queueLuaCommand('ai.setMode("stop")');end -- End of drag strip brakes end I read that as, "Do line 1, then line 2, then line 3" but it seems to solve for, "if line 1 is right, then do line 2, if line 2 is right, then do line 1, and 2, then do line 3." I'm trying to get the line: Code: if lanes1CarPresent == true and lanes2CarPresent == true and lanes3CarPresent == true then startDrag = true; print("Lanes 1, 2, 3 occupied. Ready To Count!"); end To only show once, but it shows three times. I don't understand why. --- Post updated --- More so - I can't figure out how to make more triggers I figured if I made a new trigger with the NAME "lane1Trigger" and the "type" says "lane1TriggerType" that I should be able to do this: Code: local function lane1TriggerType(tdata) print("I'm being read at the start of what?"); if tdata.triggerName == "lane1Trigger" then lanes1CarPresent = (tdata.event == "enter"); lane1CarID = be:getObjectByID(tdata.subjectID) print("LANE ONE HAS BEEN TRIGGERED");end end and at the bottom either: Code: M.onBeamNGTrigger = lane1TriggerType or Code: M.lane1TriggerType= onBeamNGTrigger But. . that's not working. I just don't understand how to have multiple triggers with unique names/identifiers. I'm trying to make separate things so they're not all under the same "onBeamNGTrigger" function, because that's why I figure my code is running over and over itself for different functions.
I think you should check to see if your function (onBeamNGTrigger) is getting triggered multiple times? For example once 3 cars have entered the trigger, each car might individually recognise that 3 cars are present, leading all 3 cars to call the function, each time stating that lanes1,2,3 are occupied. Maybe the following would only output that message once? Code: if startDrag == false and lanes1CarPresent == true and lanes2CarPresent == true and lanes3CarPresent == true then startDrag = true; print("Lanes 1, 2, 3 occupied. Ready To Count!"); end Since once one car has triggered it, "startDrag" will become true and the statement will no longer execute.
Thanks for the reply but I'm not sure I follow: There are 6 "onBeamNGTrigger"s right now, which is what I've discovered is the reason they're being triggered so much. So what I need to do is make each trigger call a different function, BUT I can't seem to make this work. Here's an example of the code: Code: local M = {} local function laneOneTriggerType(data) if data.triggerName == "lane1Trigger" then print(Hello?");end end M.laneOneTriggerType= laneOneTriggerType return M "function name(data)" which I assume is the trigger's lua type, and (data) is just a variable name, I could call it "wazoo" if I wanted as long as I changed all references to "data" to wazoo. Then, the end, "M.lane1TriggerType = lane1TriggerType" I have no idea why I have that there other than the usual code always has, "M.onBeamNGTrigger = onBeamNGTrigger"
Fantastic, I can't believe I didn't think of this. Jeeezus. "if startDrag == false and lanes1CarPresent == true and lanes2CarPresent == true and lanes3CarPresent == true then startDrag = true; print("Lanes 1, 2, 3 occupied. Ready To Count!"); end" does the trick just right. Derp. Now for trying to make custom functions, I have no idea why it's not working - I'm clearly missing something about how they work. --- Post updated --- Wow, significantly cleaner code now that I . . better understand! Code: local function onBeamNGTrigger(data) if data.event == "enter" then print("I'm being read at the start of what?"); if data.triggerName == "lane1" then lanes1CarPresent = (data.event == "enter"); lane1CarID = be:getObjectByID(data.subjectID) print("Car 1 Ready");end if data.triggerName == "lane2" then lanes2CarPresent = (data.event == "enter"); lane2CarID = be:getObjectByID(data.subjectID) print("Car 2 Ready");end if data.triggerName == "lane3" then lanes3CarPresent = (data.event == "enter"); lane3CarID = be:getObjectByID(data.subjectID) print("Car 3 Ready");end if data.triggerName == "lane4" then lanes4CarPresent = (data.event == "enter"); lane4CarID = be:getObjectByID(data.subjectID) print("Car 4 Ready");end if data.triggerName == "lane5" then lanes5CarPresent = (data.event == "enter"); lane5CarID = be:getObjectByID(data.subjectID) print("Car 5 Ready");end if data.triggerName == "lane6" then lanes6CarPresent = (data.event == "enter"); lane6CarID = be:getObjectByID(data.subjectID) print("Car 6 Ready");end if startDrag == false and lanes1CarPresent == true and lanes2CarPresent == true and lanes3CarPresent == true then startDrag = true; print("Lanes 1, 2, 3 occupied. Ready To Count!"); end -- do not start counter unless lanes 1, 2, and 3 are occupied. end if (data.triggerName == "lane1" and data.event == "exit") or (data.triggerName == "lane2" and data.event == "exit") or (data.triggerName == "lane3" and data.event == "exit") then startDrag = false; print("drag timer has stopped, Car has left the start"); end -- used to prevent drag timer starting over when you exit the trigger if data.event == 'enter' and data.triggerName == "stopDrag1" then be:getObjectByID(data.subjectID):queueLuaCommand('ai.setMode("stop")');end -- End of drag strip brakes end --- Post updated --- @aljowen are you able to tell me why my triggers aren't working with custom functions? I figure, I place a trigger in the world editor. I change "onBeamNGTrigger" to say, "customTriggerTest" which should call the function, "customTriggerTest" in the mainLevel.lua file. Then in there I put a code as follows: Code: local M = {} local function customTriggerTest(data) print(Hello?");end end M.customTriggerTest = customTriggerTest return M That doesn't work though. I don't understand why =)
I could be wrong, but I think that is to tell the game that it is possible call the a function called "onBeamNGTrigger", and then maps that to the actual functions name in your file. However, Lua isn't a language that I am super proficient in, so that is only a guess. If you create a new function, you will have to call it yourself. This is because the game wont know what to do with it, since it could be or do literally anything (that BeamNG permits [for example BeamNG won't allow you to connect to the internet, since people could write code that creates botnets]). If you are calling it from within your own Lua file, you don't need to do the "M.myFunction = myFunction" bit, since it doesn't need to be called from outside your file. If you expect the game to call it, you will need to add the "M.myFunction = myFunction" bit. --- Post updated --- I'm not sure. I would expect that to work, but I haven't done triggers in BeamNG before, so there is probably stuff that I am missing. I guess the most useful thing I could say is to double check that you have typed it in correctly in both places
Good to know about when you do and don't need the M.myFunction = myFunction bit, thanks! I'm trying to make a trigger in the game, call that function, and it simply isn't working. I think it has to do with the, "onBeamNGTrigger" part being an actual function name the game uses to then call your trigger in the code, but again trying "M.myFunction = onBeamNGTrigger" or "M.onBeamNGTrigger = myFunction" didn't do anything. There aren't any examples I can find in the game that show me how to do this either, I hate constantly writing to the dev's asking every single question that pops up, but man I keep learning more and more how little I really understand this stuff, and. . I hate admitting that =D
Does the drag strip on West Coast USA use triggers to make the Christmas tree lights function? If so, that might be worth a look. But I can't imagine its all that complex, it might only use one trigger for the whole thing. But, if that trigger has a custom name, it should be helpful.
Yeah it does, but it's all under "onBeamNGTrigger" that's the issue Currently if I put everything under that, it will run every check/command so if I had 25 triggers, it would do 24 things I don't need it to. I think the only way to avoid that would be to create custom trigger functions, and as you agreed, 'that should work' and it isn't, and just giving me: But "onBeamNGTrigger" works fine - again issue is it calls all the other things I don't want it to. --- Post updated --- I dunno why that image is so small sorry, but basically just says: "fatal lua error: [string"line"]:1: attempt to call global 'customTriggerTest' (a nil value)
It goes big when "right click -> view image" is used. You should be able to get around the issue by selecting the image in your post, deleting it (with 'del' key), and then adding it back in again using the 'Thumbnail' button at the below the text box. Its just one of the quirks with this forum software ¯\_(ツ)_/¯
Yeah I've always just added images instead of pasting. Ah well. This function crap is taking up my whole day. Looking at other people's scripts they're all setup just the same way I'm doing it! I've tried this too: Code: local function customTriggerTest(newVal) if newVal.event == "enter" and newVal.triggerName == "lane1" then print (Hello?"); end end end Still no damn luck, same thing. --- Post updated --- Hah. Clearly mentally deficient today. "Screw this, I'm taking a break" Three minutes later, "NO, IT'S CODE, It cannot beat me." So I get back to it and notice, first of all - THREE ends instead of two. Secondly, "print (Hello?")" . . how about putting double quotes on BOTH sides of the word? "That was it!" Nope. Still not working =D --- Post updated --- Well I got it to work using an example from a help doc somewhere, this code works: Code: function f(switch) if not switch then --if switch is nil, function f() will not complete anything else below Return return end print("Not Hello") end f()--doesn't print anything function f(switch) if not switch then --switch is no longer nil but is instead "1" return end print("Hello") end Now I just need to study WHY. I don't understand why the code f() does anything. --- Post updated --- What on Earth!!!!!! It works fine, but if I delete the top Code: function f(switch) if not switch then --if switch is nil, function f() will not complete anything else below Return return end print("Not Hello") end it stops working. So then I add it back, and it STILL doesn't work! This is insanity --- Post updated --- Holy crap! The issue is using "LOCAL function" if I just get rid of LOCAL then it works! ... If I use this code, it's functional: Code: function lane1trigger() print("3 Hello") end --- Post updated --- Well, duh, of course changing from local means my other stuff can't work. Such as, "be:getObjectByID(data.subjectID):queueLuaCommand('ai.setMode("stop")')" What the hell, why won't this just work the way it's clearly meant to: LOCAL FUNCTION DO YOUR DAMN LOCAL FUNCTIONING!!