You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-19 14:31:59 +00:00
Enable autoload in editor
- Tool scripts will be executed and can be accessed by plugins. - Other script languages can implement add/remove_named_global_constant to make use of this functionality.
This commit is contained in:
@@ -1333,6 +1333,15 @@ void GDScriptLanguage::add_global_constant(const StringName &p_variable, const V
|
||||
_add_global(p_variable, p_value);
|
||||
}
|
||||
|
||||
void GDScriptLanguage::add_named_global_constant(const StringName &p_name, const Variant &p_value) {
|
||||
named_globals[p_name] = p_value;
|
||||
}
|
||||
|
||||
void GDScriptLanguage::remove_named_global_constant(const StringName &p_name) {
|
||||
ERR_FAIL_COND(!named_globals.has(p_name));
|
||||
named_globals.erase(p_name);
|
||||
}
|
||||
|
||||
void GDScriptLanguage::init() {
|
||||
|
||||
//populate global constants
|
||||
|
||||
@@ -262,6 +262,7 @@ class GDScriptLanguage : public ScriptLanguage {
|
||||
Variant *_global_array;
|
||||
Vector<Variant> global_array;
|
||||
Map<StringName, int> globals;
|
||||
Map<StringName, Variant> named_globals;
|
||||
|
||||
struct CallLevel {
|
||||
|
||||
@@ -369,7 +370,8 @@ public:
|
||||
|
||||
_FORCE_INLINE_ int get_global_array_size() const { return global_array.size(); }
|
||||
_FORCE_INLINE_ Variant *get_global_array() { return _global_array; }
|
||||
_FORCE_INLINE_ const Map<StringName, int> &get_global_map() { return globals; }
|
||||
_FORCE_INLINE_ const Map<StringName, int> &get_global_map() const { return globals; }
|
||||
_FORCE_INLINE_ const Map<StringName, Variant> &get_named_globals_map() const { return named_globals; }
|
||||
|
||||
_FORCE_INLINE_ static GDScriptLanguage *get_singleton() { return singleton; }
|
||||
|
||||
@@ -403,6 +405,8 @@ public:
|
||||
virtual String _get_indentation() const;
|
||||
virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const;
|
||||
virtual void add_global_constant(const StringName &p_variable, const Variant &p_value);
|
||||
virtual void add_named_global_constant(const StringName &p_name, const Variant &p_value);
|
||||
virtual void remove_named_global_constant(const StringName &p_name);
|
||||
|
||||
/* DEBUGGER FUNCTIONS */
|
||||
|
||||
|
||||
@@ -278,6 +278,18 @@ int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::
|
||||
return idx | (GDScriptFunction::ADDR_TYPE_GLOBAL << GDScriptFunction::ADDR_BITS); //argument (stack root)
|
||||
}
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(identifier)) {
|
||||
|
||||
int idx = codegen.named_globals.find(identifier);
|
||||
if (idx == -1) {
|
||||
idx = codegen.named_globals.size();
|
||||
codegen.named_globals.push_back(identifier);
|
||||
}
|
||||
return idx | (GDScriptFunction::ADDR_TYPE_NAMED_GLOBAL << GDScriptFunction::ADDR_BITS);
|
||||
}
|
||||
#endif
|
||||
|
||||
//not found, error
|
||||
|
||||
_set_error("Identifier not found: " + String(identifier), p_expression);
|
||||
@@ -1511,6 +1523,18 @@ Error GDScriptCompiler::_parse_function(GDScript *p_script, const GDScriptParser
|
||||
gdfunc->_global_names_count = 0;
|
||||
}
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
// Named globals
|
||||
if (codegen.named_globals.size()) {
|
||||
gdfunc->named_globals.resize(codegen.named_globals.size());
|
||||
gdfunc->_named_globals_ptr = gdfunc->named_globals.ptr();
|
||||
for (int i = 0; i < codegen.named_globals.size(); i++) {
|
||||
gdfunc->named_globals[i] = codegen.named_globals[i];
|
||||
}
|
||||
gdfunc->_named_globals_count = gdfunc->named_globals.size();
|
||||
}
|
||||
#endif
|
||||
|
||||
if (codegen.opcodes.size()) {
|
||||
|
||||
gdfunc->code = codegen.opcodes;
|
||||
|
||||
@@ -94,6 +94,9 @@ class GDScriptCompiler {
|
||||
|
||||
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
|
||||
Map<StringName, int> name_map;
|
||||
#ifdef TOOLS_ENABLED
|
||||
Vector<StringName> named_globals;
|
||||
#endif
|
||||
|
||||
int get_name_map_pos(const StringName &p_identifier) {
|
||||
int ret;
|
||||
|
||||
@@ -108,6 +108,21 @@ Variant *GDScriptFunction::_get_variant(int p_address, GDScriptInstance *p_insta
|
||||
#endif
|
||||
return &GDScriptLanguage::get_singleton()->get_global_array()[address];
|
||||
} break;
|
||||
#ifdef TOOLS_ENABLED
|
||||
case ADDR_TYPE_NAMED_GLOBAL: {
|
||||
#ifdef DEBUG_ENABLED
|
||||
ERR_FAIL_INDEX_V(address, _named_globals_count, NULL);
|
||||
#endif
|
||||
StringName id = _named_globals_ptr[address];
|
||||
|
||||
if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(id)) {
|
||||
return (Variant *)&GDScriptLanguage::get_singleton()->get_named_globals_map()[id];
|
||||
} else {
|
||||
r_error = "Autoload singleton '" + String(id) + "' has been removed.";
|
||||
return NULL;
|
||||
}
|
||||
} break;
|
||||
#endif
|
||||
case ADDR_TYPE_NIL: {
|
||||
return &nil;
|
||||
} break;
|
||||
|
||||
@@ -92,7 +92,8 @@ public:
|
||||
ADDR_TYPE_STACK = 5,
|
||||
ADDR_TYPE_STACK_VARIABLE = 6,
|
||||
ADDR_TYPE_GLOBAL = 7,
|
||||
ADDR_TYPE_NIL = 8
|
||||
ADDR_TYPE_NAMED_GLOBAL = 8,
|
||||
ADDR_TYPE_NIL = 9
|
||||
};
|
||||
|
||||
enum RPCMode {
|
||||
@@ -121,6 +122,10 @@ private:
|
||||
int _constant_count;
|
||||
const StringName *_global_names_ptr;
|
||||
int _global_names_count;
|
||||
#ifdef TOOLS_ENABLED
|
||||
const StringName *_named_globals_ptr;
|
||||
int _named_globals_count;
|
||||
#endif
|
||||
const int *_default_arg_ptr;
|
||||
int _default_arg_count;
|
||||
const int *_code_ptr;
|
||||
@@ -137,6 +142,9 @@ private:
|
||||
StringName name;
|
||||
Vector<Variant> constants;
|
||||
Vector<StringName> global_names;
|
||||
#ifdef TOOLS_ENABLED
|
||||
Vector<StringName> named_globals;
|
||||
#endif
|
||||
Vector<int> default_arguments;
|
||||
Vector<int> code;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user