diff options
| author | Hein-Pieter van Braam | 2017-02-16 14:29:18 +0100 |
|---|---|---|
| committer | Rémi Verschelde | 2017-03-18 20:10:36 +0100 |
| commit | 4cf49bb6f49630f0d563b3cc005c0014f1cec378 (patch) | |
| tree | 6324d40b84ed25a0e638c03919770258f64f336d /modules/gdscript | |
| parent | 674a090e5917eab5b590563fb156ff90ea53fe31 (diff) | |
| download | godot-4cf49bb6f49630f0d563b3cc005c0014f1cec378.tar.gz godot-4cf49bb6f49630f0d563b3cc005c0014f1cec378.tar.zst godot-4cf49bb6f49630f0d563b3cc005c0014f1cec378.zip | |
Allow preload to accept a const string.
In preload() parsing this code will lookup the identifier in the local
constant database. If the identifier corresponds to a string constant
it is used as the path for preload().
Currently this does not work for global constants, only constants
declared in the same class as the preload is happening. We can implement
a full fix too. Maybe we can use this PR to discuss the possibilities.
This (partially) fixes #6798
(cherry picked from commit 3e5743ca3619d9767caeddac8520463db50291f6)
Diffstat (limited to 'modules/gdscript')
| -rw-r--r-- | modules/gdscript/gd_parser.cpp | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp index 37cbe9bf1..bc2376103 100644 --- a/modules/gdscript/gd_parser.cpp +++ b/modules/gdscript/gd_parser.cpp @@ -290,21 +290,42 @@ GDParser::Node* GDParser::_parse_expression(Node *p_parent,bool p_static,bool p_ tokenizer->advance(); String path; + bool found_constant = false; bool valid = false; + ConstantNode *cn; + Node *subexpr = _parse_and_reduce_expression(p_parent, p_static); if (subexpr) { if (subexpr->type == Node::TYPE_CONSTANT) { - ConstantNode *cn = static_cast<ConstantNode*>(subexpr); - if (cn->value.get_type() == Variant::STRING) { - valid = true; - path = (String) cn->value; + cn = static_cast<ConstantNode*>(subexpr); + found_constant = true; + } + if (subexpr->type == Node::TYPE_IDENTIFIER) { + IdentifierNode *in = static_cast<IdentifierNode*>(subexpr); + Vector<ClassNode::Constant> ce = current_class->constant_expressions; + + // Try to find the constant expression by the identifier + for(int i=0; i < ce.size(); ++i){ + if(ce[i].identifier == in->name) { + if(ce[i].expression->type == Node::TYPE_CONSTANT) { + cn = static_cast<ConstantNode*>(ce[i].expression); + found_constant = true; + } + } } } + + if (found_constant && cn->value.get_type() == Variant::STRING) { + valid = true; + path = (String) cn->value; + } } + if (!valid) { _set_error("expected string constant as 'preload' argument."); return NULL; } + if (!path.is_abs_path() && base_path!="") path=base_path+"/"+path; path = path.replace("///","//").simplify_path(); |
