Search Results for

    Show / Hide Table of Contents

    Engine RPM and landing gear

    中文 | English

    Run this command at the repository root to replay the examples through the local Unity MCP.

    python Docs/Manual/manual/tools/tutorials.py run lua-api2-engine-gear
    
    Click to expand MCP actionsClick to collapse MCP actions

    Prepare the teaching sample. The recipe provides ${variables}.

    {
      "variables": {
        "script": "${fixture}/EngineAndGear.lua",
        "prefab": "${fixture}/Tutorial_EngineAndGear.prefab"
      },
      "calls": [
        {
          "tool": "invoke",
          "arguments": {
            "path": "static:UnityEditor.AssetDatabase/CreateFolder",
            "args": [
              "${fixtureParent}",
              "${folder}"
            ]
          },
          "expect": {
            "contains": "(String)"
          }
        }
      ]
    }
    

    Attach the script to a vehicle. Assign fan, smoke or gearLights as needed. On aircraft, read the initial gear state and update the lights with FlightAPI.OnGearChanged. Pass self.script as the subscription owner and turn off per-frame updates for this branch.

    The ground branch uses TankAPI.GetEngineState, which returns success, RPM, RPM percentage, gear, speed and reverse status. The RPM percentage runs from 0 to 100; multiply RPM by 6 to get degrees per second. Speed is in km/h and is negative in reverse for tracked and wheeled vehicles. Tracked vehicles report first gear as 1 and reverse as 0. A wheeled vehicle without a gearbox reports RPM 0 and gear 1. Ships report RPM and speed, always gear 1 and reversing false. Fixed vehicles report RPM and speed 0, gear 1. The example scales the fan speed by 0.1 and shows smoke above 80 percent RPM.

    FlightAPI.IsOnGround checks ground contact. With the gear down, it can also run one physics overlap per wheel and return true for nearby ground or buildings. Limit the frequency of this physics query if the visual update runs often.

    Behavior()
    Property = {
        fan = { type = "object", value = nil },
        smoke = { type = "object", value = nil },
        gearLights = { type = "object", value = nil },
    }
    
    local EngineAndGear = class("EngineAndGear")
    
    function EngineAndGear:OnVehicleReady(vehicle)
        self.vehicle = vehicle
        if VehicleAPI.IsFlightVehicle(vehicle) then
            if self.gearLights ~= nil then
                self.gearLights:SetActive(FlightAPI.IsGearDown(vehicle))
                FlightAPI.OnGearChanged(self.script, vehicle, function(isDown)
                    self.gearLights:SetActive(isDown)
                end)
            end
            self.vehicle = nil
            self.script:SetUpdateEnabled(false)
            return
        end
        self.script:SetUpdateEnabled(self.fan ~= nil or self.smoke ~= nil)
    end
    
    function EngineAndGear:OnUpdated(dt)
        if self.vehicle == nil then
            return
        end
        local ok, rpm, rpmPercent = TankAPI.GetEngineState(self.vehicle)
        if not ok then
            return
        end
        if self.fan ~= nil then
            self.fan.transform:Rotate(0, 0, rpm * 6 * dt * 0.1)
        end
        if self.smoke ~= nil then
            self.smoke:SetActive(rpmPercent > 80)
        end
    end
    
    return EngineAndGear
    
    Click to expand MCP actionsClick to collapse MCP actions
    [
      {
        "tool": "invoke",
        "arguments": {
          "path": "static:System.IO.File/Copy",
          "args": [
            "../../Docs/Manual/tutorials/lua-api2-engine-gear/example.lua",
            "${script}"
          ]
        },
        "expect": {
          "contains": "OK:"
        }
      },
      {
        "tool": "invoke",
        "arguments": {
          "path": "static:UnityEditor.AssetDatabase/ImportAsset",
          "args": [
            "${script}",
            "ForceSynchronousImport"
          ]
        },
        "expect": {
          "contains": "OK:"
        }
      },
      {
        "tool": "prefab_edit",
        "arguments": {
          "json": {
            "path": "${prefab}",
            "mode": "create",
            "component": "ShanghaiWindy.Core.Lua.LuaBehaviorMono",
            "children": [
              "Fan",
              "Smoke",
              "GearLights"
            ],
            "references": [
              {
                "property": "luaBehavior",
                "asset": "${script}",
                "type": "ShanghaiWindy.Core.Lua.LuaBehaviorScript"
              },
              {
                "property": "instanceLuaProperty.objectProperties.Array.data[0].value",
                "child": "Fan",
                "component": "UnityEngine.GameObject"
              },
              {
                "property": "instanceLuaProperty.objectProperties.Array.data[1].value",
                "child": "Smoke",
                "component": "UnityEngine.GameObject"
              },
              {
                "property": "instanceLuaProperty.objectProperties.Array.data[2].value",
                "child": "GearLights",
                "component": "UnityEngine.GameObject"
              }
            ],
            "values": {
              "instanceLuaProperty": {
                "objectProperties": [
                  {
                    "variableName": "fan"
                  },
                  {
                    "variableName": "smoke"
                  },
                  {
                    "variableName": "gearLights"
                  }
                ]
              }
            }
          }
        },
        "expect": {
          "equals": {
            "ok": true
          }
        }
      },
      {
        "tool": "inspect",
        "arguments": {
          "path": "asset:${prefab}/LuaBehaviorMono/luaBehavior/name"
        },
        "expect": {
          "contains": "(String) EngineAndGear"
        }
      },
      {
        "tool": "inspect",
        "arguments": {
          "path": "asset:${prefab}/LuaBehaviorMono/instanceLuaProperty/objectProperties/0/value/name"
        },
        "expect": {
          "contains": "(String) Fan"
        }
      },
      {
        "tool": "inspect",
        "arguments": {
          "path": "asset:${prefab}/LuaBehaviorMono/instanceLuaProperty/objectProperties/1/value/name"
        },
        "expect": {
          "contains": "(String) Smoke"
        }
      },
      {
        "tool": "inspect",
        "arguments": {
          "path": "asset:${prefab}/LuaBehaviorMono/instanceLuaProperty/objectProperties/2/value/name"
        },
        "expect": {
          "contains": "(String) GearLights"
        }
      }
    ]
    

    The replay supplies controlled RPM and gear values to test the script branches. Check the fan, smoke and lights on a real vehicle.

    Click to expand MCP actionsClick to collapse MCP actions
    [
      {
        "tool": "invoke",
        "arguments": {
          "path": "static:ShanghaiWindy.Core.AssetBundleManager/LuaEnv/DoString",
          "args": [
            "local E = setmetatable({ Behavior = function() end, class = function() return {} end, print = function() end }, { __index = _G })\nlocal gearCallback, angle, smoke, light\nlocal update\nE.VehicleAPI = { IsFlightVehicle = function(v) return v.aircraft end }\nE.FlightAPI = {\n    IsGearDown = function() return true end,\n    OnGearChanged = function(owner, vehicle, cb) gearCallback = cb end\n}\nE.TankAPI = { GetEngineState = function() return true, 360, 82, 1, 10, false end }\n\nlocal B = assert(loadfile('../../Docs/Manual/tutorials/lua-api2-engine-gear/example.lua', 't', E))()\nlocal function instance(t) return setmetatable(t, { __index = B }) end\nlocal script = { SetUpdateEnabled = function(_, value) update = value end }\nlocal lights = { SetActive = function(_, value) light = value end }\nlocal fan = { transform = { Rotate = function(_, x, y, z) angle = z end } }\nlocal exhaust = { SetActive = function(_, value) smoke = value end }\nlocal aircraft = instance({ gearLights = lights, script = script })\naircraft:OnVehicleReady({ aircraft = true })\nassert(light == true and update == false)\ngearCallback(false)\nassert(light == false)\naircraft:OnUpdated(1)\nassert(angle == nil)\nlocal tank = instance({ fan = fan, smoke = exhaust, script = script })\ntank:OnUpdated(1)\nassert(angle == nil)\ntank:OnVehicleReady({})\nassert(update == true)\ntank:OnUpdated(0.5)\nassert(math.abs(angle - 108) < 0.001 and smoke == true)\nlocal ok = CS.ShanghaiWindy.Core.API.TankAPI.GetEngineState(nil)\nassert(ok == false)\nreturn \"Lua example passed\"\n",
            "manual-lua-api2-engine-gear",
            "null"
          ]
        },
        "expect": {
          "contains": "Lua example passed"
        }
      }
    ]
    
    In this article
    Back to top Panzer War Document