一、UNREALSCRIPT預處理器
1.MACRO(宏)的基礎知識
1.1語法:
`define MyFirstMacro "This is really my first macro." // 定義
`{MyFirstMacro} // 調用,需要用到{}
注意: 定義和調用前都有一個反引號(`) ——鍵盤數字1左邊的按鍵
使用宏的時候,應注意是否包含其他宏定義,不然可能會陷入到無限宏定義循環中調用用到的花括號{},是確定周圍的空白是有效的,不然會出現下面的情況:
`define ClassSpecs palceable // 聲明
class AmbientCreature extends Actor `{ClassSpecs}; // 調用
class AmbientCreature extends Actorplaceable; // 展開,沒用到花括號的情況
placeable 使類可以在Actor Classes中進行移動,添加到場景中
abstract 使類變成抽象類,在Actor Classes中呈現爲灰掉狀態,並且不是一個可以放置的類
1.2實例:
1.2.1創建一個數的三次方的宏
`define Cube(x) `x*`x*`x
注意:當引用參數的名稱時,必須在它前面加上`(反引號)
var int myVal = `Cube(3); // 調用
注意:當調用宏時,小括號()必須直接跟在宏的後面,且不能有任何空格出現
1.2.2創建兩個數之和的平方的宏
`define SumSquare(x, y) (`x + `y) * (`x + `y) var int myVal = `SumSquare(p1, p2);
上面的調用如果x y均爲整數,那麼沒有任何問題,但是由於宏沒有提供類型保護,只是單純的展開,所以有可能會獲得不能編譯的代碼
另外需要注意的是,虛幻預處理器會吞掉宏表達式間的空格字符,如果想讓輸出如下所示
var int badValue = (p1 + p2) * (p1 + p2);
那麼需要使用花括號{}把宏保衛起來
`define SumSquare(x, y) (`{x} + `{y}) * (`{x} + `{y})
在這個實例中出現的空格沒有什麼關係,但是它確實是一個潛在的問題,所以請記住這個事實。
2.內置宏
2.1DEFINE
`define <macro_name>[(<param1>[,<param2>...])] [macro_definition]
2.1.1正確定義:
`define <macro_name>[(<param1>[,<param2>...])] [macro_definition]
2.1.2使用:
`define MyMacro(p1,p2,p3,p4) "This macro contains '#// #號表示斷句連接
parameters."
var string myString = `MyMacro(1, 2, 3, 4);
2.1.3上面的MyMacro展開形式是:
var string myString = "This is macro contains 4 parameters.";
2.2IF/ELSE/ENDIF
2.2.1概念:用於支持條件編譯
2.2.2正確定義:
`if(<value>)
`else
`endif
注意: 真——執行IF和ELSE之間的語句
假——執行ELSE和ENDIF之間的語句
2.2.3應用
2.2.3.1實例:IF/ELSE/ENDIF的應用:
`define MyIFCheck "YES"
var int myValue;
`if(`MyIfCheck')
myValue = 10;
`else
myValue = 20;
`endif
2.2.3.2實例:NESTING IF/ELSE/ENDIF 宏
方法一:
`if(`PC)
//The code to execute for the PC would go here
`else `if(`XBOXONE)
//The code to execute for the Xbox One would go here
`endif
方法二:
`if(`PC)
//The code to execute for the PC would go here
`endif
`if(`XBOXONE)
//The code to execute for the Xbox One would go here
`endif
2.3 INCLUDE
2.3.1 正確定義:
`include(<filename>)
注意:
1.filename 編輯器首先會在Src對應工程Classes目錄下進行查找,然後會在當前編輯的包的根目錄中進行查找
例如`include(WOTGameInfo) 實際是在\Development\Src\WOTGame\Classes\WOTGameInfo
2.在包含文件中定義沒有宏指令體的宏可能會導致編譯錯誤,特別是如果這個定義是文件的最後一行時。只要
在這個聲明後面放一小段其他的代碼,甚至是一個註釋便可以消除該問題
2.4 ISDEFINED/NOTDEFINED
2.4.1 正確定義:
`isdefined(<macro_name>)
`notdefined(<macro_name>)
2.4.1 結合使用 IF/ELSE/ENDIF 和 ISDEFINED/NOTDEFINED
`define MyCode
`if(`isdefined(MyCode))
// 這個代碼塊將會被包含,因爲已經定義了MyCode宏
`endif
`if(`notdefine(MyCodeNotDefined))
// 這個代碼塊也將會被包含,因爲還沒有定義MyCodeNotDefined宏
`endif
2.5 UNDEFINE
2.5.1 正確定義
`undefine(<macro_name>)
2.6 LOG/WARN
`log(string OutputString, optional bool bRequiredCondition, optional name LogTag);
`warn(string OutputString, optional bool bRequiredCondition);
注意:
如果指定了BrequiredCondition,那麼將會在執行腳本的過程中計算條件的值,以便是否需要記錄消息
如果使用final_release開關來編譯腳本,那麼將會禁用這兩個宏
2.7 LOGD
LOGD宏實際上和LOG宏是一樣的
`logd(string OutputString, optional bool bRequiredCondition, optional name LogTag);
注意:如果使用final_release開關來編譯腳本,那麼將會禁用這兩個宏
2.8 ASSERT
這個宏是對Assert表達式的封裝。它對於判斷或炎症是否滿足特定條件是游泳的,尤其是在調試時
`assert(bool bCondition)
注意:如果使用final_release開關來編譯腳本,那麼將會禁用這兩個宏
實例:驗證條件
var int x = 1;
var int y = 4;
var int sum = x + y;
`assert(sum == 4); //進行斷言檢測