Using lua to switch cameras

Discussion in 'Utilities and programming' started by ThreeDTech21, Apr 5, 2022.

  1. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    I have made it so that when a beam deforms slow motion is automatically activated.

    Code:
    -- only being called if the beam has deform triggers
    function onBeamDeformed(id, ratio)
      bullettime.set(8)
      enterNextVehicle(player, step)
      beamstate.beamDeformed(id, ratio)
      controller.beamDeformed(id, ratio)
      bdebug.beamDeformed(id, ratio)
    end
    what I'm trying to do next is add a feature where the camera automatically switches to the next vehicle, then switches back to the previous vehicle. How do I add that?
     
    • Like Like x 1
  2. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    Use this to go to the next vehicle:
    Code:
    obj:queueGameEngineLua('be:enterNextVehicle(0, 1)')
    And this to go back:
    Code:
    obj:queueGameEngineLua('be:enterNextVehicle(0, -1)')
     
  3. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    Just Edited:

    Thanks! I used that but it crashes the game when beamDeform is activated, I crash the car and then the BeamNG.drive becomes unresponsive/hangs. I think whats happening is the vehicle switches constantly on each beamDeform which is probably hundreds of time's a second. Is there a way to have it switch vehicle only at first beamDeform ignoring all subsequent beamDeforms, then switch back after a few seconds elapse? What I'm ultimately trying to do is have the camera switch to the AI car that crashes (beamDeform) for a few seconds then switch back to the player car. I know it will take a lot of programing and files but for now I'm trying to see if I can get the logic works, that's why I'm using the main.lua file. I plan to create a separate process and game mode for the finished product. About all I know so far is that for a separate game mode (such as scenarios) I need alot of "local functions" that don't load when normal BeamNG loads so cars don't do this in free roam, only in the seperate game mode/scenario. Also is it possible to make it a selectable option similar to how you can choose "Muli-Seat" checkmark? I'd call it "Crash Mode".

    So heres a rough draft of the logic
    1. Your in the Player car chasing an AI car - You make the AI car crash
    2. switch vehicle to AI car (onbeamDeform)
    3. wait 4 seconds
    4. switch back to Player car
    Some Rules:

    1. The script never runs on the Player car or any car that the Player manually switches to.
    2. If 2 or more AI cars crash at the same time, then the logic will choose the closet car in distance to the Player car to run the script on
    3. This "Crash Mode" scenario is selectable like Muilt-Seat mode
    CrashMode.png

    The finish product will be similar to Burnout - Take Down crash cam


    programing all of this is going to take a lot but its a project I've been thinking about for several years, hope to make it come true with help from all of you!
     
    #3 ThreeDTech21, Apr 6, 2022
    Last edited: Apr 6, 2022
    • Like Like x 1
  4. a plane guy

    a plane guy
    Expand Collapse

    Joined:
    Mar 14, 2021
    Messages:
    4,142
    Oh your back,
    I thought you left the forums since you havent posted in a while
     
  5. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    Ive been away for quite a while, been doing alot of different things, just got back into IT Networking and some programing and figured its time to return to my roots! Lots of recourses to update and projects to try out. My time is still limited though because of school but I'm looking to create some more Mods.
     
    • Like Like x 1
  6. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    The onBeamDeform function isn't something you can hook to/receive events from and is only local to the main.lua file. But there is another way by using the damageTracker.lua module. I've written up several lines of code that prints to the console when another car has been damaged heavily. Place the attached zip file in the mods folder or unpack there. This probably will be a good starting point for you :)
     

    Attached Files:

    • Like Like x 1
  7. danielr

    danielr
    Expand Collapse

    Joined:
    Jan 6, 2014
    Messages:
    81
    Nice idea, a burnout style slowmotion camera mode sounds cool. I'm currently working on something similar (automatic camera director for AI races)

    So just in case you also want to change the camera modes, one GE Lua command would be:
    Code:
    core_camera.setByName(0, "orbit")
    The standard cam modes would be: "orbit", "driver", "onboard.hood", "external", "relative", "chase"

    @angelo234 Thanks for that starting point, it'll help me out as well.
     
    • Like Like x 2
  8. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    Awesome start thanks! Nice touch that it stops after the car is already damaged. I wish I knew how to code in Lua. I installed the mod but I don't see anything happening, I have the messages app up. I put the zip file in the mods folder. Ill try a few things today.
     
    #8 ThreeDTech21, Apr 8, 2022
    Last edited: Apr 8, 2022
  9. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    It outputs to the console (press ~)
     
  10. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    It could just be my computer but im seeing this in the console. I dont know what it all means, maybe I have a setting wrong
    error.png
     
  11. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    Alright this should fix it
     

    Attached Files:

  12. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    It works! Thanks this is certainly a good start. Ill have to learn more lua and see what else I can do on my end. ill try to get the code to activate camera switches and get distance information.
    Test1.png
     
  13. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    Nice and I don't think you need to make a choice on which car to choose since its basically impossible for two cars to crash at exactly the same time down to the millisecond lol. And even if somehow they both crash at the exact millisecond, one of the cars will always let you know they crashed first.
     
  14. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    True it even list it that way in the console, the first crash car is listed first perhaps miliseconds before the 2nd crashed car ;)
     
  15. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    Thanks! definitely going to be a good cam mode

    I got a portion of it working! it triggers slow motion and switches the car. But when there are multiple cars it does a random switch to any car, not to the damaged car.

    What I have to figure out is how to make the console printed car ID be the car it switches to. BeamNG seems to assign a random 4 digit number ID to all cars in the scene.

    Some issues I ran into running the current function.
    • When the camera is switched to the damaged car, the damaged car now becomes the player car and the script is allowed to run on the previous player car if it happens to get damaged during the function. I'll have to find a way to mark the start car as locked out of the function. - A potential overall fix is to somehow set only the camera focus to the damaged car but not switch the player to it.
    • Once the function runs it wont run again unless the cars are full reload using CTRL+R, pressing i or r to reset the car wont allow the script to run again.
    • I cant set the second function of obj:queueGameEngineLua('be:enterNextVehicle(0, -1)') switch back because it switches back so fast you don't see it move
    Ill keep looking for way to get it working but my current knowledge of Lua is beginner.

    This is what It looks like so far:


    Code:
    local M = {}
    
    -- threshold set to 0.05 for whats considered moderate damage
    -- to call onCarDamaged function, change it if you like
    local heavyDamageThreshold = 0.01
    
    local hasInit = false
    local isDamaged = false
     local player = 0
    
    function onCarDamaged()
        -- code executed when car is damaged
        bullettime.set(8)
        print(obj:getID() .. " car damaged!")
        obj:queueGameEngineLua('be:enterNextVehicle(0, 1)')
    end
    
    function damageUpdateCallback(damageData, damageDataDelta)
        -- if player in vehicle or already was moderately damaged before, stop
        if playerInfo.firstPlayerSeated or isDamaged or not damageDataDelta.body then return end
    
        local totalDamage = 0
     
        for k, v in pairs(damageDataDelta.body) do
            totalDamage = totalDamage + v
        end
     
        if totalDamage >= heavyDamageThreshold then
            onCarDamaged()
     
            isDamaged = true
        end
    end
    
    function updateGFX(dt)
        if not hasInit then
            if damageTracker then
                -- register callback function to get car damage events
                damageTracker.registerDamageUpdateCallback(damageUpdateCallback)
                hasInit = true
            end
        end
     
        -- main.lua only calls updateGFX if player in vehicle
        if not playerInfo.firstPlayerSeated then
            damageTracker.updateGFX(dt)
        end
    end
    
    M.updateGFX = updateGFX
    
    return M
     

    Attached Files:

    #15 ThreeDTech21, Apr 10, 2022
    Last edited: Apr 11, 2022
    • Like Like x 1
  16. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    539
    Hey that looks pretty neat :) I think the next step is to have the vehicle Lua scripts contact a central GE Lua script for queueing up/locking out other vehicles from triggering the slow-motion cutscene. And that's what I wrote up and this is the result which looks pretty cool I think:


    You think you can take it from here? :)
     

    Attached Files:

    • Like Like x 2
  17. ThreeDTech21

    ThreeDTech21
    Expand Collapse

    Joined:
    Sep 27, 2013
    Messages:
    1,616
    You did it! This was the concept I was trying to make when I first came to the forums! you make it look easy ha. The transitions are smooth, everything is just seamless I dont think I can improve on that :cool:. I'll have to learn more coding before I can really start to add stuff. Currently learning PowerShell and Bash for networking but haven't gotten into Bash scripting yet or Python. Once I get a working knowledge of classes, global modifiers, libraries and all that I can start to make some decent mods. Cheers!
     
    • 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