主题
BaseScript
概览
| 字段 | 值 |
|---|---|
| Kind | Unit · 单位 Unit |
| Realm | common |
继承关系
- Unit(4 属性 / 25 函数 / 6 事件)
- [BaseScript](1 属性)
继承成员
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 字段提供。
适用场景
在创建 Script、LocalScript 或 ModuleScript 等具体脚本实例时,通过 config 参数传入 SourceCode 来初始化脚本内容,或通过读取 SourceCode 属性获取已加载脚本的源代码。
使用要点
通过 game:CreateUnit("Script"/"LocalScript"/"ModuleScript", config) 创建具体脚本,并在 config 中提供 SourceCode;创建后可读取 SourceCode。
注意事项
BaseScript 本身不可直接创建。当前公开契约只声明 SourceCode;不要访问未进入 Meta 的编译校验字段,也不要把已创建实例上的 SourceCode 当作常规可写属性。
代码示例
创建具体脚本并读取源代码
lua
-- @runtime client
local scriptUnit = game:CreateUnit('LocalScript', {
Name = 'ClientHello',
SourceCode = "print('hello from client script')",
})
print('脚本源码:', scriptUnit.SourceCode)
scriptUnit:Destroy()属性 (1)
| Name | 类型 | 默认值 | 说明 |
|---|---|---|---|
SourceCode | String | "" | 脚本的源代码字符串,可读写。 |
