LuaGE function to teleport vehicles [CLOSED]

Discussion in 'Programming' started by coolcostupit, Jan 3, 2023.

  1. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    Hello I am coolcostupit and I am kinda new to beamngs api and modding abilities. I am still learning but I understand lua statements so all I need to know is how I can make a function in LuaGE where the current players vehicle gets teleported to given coords. This is what my function looks like right now:


    local function teleport(dest)
    local vehicle = v.data
    --obj:queueGameEngineLua("queueLuaCommand('vehicle = be:getObjectByID(be:getPlayerVehicleID(0))')")
    vehicle.position = dest
    print(vehicle.position)
    print(dest)
    end

    the lua file is located inside modfile/lua/vehicle/extensions/luafile.lua
     
  2. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,414
    I did teleportation in a different way in my mod, it teleports the player by offset values, maybe it will help
    Code:
    local function teleport(X,Y,Z)
      for cid, node in pairs (v.data.nodes) do
        local d = obj:getNodePosition(node.cid):toTable()
        d[1] = d[1] + X
        d[2] = d[2] + Y
        d[3] = d[3] + Z
        obj:setNodePosition(cid, vec3(d))
      end
    end
     
    • Like Like x 2
  3. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    Thank you , do you maybe know how to get the coordinates/Vector3 Position of the destination (if available) from the Navigation extensions from beamng so players will be teleported to the destination.
    By the way I love ur mods.
     
  4. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,473
    You can teleport the player vehicle in GELua rather easily like this:
    Code:
     local function teleport(x,y,z)
    vx = x or 0 -- x coordinate
     y = y or 0 -- y coordinate
     z = z or 0 -- z coordinate
     be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3(x,y,z))
    end
    The navigation map follows world coordinates, so if you were to figure out the cursor coordinates relative to the ui map itself you'd have usable values already.
    However you'd still need to figure out a Z position, but a raycast like this does suffice:
    Code:
    Engine.castRay(vec3(x,y,99999), vec3(x,y,-99999), true, true)
    The full code would look like this:
    Code:
    local function teleport(x,y,z)
      x = x or 0 -- x coordinate
      y = y or 0 -- y coordinate
    
      -- cast ray
      if not z then
        local res = Engine.castRay(vec3(x,y,99999), vec3(x,y,-99999), true, true)
        if res and res.pt then
          z = res.pt.z
        end
      end
      z = z or 0 -- z coordinate
      be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3(x,y,z))
    end
    
     
    • Like Like x 3
  5. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    Thank you for your reply and help.
    Well now I got another problem (sorry I made a writing mistake), I am not sure but due to the lua location inside my mod file , beamng will not execute the code in LuaGe , I think its vehicleLua or something called like that. By that beamng gives me the variables:
    v --Vehicle User data
    obj --Vehicle API (beamng functions for the current player vehicle)
    There are probably alot of other variables but I am still new to beamngs api .
    Also I dont know java script very well, I tried making a $score.$on(navigationmapupdate) then it returns marker.data, I get some position but I dont know if this is the right position. I cant get on my computer right now but I can send you the rest code if you'd like to continue helping me at making this mod.
     
  6. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    I have tried it out and it does work fine but :

    I am not sure if it is my mistake but the problem may be it adds the coordinates and not sets them
    Here is how I get the maps destination position:


    scope.$on('NavigationGroundMarkersUpdate', (event, data) => {
    scope.dest = data.markers;
    });


    Here is the button function:

    scope.teleport = function () {
    console.log(scope.dest)
    bngApi.engineLua('ui_message("Teleporting to mission cords")')
    bngApi.activeObjectLua('extensions.beamTeleporter.teleport('+scope.dest+')');
    };
     
  7. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,414
    Yeah I just copy+pasted it from my mod and there it works by adding the coordinates, setting would be more complex
     
  8. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    Do you maybe got a idea on how to make it set the position variables instead of adding them?
     
  9. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,414
    Nope, I tried and it's too tricky, you would have to compare local and global position for each node accounting for vehicle rotation
     
  10. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    obj:queueGameEngineLua("queueLuaCommand('be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3('"..x..,..y..,..z..'))'..)
    Now I am trying to do it using queueLuaCommand but I get a unfinished string error , this seems very complicated here lol
     
  11. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,473
    Your command wasn't quite correct, here's the correct one:
    obj:queueGameEngineLua("be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3("..x..","..y..","..z.."))")
    "queueLuaCommand()" is a function you can call via GELua on vehicle objects, to execute something in vehicle lua for that specific vehicle. You don't need it here.
     
    • Like Like x 1
  12. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    I just made it, you can also use the code for your mod if you'd like to :

    obj:queueGameEngineLua("be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3("..X..","..Y..","..Z.."))")

    Now it doesnt add the position to the vehicles nodes, it sets them.
    But do you know how to get the destination position ? Because I am not getting the destination position I am getting the nearest node position for some reason.
    --- Post updated ---
    The fact just a seconds before you posted that I made the exact same code is crazy, programmer moment lol
    --- Post updated ---
    Just a short question:
    Do you maybe know how I get the position from the navigation destination?
    This is my current code:

    scope.$on('NavigationGroundMarkersUpdate', (event, data) => {
    scope.dest = data.markers;
    });

    scope.dest gives me the nearest node (if I am right) but I want to get the position from the destination,
    do you maybe know how?
     
  13. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,473
    data.markers contains ALL markers the path is run through; it's an array. The data is stored as x1, y1, x2, y2, etc. So you have to grab the last two items to get x and y of the last marker. You can do that like this:
    let x = data.markers[data.markers.length - 2];
    let y = data.markers[data.markers.length - 1];
     
    • Agree Agree x 1
  14. coolcostupit

    coolcostupit
    Expand Collapse

    Joined:
    Jun 19, 2022
    Messages:
    8
    Now as you said before beamng needs to raycast the z value , but since my code is not using luaGe I did it like that:
    obj:queueGameEngineLua("be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3("..x..","..y..",".."(Engine.castRay(vec3("..x..","..y..",99999), vec3("..x..","..y..",-99999), true, true))".."))")

    Now I get this error:
    upload_2023-1-4_13-59-33.png
    --- Post updated ---
    I MADE IT OMG THANK YOU SO MUCH FOR THE HELP

    This is the current code I made and it works just as fine :D:
    obj:queueGameEngineLua("be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3("..x..","..y..",".."(Engine.castRay(vec3("..x..","..y..",99999), vec3("..x..","..y..",-99999), true, true)).pt.z".."))")

    Thank you for the help .
     
  15. DaddelZeit

    DaddelZeit
    Expand Collapse

    Joined:
    Jul 17, 2019
    Messages:
    3,473
    Engine.rayCast returns a table containing more information than the hit height, you have to be more specific:
    obj:queueGameEngineLua("be:getPlayerVehicle(0):setPositionNoPhysicsReset(vec3("..x..","..y..",".."(Engine.castRay(vec3("..x..","..y..",99999), vec3("..x..","..y..",-99999), true, true).pt.z)".."))")

    Edit: Welp, I was too late it seems
     
    • 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