UDN
Search public documentation:
UnrealScriptDefaultPropertiesCH
English Translation
日本語訳
한국어
Interested in the Unreal Engine?
Visit the Unreal Technology site.
Looking for jobs and company info?
Check out the Epic games site.
Questions about support via UDN?
Contact the UDN Staff
日本語訳
한국어
Interested in the Unreal Engine?
Visit the Unreal Technology site.
Looking for jobs and company info?
Check out the Epic games site.
Questions about support via UDN?
Contact the UDN Staff
UnrealScript默认属性
概述
defaultproperties
的特殊代码块来为属于该类的所有实例变量设置值,当创建该类的实例时将会使用该默认属性代码块进行初始化。这个代码块总是位于脚本的尾部,所有函数及状态声明的后面,尽管它是UnrealScript的一部分,但是这段代码不是标准的UnrealScript代码,并且遵循着略微不同的规则。
指定默认属性值
defaultproperties
块的规则和标准的UnrealScript语法略有不同。总的不同之处有:
- 在defaultproperties语句块中不允许存在语句,动态数组操作除外。这意味着您不能执行计算、调用函数等。
defaultproperties
仅用于分配特定的值给实例变量。 - 分号可以放在每行的末尾,但不是必须的。
- 避免出现空格 (比如在变量名、赋值运算符、及值之间)。
-
defaultproperties(默认属性)
代码块的花括号应该另起一行。
VarName=Value
ArrayProp(0)=Value1 ArrayProp(1)=Value2
ArrayProp[0]=Value1 ArrayProp[1]=Value2
ArrayProp=(Value1,Value2,Value3)
ArrayProp(0)=Value1 ArrayProp(1)=Value2 ArrayProp(2)=Value3
ArrayProp.Add(Value1) ArrayProp.Add(Value2) ArrayProp.Add(Value3)
NameProp='Value'
NameProp=Value
ObjectProp=ObjectClass'ObjectName'
Begin Object Class=ObjectClass Name=ObjectName VarName=Value ... End Object ObjectProperty=ObjectName
StructProperty=(InnerStructPropertyA=Value1,InnerStructPropertyB=Value2)
StructProperty={( InnerStructPropertyA=Value1, InnerStructPropertyB=Value2 )}
- 内联静态数组必须像这样来声明(注意这里是使用括弧"[]"来作为数组的分隔符,而不是使用圆括号"()")
StructProperty=(StaticArray[0]=Value,StaticArrayProp[1]=Value)
- 内联动态数组必须使用单独的一行语法来声明:
StructProperty=(DynamicArray=(Value,Value))</code>
- 内联名称变量必须有双引号括起:
StructProperty=(NameProperty="Value")
- Empty - 清除整个数组。
Array.Empty
- Add(element) - 添加
element
元素到数组的尾部。Array.Add(element)
- Remove(element) - 从数组中删除
element(元素)
, 这将删除所出现的所有该element(元素)
。Array.Remove(element)
- RemoveIndex(index) -删除给定
index(索引)
处的元素。Array.RemoveIndex(index)
- Replace(element1, element2) - 使用
element2
代替element1
。所有出现element1的地方都会被替换。如果没有找到element1
将会弹出一个警告。Array.Replace(element1, element2)
以下是完整的defaultproperties
代码块的示例( 基于Actor.uc
文件):defaultproperties { // objects MessageClass=class'LocalMessage' // declare an inline subobject of class SpriteComponent named "Sprite" Begin Object Class=SpriteComponent Name=Sprite // values specified here override SpriteComponent's own defaultproperties Sprite=Texture2D'EngineResources.S_Actor' HiddenGame=true End Object //todo Components.Add(Sprite) // declare an inline subobject of class CylinderComponent named "CollisionCylinder" Begin Object Class=CylinderComponent Name=CollisionCylinder // values specified here override CylinderComponent's own defaultproperties CollisionRadius=10 CollisionHeight=10 AlwaysLoadOnClient=True AlwaysLoadOnServer=True End Object //todo Components.Add(CollisionCylinder) CollisionComponent=CollisionCylinder // floats (leading '+' and trailing 'f' characters are ignored) DrawScale=00001.000000 Mass=+00100.000000 NetPriority=00001.f // ints NetUpdateFrequency=100 // enumerations Role=ROLE_Authority RemoteRole=ROLE_None // structs DrawScale3D=(X=1,Y=1,Z=1) // bools bJustTeleported=true bMovable=true bHiddenEdGroup=false bReplicateMovement=true // names InitialState=None // dynamic array (in this case, a dynamic class array) SupportedEvents(0)=class'SeqEvent_Touch' SupportedEvents(1)=class'SeqEvent_UnTouch' SupportedEvents(2)=class'SeqEvent_Destroyed' SupportedEvents(3)=class'SeqEvent_TakeDamage' }
Struct默认值
当您在UnrealScript中声明一个struct时,您可以选择为struct的属性指定默认值。 任何时候在UnrealScript中使用这个struct时,它的成员将会使用这些值进行初始化。 语法和一个类的defaultproperties(默认属性)块的语法相同 – 唯一的不同是这个语句块名称为 structdefaultproperties 。 比如:struct LinearColor { var() config float R, G, B, A; structdefaultproperties { A=1.f } };
var LinearColor NormalColor, DarkColor; defaultproperties { NormalColor=(R=1.f,B=1.f,G=1.f) // value of A will be 1.0f for this property DarkColor=(R=1.f,B=1.f,G=1.f,A=0.2f) // value of A will be 0.2f for this property }
访问变量的默认值
UnrealEd允许关卡设计人员修改一个对象的类的变量的默认值。当生成一个属于这个类的新actor时,它的所有变量都会被初始化为这些默认值。有时,手动地将一个变量重置为默认值是有帮助的。比如,当一个玩家掉落一个武器装备,那么武器装备代码需要将actor的某些值重置为默认值。在UnrealScript中,您可以通过使用Default
关键字来访问一个类的默认变量。比如:var() float Health, Stamina; ... // Reset some variables to their defaults. function ResetToDefaults() { // Reset health, and stamina. Health = Default.Health; Stamina = Default.Stamina; }
通过一个类引用来访问变量的默认值
如果您有一个类的引用(一个class
或class<classlimitor>
类型的变量),您便可以访问它所引用的类的默认属性,而不需要有那个类的对象。这个语法适用于和class类型等同的其它任何表达式。var class C; var class
PC; Health = class'Spotlight'.default.LightBrightness; // Access the default value of // LightBrightness in the Spotlight class. Health = PC.default.Health; // Access the default value of Health in // a variable class identified by PC. Health = class (C).default.Health; // Access the default value // of Health in a casted class // expression. - Add(element) - 添加