lua-debugging tips


title: lua-debugging skills
categories: Lua
tags: [lua, debugging, skills]
date: 2022-07-14 16:31:27
comments: false
mathjax: true
toc: true

lua-debugging tips


Prequel

Game lua script debugging skills, to achieve real-time execution after modifying logic scripts, and improve development efficiency


PC side editor mode

  1. Create a new test code script

    gDebugCustom = gDebugCustom or {
          
          }
    
    function gDebugCustom.Test01()
    	gLog("--- Test01")    
    end
    
  2. The editor adds to parse this script and display the button

    image-20220715161734442

    1. Click the button to execute the corresponding method

      • csharp

        GameMgr.CallLuaFunction("Debug_ExecCustomLua", luaFile, funcName);
        
      • lua

        -- 动态执行 自定义 lua 文件调试
        function Debug_ExecCustomLua(luaFile, funcName)
            local ok = xpcall(function()
                gAssert(Utils.IsFileExist(luaFile), "--- 找不到lua 文件, path: {0}", luaFile)
                dofile(luaFile)
                gAssert(gDebugCustom, "--- 找不到全局 table: gDebugCustom")
                gAssert(type(gDebugCustom[funcName]) == "function", "--- 找不到 gDebugCustom.{0} 方法", funcName)
        
                gDebugCustom[funcName]()
            end, __G__TRACKBACK__)
            if ok then
                gLog("<color=#00ff00ff>--- gDebugCustom.{0} success!</color>", funcName)
            else
                gLog("<color=yellow>--- gDebugCustom.{0} execute fail!</color>", funcName)
            end
        end
        
        
        
        
        

mobile terminal

download lua

  1. Add a global method to the [pc editor mode](#pc editor mode) test script gDebugWebFnfor web execution

    
    gDebugCustom = gDebugCustom or {
          
          }
    
    function gDebugCustom.Test01()
        gLog("--- Test01")
    end
    
     -- 增加的全局方法
    gDebugWebFn = function()
        gDebugCustom.Test01()
    end
    
    
  2. Go to the game debugging ui button to execute a download test script, and executegDebugWebFn

    local function testDynamicLua()
        local url = "http://192.168.1.200:59090/lua/custom_unittest.lua"
        gTool.SafeDoString(url, function(isStop, msg)
            gLog("<color=#ffff00>--- gTool.SafeDoString, isStop: {0}</color>, msg: {1}", isStop, msg)
            gAssert(type(gDebugWebFn) == "function", "--- 找不到全局调试方法 gDebugWebFn")
            gDebugWebFn()
        end)
    end
    

Advanced
  1. Dynamically execute scripts, and dynamically download other logic scripts again, which will rewrite the lua method to achieve real-time refresh script logic

    -- 避免出现在 unity editor中
    gDebugCustom.requireExt = function(luaPath, callback)
        local luaDirUrl = "http://192.168.1.200:59090/require"
        local newPath = string.gsub(luaPath, "%.", "/")
        local relaPath = string.formatExt("{0}.lua", newPath)
        local fullUrl = string.formatExt("{0}/{1}", luaDirUrl, relaPath)
        -- gLog("--- fullUrl: {0}", fullUrl)
    
        local cbWrap = function(isOk, res)
            gAssert(isOk, "--- url require fail, luaPath: {0}, fullUrl: {1}", luaPath, fullUrl)
            gLog("<color=#00ff00>--- url require success</color>, luaPath: {0}, fullUrl: {1}", luaPath, fullUrl)
            if callback then callback(res) end
        end
    
        gBestHttpMgr:GetData(fullUrl, function(isSucc, data)
            if isSucc and not IsNull(data) then
                local text = Utils.BytesToUTF8(data)
                local ok, res = xpcall(loadstring(text), __G__TRACKBACK__)
                if not ok then
                    cbWrap(false, "do remote string error")
                else
                    -- 移动端非 ab 模式, 写入本地
                    if gLogicTool.IsMobile() and not Const.LuaBundleMode then
                        local fullPath = gTool.PathJoin(Application.persistentDataPath, Const.kResluaDir, relaPath)
                        Utils.WriteFileUTF8(fullPath, text)
                    end
    
                    cbWrap(true, res)
                end
            else
                cbWrap(false, "Network error")
            end
        end)
    end
    
    function gDebugCustom.Dynamic()
        local function testGm()
            gDebugCustom.requireExt("logic.common.gm", function()
                local input = "show me the log"
                local isOk = gGmMgr:Do(input)
                gLog("--- gm ok: {0}", isOk)
            end)
        end
    	
        -- 测试
        testGm()
    end
    
    
    
    

Android side copy lua

In the way of plain text lua, you can directly copy the lua script in the project to the package body directory with the adb command, and then restart the game or restart the lua virtual machine

  • adb command

    $ adb push E:/its/rummy_itc-v4/z_tempSave/lua /sdcard/Android/data/com.aaa.bbb/files
    

Guess you like

Origin blog.csdn.net/yangxuan0261/article/details/125946307