Vehicle Lua to GE lua

Discussion in 'Programming' started by NuclearKnight, Jan 6, 2022.

  1. NuclearKnight

    NuclearKnight
    Expand Collapse

    Joined:
    Nov 30, 2013
    Messages:
    64
    I'm much closer now to completing my mod but there is something that is causing me a lot of headache. Once again, I'm sure there is something simple i'm missing but I can't figure out how to use data from Vehicle lua to run functions in GE lua. For example, using updateGFX to get a value such as airspeed and then when that value is more than 10 or whatever, run a function in GE lua. I searched around for anything relating to this but couldn't find any clear explanations on how to do what I want, thanks in advance for any help!



    Also on a sidenote, having trouble with lua triggers as well. I can get code to work when I input it into the function window in the world editor, but I can't figure out how to use this trigger to interact with my custom scripts.
     
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Here's an example of how to call a GE Lua function from VE Lua, without and with parameters:
    Code:
    obj:queueGameEngineLua('your_func()')
    
    local x = 10
    obj:queueGameEngineLua('other_func('.. tostring(x) ..')')
    Could you give more info on what you're trying to do with the Lua triggers and FYI the Lua triggers run in the GE Lua environment.
     
  3. NuclearKnight

    NuclearKnight
    Expand Collapse

    Joined:
    Nov 30, 2013
    Messages:
    64
    All I'm really trying to do with the trigger is have it call a function that is in my GE Lua script file that triggers
    So, I couldn't get the queueGameEngineLua working right. I made a simple function called "testScript()" in my lua script file in lua\ge\extensions\myscriptfile but when i try to call that function with the queueGameEngineLua it says "attempt to call nil value(or nil field i cant remember which but i can check later) testScript".
    '

    As for the triggers, all i really wanna do with that is to trigger a function that does a TriggerServerEvent for BeamMP but am unsure how to use the trigger to execute the code in my lua\ge\extensions\myscript file.

    Thanks for your help btw!
     
  4. NuclearKnight

    NuclearKnight
    Expand Collapse

    Joined:
    Nov 30, 2013
    Messages:
    64
    Yeah I've been trying to get this to work to no avail. To clarify on what I'm trying to do, right now I just want to get the vehicle z position and pass that over to a function in the GE environment that I have made. I can get the vehicle z position with no issues but when I try to queue my custom function it doesn't work and says "attempt to call field myfunction() a nil value".
    --- Post updated ---
    Code:
    local M = {}
    slowTime = 0
    local function updateGFX(dt)
        playerPos = obj:getPosition()
           posZ = playerPos.z
      
        slowTime = slowTime + dt
        
        
        if slowTime >= 1 then
          
      
                obj:queueGameEngineLua("testScript('tostring(posZ)')")
              
        print(playerPos.z)
        print(dt)
      
      
     
            
          
        slowTime = 0
    end
    end
    M.updateGFX = updateGFX
    return M
    
    This is the vehicle lua



    Code:
    M = {}
    playerID = nil
    local function testScript(posZ)
    print("Test", posZ )
     
    end
    
       
    M.testScript = testScript
    return M
    and the GE code


    This gives me an error: "attempt to call global testScript" (a nil value)
    However, if i replace my function with be:restVehicle() or similar it works
     
    #4 NuclearKnight, Jan 7, 2022
    Last edited: Jan 7, 2022
  5. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Okay its because that function isn't in the global scope. There's a specific way to load in the GE Lua script too and I've attached a zip file containing how it should look like. Now that you've loaded in the script, the variable name of the Lua script is (derived from the file path "scripts\test_mod\extension.lua"):
    Code:
    scripts_test__mod_extension
    So in order to call the testScript function, you would call it like this:
    Code:
    scripts_test__mod_extension.testScript()
    And this is how you should call the testScript function from VE Lua (you want to resolve the posZ's value first and then concatenate it with the function call):
    Code:
    obj:queueGameEngineLua("scripts_test__mod_extension.testScript(" .. tostring(posZ) .. ")")
     

    Attached Files:

  6. NuclearKnight

    NuclearKnight
    Expand Collapse

    Joined:
    Nov 30, 2013
    Messages:
    64
    Ah yes that got rid of the error thanks for you help! Maybe I'll be able to finish what I'm doing now lol

    Actually I was wrong, still gives the same error. I'll keep messing around and try to get it to work.

    Well I was an idiot and named it "extensions" instead of just "extension" so its fixed now...
     
    #6 NuclearKnight, Jan 7, 2022
    Last edited: Jan 7, 2022
    • 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