记录一次xlua修复构造函数的经验
需求:类型A包含多个重载构造函数(包含参数数量相同但参数类型不同的情况)。
修复方法:像这种修构造函数的lua,会注入到所有符合条件的函数中(参数数量不同的也会注入),但可能你只需要修复其中一个,这个时候可以在lua函数内部进行类型判断,来决定时候走热更代码。
1 2 3 4 5 6 7 | local A = function(self,jsonVehicleComponent,checkCollect) if jsonVehicleComponent:GetType() == typeof (CS.LitJson.JsonData) then --热更代码 end self:A(); end end |
本来修到这里就完事了,但修后面需求的时候发现了问题:在别的热更代码(lua)中调用了A的构造函数,报错了:
attempt to index local 'self' (a number value)
查出来是走到了上面修复的构造函数A中,由于传入的是lua的基本类型,导致jsonVehicleComponent:GetType()报了空指针,猜测是是C#的GetType()只能检查C#的类型,lua的基础类型调用GetType()会报这个错,后来改成了:
1 2 3 4 5 6 7 | local A = function(self,jsonVehicleComponent,checkCollect) if type(jsonVehicleComponent) ~= "number" and jsonVehicleComponent:GetType() == typeof (CS.LitJson.JsonData) then --热更代码 end self:A(); end end |
加上了lua基础类型的判断就好了,
还可以加上参数个数判断:
1 2 3 4 5 6 7 8 9 10 11 | local A = function(self,...) local length = select ( '#' ,...); if length == 2 then local jsonVehicleComponent = select (1,...); if jsonVehicleComponent ~= nil and type(jsonVehicleComponent) ~= "number" and jsonVehicleComponent:GetType() == typeof (CS.LitJson.JsonData) then ..... end end end |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2018-09-15 关于Rigidbody,Collider和CharacterController三者之间的关系和用法的总结