Solved How can I make a custom button on the UI, like the jato and n2o.

Discussion in 'Mod Support' started by señor Walter Blanco, Aug 8, 2023.

  1. Diamondback

    Diamondback
    Expand Collapse
    Vehicle Systems Lead
    BeamNG Team

    Joined:
    Apr 8, 2014
    Messages:
    1,966
    Using these buttons is pretty simple.
    As you found out already, the relevant call is this:
    Code:
    extensions.ui_simplePowertrainControl.setButton("my_id", "UI Name", "powertrain_jato", "0072bc", 1, "print('button clicked!')")
    Feel free to look at the relevant extension code to get a better idea of what it does.

    Here's the params explained:
    1. a unique identifier, make sure you are not using something existing
    2. the tool tip when hovering the button
    3. an icon to be displayed, can be left empty if you wish
    4. the color of the progress ring if you want to use that
    5. the value of the progress ring from 1 (full ring) to 0 (no ring)
    6. the vlua code executed within the current vehicle when the button is clicked
    You need to call this method any time you want to change something about the button and when the vehicle changes. The second part is taken care of by the
    updateSimpleControlButtons method if set up correctly like in official content. Updating the button when its data changes is up to you though.

    Please make sure to not just blindly update it every frame, that's not very efficient.
     
    • Like Like x 1
  2. señor Walter Blanco

    señor Walter Blanco
    Expand Collapse

    Joined:
    May 31, 2023
    Messages:
    15
    I'm running into a problem with LUA itself, I defined JetsEnable as 1 multiple times here
    Code:
    local M = {}
    
    M.type = "auxiliary"
    M.defaultOrder = 500
    
    local rpm = 0
    
    local outputmin = 0
    local outputmax = 1
    local thrust = 0
    local indicator = 1
    local JetsEnable = 1
    electrics.values.Jet = 0
    
    local lastFuelRatio = 1
    
    local function init(jbeamData)
    JetsEnable = 1
    if JetsEnable ~= nil then JetsEnable = 1 end
    min = jbeamData.min or 4000
    max = jbeamData.max or 6000
    end
    
    local function updateSimpleControlButtons()
      extensions.ui_simplePowertrainControl.setButton("jato_" .. M.name, "Jets", "powertrain_jato", "0072bc", JetsEnable, "JetsEnable = 1 - (JetsEnable)")
    end
    
    local function rpmthrust(rpm, min, max, outputmin, outputmax)
      local output = (rpm - min) / (max - min) * (outputmax - outputmin) + outputmin
      if output > outputmax then return outputmax
      else return output
      end
    end
    
    local function updateGFX(dt)
    
      rpm = electrics.values.rpm
     
      if rpm >= min then
        thrust = rpmthrust(rpm, min, max, outputmin, outputmax)
        indicator = thrust
      else
        thrust = 0
        indicator = 0.01
      end
    
     
      electrics.values.Jet = thrust * electrics.values.throttle_input * JetsEnable
      updateSimpleControlButtons()
    
    end
    
    M.updateGFX = updateGFX
    M.init = init
    
    M.updateSimpleControlButtons = updateSimpleControlButtons
    
    return M
    yet it keeps telling me that it is a nil value?
     
  3. señor Walter Blanco

    señor Walter Blanco
    Expand Collapse

    Joined:
    May 31, 2023
    Messages:
    15
    Since I didn't get a response, I used the good ol' f*ck around and find out technique, and realized that it didn't work because the int was defined as a local, and the function was either unable to access or change it. thank you to both @Diamondback and @Agent_Y for helping me and whoever finds this thread thru google search, the problem is now resolved
     
  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