Spawn Props Programmatically

Discussion in 'Utilities and programming' started by EdxTm, Oct 11, 2022.

  1. EdxTm

    EdxTm
    Expand Collapse

    Joined:
    May 12, 2019
    Messages:
    17
    Hey all,

    I know that the console allows me to span a vehicle (or a prop) using the function below:

    core_vehicles.spawnNewVehicle("cardboard_box", {config = 'vehicles/cardboard_box/large.pc'});

    I'm trying to span a pile of boxes at random places by using a loop:

    local items = 0;
    while (items < 50)
    do
    core_vehicles.spawnNewVehicle("cardboard_box", {config = 'vehicles/cardboard_box/large.pc'});
    items = items + 1;
    end

    However I'd like to have some control where to spawn them, currently they space one after each other equally, which is expected since I'm not specifying where to spawn them. Is it possible to control where to span things (or even the orientation) using this function? I didn't find any references.

    Thanks in advance.
     
    • Like Like x 2
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    585
    Hey, here's an example code that spawns those boxes in a circular pattern on Gridmap V2:
    Code:
    local items = 10
    local offset = vec3(-150, -200, 105)
    
    for i = 0, items - 1 do
      local angle = i / items * 2 * math.pi
     
      local x = math.cos(angle) * 10
      local y = math.sin(angle) * 10
      local z = 0
     
      local vehPos = vec3(x, y, z) + offset
      local vehRot = quatFromAxisAngle(vec3(0,0,1), -angle)
     
      core_vehicles.spawnNewVehicle("cardboard_box", {config = 'vehicles/cardboard_box/large.pc', pos = vehPos, rot = vehRot})
    end
    Here's the result:
    resultMedium.png

    Sounds like you type it all in the console and execute it that way, but in my opinion, here's an easier way to do it for longer code:

    1. Create a Lua file (lets say name it test.lua) in the BeamNG userfolder
    2. Write your code there
    3. And just execute the Lua file in the console using
    Code:
    dofile("test.lua")
     
    #2 angelo234, Oct 20, 2022
    Last edited: Oct 20, 2022
    • Like Like x 2
  3. EdxTm

    EdxTm
    Expand Collapse

    Joined:
    May 12, 2019
    Messages:
    17
    awesome, exactly what I wanted! Thank you very much!
     
  4. Hank_Montgomery

    Hank_Montgomery
    Expand Collapse

    Joined:
    Jun 9, 2022
    Messages:
    68
    How do I force the boxes to spawn at the 222,444,666 coordinates? When I spawn the first one, it spawns at the exact coordinates, but if I spawn the second one and more, they all spawn only next to each other. This is my code:
    Code:
    local offset = vec3(0, 0, 0)
      local x = 222
      local y = 444
      local z = 666
      local vehPos = vec3(x, y, z)
      core_vehicles.spawnNewVehicle("cardboard_box", {config = 'vehicles/cardboard_box/large.pc', pos = vehPos, rot = vehRot})
    
    I would also like to learn how to specify the angle at which the box spawns, because the rotation on spawn seems random with this code.
     
    #4 Hank_Montgomery, Jun 4, 2024
    Last edited: Jun 4, 2024
  5. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    585
    They spawn around it to avoid spawning inside of each other. I'm not sure if there's a way to force spawning at a specific position. About the rotation, I don't see your "vehRot" variable?
     
  6. Hank_Montgomery

    Hank_Montgomery
    Expand Collapse

    Joined:
    Jun 9, 2022
    Messages:
    68
    Do you know how to change the vehicle's position after it's spawned? I appreciate the reply, btw.
    --- Post updated ---
    i've found this script, but it's for Lua triggers. I can't make it work with consoleNG:
    Code:
    local function teleportPlayer(data) 
     local vehicleName = data.subjectName 
     if data.event == 'enter' and data.subjectName ~= 'scenario_player0' then
     TorqueScript.eval(vehicleName..'.position = "28.505 158.914 369.664";') 
     end 
    end
    
     
  7. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    585
    You can set the position in two ways. One is setting the position without resetting the vehicle which is (the rotation parameter is a rotation offset):
    Code:
    local pos = vec3(0,0,0)
    local diffRot = quat(0,0,0,1)
    local veh = be:getPlayerVehicle(0)
    veh:setClusterPosRelRot(veh:getRefNodeId(), pos.x, pos.z, pos.z, diffRot.x, diffRot.y, diffRot.z, diffRot.w)
    The 2nd one is setting the position and resetting the vehicle which is (rotation here is an absolute rotation):
    Code:
    local veh = be:getPlayerVehicle(0)
    local pos = vec3(0,0,0)
    local rot = quat(0,0,0,1)
    veh:setPositionRotation(pos.x, pos.z, pos.z, rot.x, rot.y, rot.z, rot.w)
     
    • Like Like x 1
  8. Hank_Montgomery

    Hank_Montgomery
    Expand Collapse

    Joined:
    Jun 9, 2022
    Messages:
    68
    Redacted by mods
     
    #8 Hank_Montgomery, Jun 7, 2024
    Last edited: Feb 13, 2025
  9. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    585
    Nice :) You can use "core_vehicles.removeAll()" to remove all of them
     
    • 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