Skip to content

BaseScript

概览

字段
KindUnit · 单位 Unit
Realmcommon

继承关系

继承成员

1 个来源 / 4 属性 / 25 函数 / 6 事件

BaseScriptScriptLocalScriptModuleScript 的抽象基类,公开脚本源代码文本。SourceCode 可在创建具体脚本子类时作为 config 字段提供。

适用场景

在创建 ScriptLocalScriptModuleScript 等具体脚本实例时,通过 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类型默认值说明
SourceCodeString""脚本的源代码字符串,可读写。

关联类型