Search Results for

    Show / Hide Table of Contents

    Vehicle initialization timing

    中文 | 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-vehicle-init-timing
    
    Click to expand MCP actionsClick to collapse MCP actions

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

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

    On Lua API 2 or later, put code that needs assembled vehicle components in OnVehicleReady. The script runs OnStarted first. If the vehicle is already assembled, OnVehicleReady follows immediately. Otherwise it runs once after assembly.

    Bind the script to a LuaBehaviorMono on the vehicle. Put your component setup in InitVehicleDependencies. On older clients, resolve the vehicle with TryGetTankInitSystem, then check IsLoaded or subscribe to the loaded event. The example removes its legacy subscription when the behaviour is destroyed.

    Behavior()
    Property = {}
    
    local VehicleTiming = class("VehicleTiming")
    
    function VehicleTiming:OnVehicleReady(vehicle)
        if vehicle == nil or self.vehicle ~= nil then
            return
        end
        self.vehicle = vehicle
        self:InitVehicleDependencies()
    end
    
    function VehicleTiming:InitVehicleDependencies()
        print("[VehicleTiming] vehicle is ready")
    end
    
    function VehicleTiming:OnStarted()
        if (GAME_API_VERSION or 1) >= 2 then
            return
        end
        local found, vehicle = self.script:TryGetTankInitSystem()
        if not found or vehicle == nil then
            return
        end
        if vehicle.IsLoaded then
            self:OnVehicleReady(vehicle)
            return
        end
        self.legacyVehicle = vehicle
        self.legacyLoaded = function()
            self:OnVehicleReady(vehicle)
        end
        VehicleAPI.RegisterVehicleLoadedEvent(vehicle, self.legacyLoaded)
    end
    
    function VehicleTiming:OnDestroyed()
        if self.legacyLoaded ~= nil then
            VehicleAPI.UnRegisterVehicleLoaedEvent(self.legacyVehicle, self.legacyLoaded)
            self.legacyVehicle = nil
            self.legacyLoaded = nil
        end
    end
    
    return VehicleTiming
    
    Click to expand MCP actionsClick to collapse MCP actions
    [
      {
        "tool": "invoke",
        "arguments": {
          "path": "static:System.IO.File/Copy",
          "args": [
            "../../Docs/Manual/tutorials/lua-vehicle-init-timing/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": [],
            "references": [
              {
                "property": "luaBehavior",
                "asset": "${script}",
                "type": "ShanghaiWindy.Core.Lua.LuaBehaviorScript"
              }
            ]
          }
        },
        "expect": {
          "equals": {
            "ok": true
          }
        }
      },
      {
        "tool": "inspect",
        "arguments": {
          "path": "asset:${prefab}/LuaBehaviorMono/luaBehavior/name"
        },
        "expect": {
          "contains": "(String) VehicleTiming"
        }
      }
    ]
    

    The replay checks that API 2 skips the legacy subscription, and that the legacy path waits and unregisters. Check actual assembly timing in game.

    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 subscriptions, removals, inits = 0, 0, 0\nlocal callback\nE.VehicleAPI = {\n    RegisterVehicleLoadedEvent = function(v, cb) subscriptions = subscriptions + 1; callback = cb end,\n    UnRegisterVehicleLoaedEvent = function(v, cb) assert(cb == callback); removals = removals + 1 end\n}\n\nlocal B = assert(loadfile('../../Docs/Manual/tutorials/lua-vehicle-init-timing/example.lua', 't', E))()\nlocal function instance(t) return setmetatable(t, { __index = B }) end\nlocal vehicle = { IsLoaded = false }\nlocal script = { TryGetTankInitSystem = function() return true, vehicle end }\nlocal init = function() inits = inits + 1 end\nE.GAME_API_VERSION = 2\nlocal current = instance({ script = script, InitVehicleDependencies = init })\ncurrent:OnStarted()\nassert(subscriptions == 0 and current.vehicle == nil)\ncurrent:OnVehicleReady(vehicle)\ncurrent:OnVehicleReady(vehicle)\nassert(inits == 1 and current.vehicle == vehicle)\nE.GAME_API_VERSION = nil\nlocal legacy = instance({ script = script, InitVehicleDependencies = init })\nlegacy:OnStarted()\nassert(subscriptions == 1 and legacy.vehicle == nil)\ncallback()\ncallback()\nassert(inits == 2 and legacy.vehicle == vehicle)\nlegacy:OnDestroyed()\nassert(removals == 1)\nreturn \"Lua example passed\"\n",
            "manual-lua-vehicle-init-timing",
            "null"
          ]
        },
        "expect": {
          "contains": "Lua example passed"
        }
      }
    ]
    
    In this article
    Back to top Panzer War Document