1. Trouble with the game?
    Try the troubleshooter!

    Dismiss Notice
  2. Issues with the game?
    Check the Known Issues list before reporting!

    Dismiss Notice
  3. Before reporting issues or bugs, please check the up-to-date Bug Reporting Thread for the current version.
    0.30 Bug Reporting thread
    Solutions and more information may already be available.

Vehicle LUA Function Troubleshooting

Discussion in 'Troubleshooting: Bugs, Questions and Support' started by CRGZ, Nov 13, 2021.

  1. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    Hello all, I have a quick question about lua that's been holding me back on my computer mod.
    I have this function:
    local function rainbow_Mode(COLOR_1, COLOR_2, COLOR_3, Roughness)
    local objID = be:getPlayerVehicleID(0)
    local veh = be:getObjectByID(objID)
    local vd = extensions.core_vehicle_manager.getVehicleData(objID)
    vd.config.paints = vd.config.paints or {}
    local paint = createVehiclePaint({x = COLOR_1 / 1000, y = COLOR_2 / 1000, z = COLOR_3 / 1000, w=2}, {1, Roughness, 0, 0})

    vd.config.paints[3] = paint
    extensions.core_vehicle_manager.liveUpdateVehicleColors(objID, veh, 3, paint)

    end

    and I'm trying to call it within the updateGFX(dt) function using this:
    local function updateGFX(dt)
    if electrics.values['multicolor_input'] == 1 then
    rainbow_Mode(COLOR_1, COLOR_2, COLOR_3, Roughness)
    else
    electrics.values['multicolor_input'] = 0
    end
    end

    The argument local objID = be:getPlayerVehicleID(0) from the rainbow_Mode function is what I think is breaking my code. It can't seem to get the vehicle ID whatsoever.

    If you need more information, ask here.

    Thanks
    -CRGZ
     
  2. Agent_Y

    Agent_Y
    Expand Collapse
    Jbeam/QA support
    BeamNG Team

    Joined:
    Jul 10, 2020
    Messages:
    10,058
    If it's local and declared inside of the function, it probably won't be seen outside of the function, so I guess that's what is breaking it. You can try moving the declaration out of the function to the top of the file, and in the function only assign the value.
     
  3. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    I have no experience on 'color coding' so far but my first suggestion is to look at your IF-THEN-ELSE statement in your update function :

    the ELSE statement says to shut of electrics.values.yourInput which is fine but irrelevant in this case imo. I think you've put that in to secure your statement loop (I'd understand) but the game's code will already do that cause if elec.val.inp is NOT 1 then it's already 0.
    Try to leave ELSE out first and only make an ELSE statement if needed. In your case it would be a different one I guess though.

    Furthermore try to print all your variables from your function to see if they are detected. Example : -- print(objID) and so on.. To actually print remove the -- and check the in-game console.

    Also have you tried to declare your function to the public interface ?

    -- public interface
    M.rainbow_Mode(COLOR_1, COLOR_2, COLOR_3, Roughness) = rainbow_Mode(COLOR_1, COLOR_2, COLOR_3, Roughness)

    Lastly I see you making (key) calls from the core files (.config.paints and extensions.core_vehicle_manager.getVehicleData)
    I don't know if they work or not, I have not much experience with the color stuff so if you're sure they work then they are fine I guess.
     
  4. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    I printed many of the statements, which almost all gave "nil". Printing the variable objID prints "nil", but printing the value "objID" is set to, which is be:getPlayerVehicleID(0), gives me the ID of 22441. Everything else is nil. It looks like objID can't define itself to the vehicleID number for some reason.
    --- Post updated ---
    Declaring the local variables outside the scope just breaks the physics of the mod quicker. It just spawns nothing, as in no mesh, and no controls.
     
  5. NOCARGO

    NOCARGO
    Expand Collapse

    Joined:
    Apr 1, 2019
    Messages:
    1,514
    Did you try adding your function to the public interface as well ?
    --- Post updated ---
    And then try to scope your vars by printing to the console ?
     
  6. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    Yes for the public interface.
    Yes for the public interface
    upload_2021-11-13_14-17-22.png
    What do you mean scoping my vars?
     

    Attached Files:

    • upload_2021-11-13_14-17-11.png
  7. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    The problem is that most of those functions you are trying to call are exclusive to Game Engine Lua unfortunately. So basically you need to find the equivalent Vehicle Lua functions to change the color of the vehicle.
    --- Post updated ---
    An easy way to get this working is just by placing that public function in a Game Engine Lua script file and calling it from Vehicle Lua (not actually running it in the Vehicle Lua environment).

    I've attached a template for you to use to make things easy and follow these steps:
    1. Place the zip file in the mods folder
    2. Open the game and go to Repository > Mods Manager > click on themod mod
    3. Click unpack
    4. Go to mods/unpacked/themod/scripts/themod/extension.lua file and paste in that existing rainbow_Mode function code into the function there
    5. Add your Vehicle Lua file to the mod and call the GE Lua function from your updateGFX function with:
      • Code:
        obj:queueGameEngineLua("scripts_themod_extension.rainbow_Mode()")

    And everytime you modify the GE Lua code, press Ctrl + L to upload the new code to the game. And Let me know if you need help and hope this helped :)
     

    Attached Files:

    • Like Like x 1
  8. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    Beam's wiki shows very outdated or not very useful information. I took a look at this forum: https://www.beamng.com/threads/a-way-to-get-a-value-from-system-lua-t3d-lua-gameengine-lua.25139/ and tried implementing both solutions into my code, though it still returns a nil value. I'm not sure if I have to delete/change the code a certain way in order to return the correct value or not
     
  9. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Its going to be a lot of work trying to call each GE Lua function separately and getting their return values. Give my method a shot since its the easiest way to handle this.
     
  10. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    So yes, I replaced my lua file with yours, and ran the obj in console, it worked, though how would I combine everything all together to make the function actually output rainbowness?

    Also thank you so much
     
  11. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    Oh right I forgot to add the arguments to the function call. Just do it like this (hopefully I didn't mess up the string formatting lol):

    Code:
    local COLOR_1, COLOR_2, COLOR_3, Roughness = x, y, z, r
    
    obj:queueGameEngineLua("scripts_themod_extension.rainbow_Mode(" .. COLOR_1 .. "," .. COLOR_2 .. "," .. COLOR_3 .. "," .. Roughness .. ")")
    --- Post updated ---
    Just copy the existing code you had in the rainbow_Mode function and paste it into the new function if that's what you're wondering?
     
  12. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    I don't think I'm following, though here is my MAIN lua file:
    upload_2021-11-13_20-4-2.png
    Here is the edited mod extension lua file that you gave me:
    upload_2021-11-13_20-4-44.png
     
  13. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    You've got it down but just replace x,y,z,r with actual numbers for the colors and roughness and you don't need that empty rainbow_Mode function in Vehicle Lua anymore.
     
  14. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    Oooh, it works. Thank you so much. But it doesn't randomize. How would I make it dynamically randomize the colors?
     
  15. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
  16. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    Unfortunately I'm slow and don't understand lua as much as it seems.

    So I copied the function(s) into my code, therefore it looks like this
    upload_2021-11-14_0-1-15.png
    So now the issue is using that "components" variable, which was returned within the function "getRainbowRGB(num)". Using it directly within the xyz of the local color function I had trouble with before just broke the game. I'm trying to figure out how to use the components[1], components[2],components[3] in my RGB values. I have the solution in my head though I can't seem to pull through with it. May I have the last bit of help?

    Thank you so much!
     
  17. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    So the getRainbowRGB function returns 3 variables so just set your COLOR variables like this:
    Code:
    local COLOR_1, COLOR_2, COLOR_3 = getRainbowRGB(time)
    local Roughness = 0.25
    And then you can set up a timer variable (increment it in the updateGFX function by dt) to plug into the getRainbowRGB function so that it changes color with time.
     
    • Like Like x 1
  18. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    Unfortunately that didn't work.
    upload_2021-11-14_14-50-18.png
    I created the time variable and put it as my input for the getRainbowRGB function, also I incremented the time by dt, (on line 39, I just put time = time * dt, instead of time = time + dt because I tried both, and they still didn't work out. In game, the input works, though it changes to a very specific red/black color, with no dynamic change at all.
     
  19. angelo234

    angelo234
    Expand Collapse
    Programmer
    BeamNG Team

    Joined:
    Aug 11, 2017
    Messages:
    540
    It needs to be time = time + dt but try setting time back to 0 once its greater than 1 since it sounds like the parameter needs to be between 0 and 1.
     
  20. CRGZ

    CRGZ
    Expand Collapse

    Joined:
    Sep 12, 2020
    Messages:
    115
    My god it works. Thank you so much for all your help man. I appreciate all of your time. Can't thank you enough bruh. If you need anything, I'll try to offer a helping hand. Anyways, have an amazing day!
     
    • 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