Solved Programmatically manipulate World Editor objects

Discussion in 'Mod Support' started by HighBeam9000, Nov 14, 2022.

  1. HighBeam9000

    HighBeam9000
    Expand Collapse

    Joined:
    Aug 2, 2021
    Messages:
    265
    Hello.

    I can add and delete objects and forestItems via the World Editor, but can I programmatically do this, maybe through Lua or C? I figured out how to add, delete and move vehicle/prop type objects with Lua, but now I'm trying to do the same with non-vehicle objects, and I'm not sure if it's even possible.

    Thank you. :)
     
  2. meywue

    meywue
    Expand Collapse
    Administrator
    BeamNG Team

    Joined:
    Nov 19, 2015
    Messages:
    340
    In order to remove forest items you need to get the ForestData object first from which you can remove the ForestItems.

    Something like this would get rid of all forest items of the forest with the name "theForest":
    Code:
    local forest = scenetree.findObject("theForest")
    local forestData = forest:getData()
    
    for _, data in ipairs(forestData) do
      forestData:removeItem(data)
    end
    for some more references you can look at the following lua files:
    lua\ge\extensions\editor\forestEditor.lua
    lua\ge\extensions\core\forest.lua
    lua\ge\extensions\editor\api\forest.lua

    Those are the ones the world editor is using to add, remove, etc forest items
     
    #2 meywue, Nov 14, 2022
    Last edited: Nov 14, 2022
    • Agree Agree x 1
  3. HighBeam9000

    HighBeam9000
    Expand Collapse

    Joined:
    Aug 2, 2021
    Messages:
    265
    Ah-ha! Thank you, very much. This will help a lot.

    There's a very small section of Italy that I'd like to customize, but I want to leave the original map intact. Rather than copying the entire level and editing just that section (it's only a few edits), I'm attempting to create a script that removes some items, then adds others. I can then load regular Italy, run the script, and I nearly instantly have my own special version of Italy.

    Thanks again! :D
     
  4. HighBeam9000

    HighBeam9000
    Expand Collapse

    Joined:
    Aug 2, 2021
    Messages:
    265
    I think I have almost everything figured out. I can programmatically place objects via Lua, just like I can through the World Editor. However, I'm having an issue with collisions.

    When using the World Editor
    I can place objects, but collisions don't happen until I set the collisionType and decalType to "Visible Mesh Final", AND I close the World Editor. Then collisions work fine.

    When using a Lua script:
    Here's the block of code of interest:
    Code:
    -- Spawn TSStatic
    local hcar = createObject('TSStatic')
    if hcar == nil then
        log("E", "Utility_Script", "Cannot create TSStatic!")
        return
    end
      
    -- Set as helicarrier
    hcar:setField('shapeName', 0, "/levels/" .. mapName .. "/art/shapes/race/gerryfrod.dae")
      
    -- Set position, rotation and scale
    local quat = quatFromEuler(0, 0, math.pi)
    hcar:setPosRot(0, 0, 30, quat.x, quat.y, quat.z, quat.w)
    hcar.scale = vec3(7, 7, 7)
    
    -- Set collision map stuff
    hcar:setField('collisionType', 0, "Visible Mesh Final")
    hcar:setField('decalType', 0, "Visible Mesh Final")
    log("D", "Utility_Script", "Collision set 4")
    
    -- Register helicarrier
    hcar.canSave = false
    hcar:registerObject('')
    
    The code sets the collisionType and decalType fields as "Visible Mesh Final", and I can open the World Editor and see those fields are set like that, but collisions don't happen. While in the World Editor, if I set the fields back to "Collision Map", then set them again to "Visible Mesh Final", then close the World Editor, collisions work fine.

    Is the World Editor doing some extra magic to make collisions work that I'm failing to do in my script? I found the function setEditorActiveInternal() in main.lua and tried some of the stuff I found in there, such as editor.rebuildCollision(), but I still couldn't get collisions to work. Please, teach me this sorcery.
     
    #4 HighBeam9000, Nov 25, 2022
    Last edited: Nov 25, 2022
  5. _N_S_

    _N_S_
    Expand Collapse

    Joined:
    Oct 28, 2017
    Messages:
    66
    Have you tried "be:reloadStaticCollision(false)"?
     
    • Like Like x 1
  6. HighBeam9000

    HighBeam9000
    Expand Collapse

    Joined:
    Aug 2, 2021
    Messages:
    265
    Not yet, but I will tonight. Thank you. I’ll let you know what happens.

    UPDATE: It works! Thank you!
     
    #6 HighBeam9000, Nov 27, 2022
    Last edited: Nov 27, 2022
    • Like Like x 1
  7. Car Devastator

    Car Devastator
    Expand Collapse

    Joined:
    May 5, 2016
    Messages:
    66
    Do you know if it's possible to change rotation of a non-vehicle object in lua? It's simple to change its position thanks to setPosition() but I tried setPositionRotation() and it seems it works only for vehicles, not a static objects
    --- Post updated ---
    OK, so actually I've just tested it out and it seems setPosRot() is a different thing and it works for a non-vehicle objects aswell, pretty interesting
     
    #7 Car Devastator, Jan 16, 2023
    Last edited: Jan 16, 2023
  8. HighBeam9000

    HighBeam9000
    Expand Collapse

    Joined:
    Aug 2, 2021
    Messages:
    265
    Yes, setPosRot is what you need. I have a script that spawns the Motorsports Playground aircraft carrier wherever I want. The script is in a zip file here:
    https://www.beamng.com/threads/naval-warfare.87940/page-8#post-1523113

    You can examine the script to see what I did. The script spawns lights for the carrier too.

    One item of note: "TheForest" is a special object in World Editor. TheForest is an object on the same level as other non-vehicle objects in World Editor. However, TheForest has sub objects, like trees, shrubs, some rocks, etc. Although I haven't programmatically manipulated TheForest sub objects, they have their own manipulation functions (add, move, delete, etc).You'll probably notice the Forest Tools in the World Editor. These tools manipulate TheForest sub objects using functions that you can call too.
     
    • 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