现在的位置: 首页 > 综合 > 正文

【打发时间之US脚本】读书笔记2

2018年05月06日 ⁄ 综合 ⁄ 共 2831字 ⁄ 字号 评论关闭

一、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); //进行断言检测

抱歉!评论已关闭.