common.lua文件分析

本文档详细介绍了Lua脚本中常见的全局变量定义及函数应用,包括数据结构、时间处理、对象创建等核心内容,并提供了多个实用的示例函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

----------------------------------------------------
--  Common Globals  and  Definitions.
--  公共的全局定义
----------------------------------------------------
----------------------------------------------------

--  data structure passed to the Signals, use this  global  to avoid temporary lua mem allocation
--  数据结构发送到信号,使用全局定义避免lua的内存临时分配

--  初始化坐标
g_SignalData_point 
=  {x = 0,y = 0,z = 0};
g_SignalData_point2 
=  {x = 0,y = 0,z = 0};

--  REMEMBER! ALWAYS write 
--  内存总是写。
--  g_SignalData.point  =  g_SignalData_point 
--  before doing direct value assignment (i.e.  not  referenced) like
--  这个过程在Direct分配之前实现定义。
--  g_SignalData.point.x  =  ...
--   and  any math.lua vector function on it (FastSumVectors(g_SignalData.point,..) etc
--  并将在这些定义应用于向量函数中。

--  g_SignalData数据结构
g_SignalData 
=  {
    point 
=  g_SignalData_point,  --  since g_SignalData.point  is  always used as a handler
                    
--  g_SignalData.point总是使用point这个句柄
    point2 
=  g_SignalData_point2,
    ObjectName 
=   "" ,         --  对象名称,初始化为空
    id 
=  NULL_ENTITY,            --  id初始化为空对象ID
    fValue 
=  0,
    iValue 
=  0,
    iValue2 
=  0,
}

g_StringTemp1 
=   "                                              " ;

--  碰撞列表
--  这里一共定义了10张这样的表
g_HitTable 
=  {{},{},{},{},{},{},{},{},{},{},}


--  时间函数
function ShowTime()
    
--  获取系统时间函数
    local ttime
= System.GetLocalOSTime();    
    
--  这里调用了系统日志函数,将当前时间写入日志文件中
    System.Log(string.format(
" %d/%d/%d, %02d:%02d " , ttime.mday, ttime.mon + 1 , ttime.year + 1900 , ttime.hour, ttime.min));
end

--  这里是一个计数器,不过不知道_tbl是一个什么对象?
function count(_tbl)
    local count 
=  0;
    
if  (_tbl) then
        
for  i,v  in  pairs(_tbl) do
            count 
=  count + 1 ;
        end
    end    
    
return  count;
end

--  创建一个新对象,第一个参数数是对象类型,第二个参数为是否允许对象重复
function new(_obj, norecurse)
    
if  (type(_obj)  ==   " table " ) then
        local _newobj 
=  {};
        
if  (norecurse) then
            
for  i,f  in  pairs(_obj) do
                _newobj[i] 
=  f;
            end
        
else
            
for  i,f  in  pairs(_obj) do
                
if  ((type(f)  ==   " table " and  (_obj ~= f)) then  --  avoid recursing into itself
                    _newobj[i] 
=  new(f);
                
else
                    _newobj[i] 
=  f;
                end
            end
        end
        
return  _newobj;
    
else
        
return  _obj;
    end
end

--  合并【暂时不清楚这个函数的作用】
function merge(dst, src, recurse)
    
for  i,v  in  pairs(src) do
        
if  (type(v)  ~=   " function " ) then
            
if (recurse) then
                
if ((type(v)  ==   " table " and  (v  ~=  src))then   --  avoid recursing into itself
                    
if  ( not  dst[i]) then
                        dst[i] 
=  {};
                    end
                    merge(dst[i], v, recurse);
                elseif (
not  dst[i]) then
                    dst[i] 
=  v;
                end
            elseif (
not  dst[i]) then
                dst[i] 
=  v;
            end
        end
    end
    
    
return  dst;
end

--  合并【暂时不清楚这个函数的作用】
function mergef(dst, src, recursive)
    
for  i,v  in  pairs(src) do
        
if  (recursive) then
            
if ((type(v)  ==   " table " and  (v  ~=  src))then   --  avoid recursing into itself
                
if  ( not  dst[i]) then
                    dst[i] 
=  {};
                end
                mergef(dst[i], v, recursive);
            elseif (
not  dst[i]) then
                dst[i] 
=  v;
            end
        elseif (
not  dst[i]) then
            dst[i] 
=  v;
        end
    end
    
    
return  dst;
end

--  向量字符串【暂时不清楚这个函数的作用】
function Vec2Str(vec)
    
return  string.format( " (x: %.3f y: %.3f z: %.3f) " , vec.x, vec.y, vec.z);
end


--  错误日志通过FontColors.txt中的查询可以看到用的字符是红色
function LogError(fmt, ...)
    System.Log(
" $4 " ..string.format(fmt, ...));
end

--  警告使用的黄色
function LogWarning(fmt, ...)
    System.Log(
" $6 " ..string.format(fmt, ...));
end

--  正常的日志
function Log(fmt, ...)
    System.Log(string.format(fmt, ...));
end


--  堆对象表长度为0
g_dump_tabs
= 0;

--  堆处理函数
function dump(_class, no_func, depth)
    
if   not  _class then
        System.Log(
" $2nil " );
    
else
        
if  ( not  depth) then
            depth 
=   8 ;
        end
        local str
= "" ;
        
for  n = 0,g_dump_tabs, 1  do
            str
= str.. "    " ;
        end
        
for  i,field  in  pairs(_class) do
            
if (type(field) == " table " ) then
                
if  (g_dump_tabs  <  depth) then
                    g_dump_tabs
= g_dump_tabs + 1 ;
                    System.Log(str..
" $4 " ..tostring(i).. " $1= { " );
                    dump(field, no_func, depth);
                    System.Log(str..
" $1} " );
                    g_dump_tabs
= g_dump_tabs - 1 ;
                
else
                    System.Log(str..
" $4 " ..tostring(i).. " $1= { $4...$1 } " );
                end
            
else
                
if (type(field) == " number "  ) then
                    System.Log(
" $2 " ..str.. " $6 " ..tostring(i).. " $1=$8 " ..field);
                elseif(type(field) 
==   " string " ) then
                    System.Log(
" $2 " ..str.. " $6 " ..tostring(i).. " $1=$8 " .. " " " ..field.. " " " );
                elseif(type(field) 
==   " boolean " ) then
                    System.Log(
" $2 " ..str.. " $6 " ..tostring(i).. " $1=$8 " .. " " " ..tostring(field).. " " " );
                
else
                    
if ( not  no_func)then
                        
if (type(field) == " function " )then
                            System.Log(
" $2 " ..str.. " $5 " ..tostring(i).. " () " );
                        
else
                            System.Log(
" $2 " ..str.. " $7 " ..tostring(i).. " $8<userdata> " );
                        end
                    end
                end
            end
        end
    end
end


--  check  if  a string  is  set  and  it ' s length > 0
--  检查字符串长度是否大于0
function EmptyString(str)
    
if  (str  and  string.len(str)  >  0) then
        
return  false;
    end
    
return  true;
end


--  check  if  a number value  is  true  or  false
--  usefull  for  entity parameters
--  检查一个数据是否为真,这里使用的是非零即为真
function NumberToBool(n)
    
if  (n  and  (tonumber(n)  ~=  0)) then
        
return  true;
    
else
        
return  false;
    end
end



--  easy way to log entity id
--  accepts both entity table  or  entityid
--  一种简单的方法用于记录实体id
--  可以接受实体表或实体id
function EntityName(entity)
    
if  (type(entity)  ==   " userdata " ) then
        local e 
=  System.GetEntity(entity);
        
if  (e) then
            
return  e:GetName();
        end
    elseif (type(entity) 
==   " table " ) then
        
return  entity:GetName();
    end
    
return   "" ;
end


--  easy way to get entity by name
--  usefull  for   " console debugging " !
--  用于通过名称获取实体
--  这个函数主要用于控制台调试环境
function EntityNamed(name)
    
return  System.GetEntityByName(name);
end

function SafeTableGet( table, name )
    
if  table then  return  table[name]  else   return  nil end
end

----------------------------------------------------
--  Load commonly used globals.
--  加载普通全局脚本
----------------------------------------------------
--  数学
Script.ReloadScript(
" scripts/Utils/Math.lua " );
--  实体工具
Script.ReloadScript(
" scripts/Utils/EntityUtils.lua " );
--  未知
Script.ReloadScript(
" scripts/Utils/ZeroG.lua " );
----------------------------------------------------

--///
--  needed to reload ai related scripts  from  editor
--  需要重新加载ai描述脚本
----------------------------------------------------
function AIReload()
    Script.LoadScript(
" Scripts/AI/aiconfig.lua " ,true,true);
end

--  是否开启AI调式
g_AIDebugToggleOn
= 0;
--///
--  
--  AI调式函数
----------------------------------------------------
function AIDebugToggle()
    
if (g_AIDebugToggleOn  ==  0) then
--         System.Log( " ___AIDebugToggle switching on " );
        g_AIDebugToggleOn
= 1 ;
        System.SetCVar( 
" ai_DebugDraw " , 1  );
    
else
--         System.Log( " ___AIDebugToggle switching off " );
        System.SetCVar( 
" ai_DebugDraw " ,0 );
        g_AIDebugToggleOn
= 0;    
    end
end




----------------------------------------------------
--  加载数学系统工具
Script.ReloadScript(
" scripts/entities/items/itemsystemmath.lua " );

--///
--  VS2 hack until we find a better place to do that
--  precache bullet_hits sound so the wavebank  is  loaded statically
--  似乎是声音相关的东西,这里包含了子弹、脚本、碰撞的声音缓存加载…………不过在显卡比较差的话,声音还是会先出来…………
Sound.Precache(
" Sounds/physics:bullet_impact:mat_grass " , SOUND_PRECACHE_LOAD_SOUND);    --  bullet_hit fsb
Sound.Precache(
" Sounds/physics:footstep_walk:mat_grass " , SOUND_PRECACHE_LOAD_SOUND);    --  footsteps fsb
Sound.Precache(
" Sounds/physics:mat_metal_sheet:mat_dirt " , SOUND_PRECACHE_LOAD_SOUND);  --  collision fsb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值