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.
     
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    542
    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 1
  3. EdxTm

    EdxTm
    Expand Collapse

    Joined:
    May 12, 2019
    Messages:
    17
    awesome, exactly what I wanted! Thank you very much!
     
  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