主题
BaseScript
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- [BaseScript](2 属性)
继承成员
1 个来源 / 4 属性 / 25 函数 / 6 事件
- 来自 Unit(4 属性 / 25 函数 / 6 事件)
- 属性:
AssetId、Name、Desc、Parent - 函数:
ClearAllChildren、GetChildren、GetDescendants、IsAncestorOf、IsDescendantOf、FindFirstAncestor、FindFirstAncestorWhichIsA、FindFirstAncestorOfClass、FindFirstChild、FindFirstChildWhichIsA、FindFirstChildOfClass、HasChildren、GetChildCount、GetChildAtIndex、SetAttribute、GetAttribute、GetAllProps、Clone、IsA、Destroy、GetPropertyChangedSignal、GetAttributeChangedSignal、FindFromPath、GetFullPath、GetDebugId - 事件:
ChildAdded、ChildRemoved、DescendantAdded、DescendantRemoving、AncestryChanged、Destroying
- 属性:
BaseScript 是 Script、LocalScript 与 ModuleScript 的抽象基类,公开脚本源代码文本。SourceCode 可在创建具体脚本子类时作为 config 字段提供。
适用场景
当需要在运行时检查某个脚本对象的代码内容或启用状态时使用,例如在遍历场景对象、输出脚本代码用于调试排查,或判断某段脚本是否处于启用状态。
使用要点
通过 game:CreateUnit("Script"/"LocalScript"/"ModuleScript", config) 创建具体脚本,并在 config 中提供 SourceCode;创建后可读取 SourceCode。
注意事项
BaseScript 本身不可直接创建。通过宿主接口取得实例后,只读取本页声明的 SourceCode;不要把已创建实例上的 SourceCode 当作常规可写属性。
代码示例
创建具体脚本并读取源代码
lua
-- @runtime client
local scriptUnit = game:CreateUnit('LocalScript', {
Name = 'ClientHello',
SourceCode = "print('hello from client script')",
})
print('脚本源码:', scriptUnit.SourceCode)
scriptUnit:Destroy()属性 (2)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
SourceCode | String | "" | 脚本的源代码字符串,可读写。 |
IsEnabled | Bool | true | IsEnabled 是 BaseScript 上公开的布尔(Bool)属性,用于表示对应脚本是否处于启用状态。该属性可读取也可修改,Meta 中给出的默认值为 true。 |
