Modding, Mission Design, and Coding > The Scripting Workshop

[Example] How to setup a custom scripted HUD Gauge

(1/1)

wookieejedi:
A few folks were interested in seeing the nuts and bolts of how to setup a custom scripted HUD gauge, so I have attached an example of how to do that here. All of this functionality is thanks to m!m's work completing that specific feature request!


--- Code: -----How to get script cockpit gauge to work with RTT cockpits


Example framework for setting up a custom scripted HUD gauge that works with RTT
--[[
    Add this to hud_gauges.tbl (or xxx-hgd.tbm)
$Gauges:
;; The Wingman Status is mostly set by cuswingmengag-sct.tbm.
;; Only aspect needed here is to set size of gauge with the +Scripted Gauge
;+Wingman Status:
+Scripted Gauge:
Origin: (0, 0)
Offset: (0, 0)
Cockpit Target: awing_HUD ;;--this name must match what the <Cockpit Target> string is in ships.tbl or xxx-shp.tbm
Canvas Size: (183, 130)
Display Offset: (660, 245)
Display Size: (285, 235)
Name: Custom_Gauge_RTT ;; Needed for cuswingmengag-sct.tbm to work.

--]]


--Example of working script file
--For a complete example that takes into account camera movements, HUD change colors, cutscences, gauge toggling, etc see the custom wingmen gauge script
--custom wingmen script located here: https://github.com/wookieejedi/Custom-Wingmen-Gauge/blob/master/cuswingmengag-sct.tbm

#Conditional Hooks
$Application: FS2_Open

$On Game Init: [
   
    CustomGauge = {}
    CustomGauge.isactive = true

--To use gauge with RTT display set:
--CustomWingGauge.RTT = {use=true}
--If use == true, then the script will use RTT in internal view then switch to screen render for external view.
--Also, for RTT to work be sure to make a '+Scripted Gauge:' entry in hud_gauges.tbl.
--Note 'gauge_name' should equal the 'Name' of the '+Scripted Gauge' that was created in the 'hud_gauges.tbl'.
CustomWingGauge.RTT = {use=true, gauge_name="Custom_Gauge_RTT"}
   
    function CustomGauge:Draw(gauge)
       
        --setup
        --this function draws a shape either using gr.draw or gauge:draw methods
        --if gauge is nil then we use gr.draw... if gauge is not nil then we use gauge:DrawString (etc)
        local function dr(functype, arglist)
--string
if functype == "drawString" then
local s = arglist[1] or ""
local x = arglist[2] or 0
local y = arglist[3] or 0
if gauge ~= nil then
gauge:drawString(s, x, y)
else
gr.drawString(s, x, y)
end
end
--line
if functype == "drawLine" then
local x1 = arglist[1] or 0
local y1 = arglist[2] or 0
local x2 = arglist[3] or 0
local y2 = arglist[4] or 0
if gauge ~= nil then
gauge:drawLine(x1, y1, x2, y2)
else
gr.drawLine(x1, y1, x2, y2)
end
end
--rectangle
if functype == "drawRectangle" then
local x1 = arglist[1] or 0
local y1 = arglist[2] or 0
local x2 = arglist[3] or 0
local y2 = arglist[4] or 0
local filled = arglist[5]
if filled == nil then filled = true end
if gauge ~= nil then
gauge:drawRectangle(x1, y1, x2, y2, filled)
else
gr.drawRectangle(x1, y1, x2, y2, filled)
end
end
--circle
if functype == "drawCircle" then
local r = arglist[1] or 1
local x = arglist[2] or 0
local y = arglist[3] or 0
local filled = arglist[4]
if filled == nil then filled = true end
if gauge ~= nil then
gauge:drawCircle(r, x, y, filled)
else
gr.drawCircle(r, x, y, filled)
end
end

end

        --actually draw something for our custom gauge. For our purposes we are just going to draw a line
        dr("drawString", {"Here lies the custom scripted gauge", 0, 0})
        dr("drawLine", {0, 0, 20, 0})

    end
]

$State: GS_STATE_GAME_PLAY

$On Gameplay Start: [
if CustomGauge.isactive then

--check if cockpit display exists and if user wants to use RTT
CustomGauge.RTT.isactive = false
local player = hv.Player
if CustomGauge.RTT.use and player ~= nil and player:isValid() then
local pcockpit = player.CockpitDisplays
if pcockpit ~= nil and pcockpit:isValid() then
if pcockpit[1] ~= nil and pcockpit[1]:isValid() then
CustomGauge.RTT.isactive = true
end
end
end

--setup custom scripted gauge render
local gname = CustomGauge.RTT.gauge_name
if gname ~= nil then
CustomGauge.RTT.use = true
if hu.getHUDGaugeHandle(gname) ~= nil and CustomGauge.RTT.use then
hu.getHUDGaugeHandle(gname).RenderFunction = function(gauge)
if gr.hasViewmode(VM_INTERNAL) or gr.hasViewmode(VM_TRACK) then
--runs the gauge drawing feature (we don't have to have this under HUD Draw because using CustomGauge.RTT.use runs ever frame)
CustomGauge:Draw(gauge)
end
end
end
end

end
]

$On HUD Draw: [
--if ww are not in cockpit mode then draw the gauge normally
    if hu.HUDDrawn and mn.getMissionTime() > 0.1 and CustomGauge.isactive then
if not CustomGauge.RTT.isactive or ( CustomGauge.RTT.isactive and gr.hasViewmode(VM_CHASE) ) then
CustomGauge:Draw()
end
end
]

#End
--- End code ---

Navigation

[0] Message Index

Go to full version