diff options
author | reduz <reduzio@gmail.com> | 2015-12-28 19:31:52 -0300 |
---|---|---|
committer | reduz <reduzio@gmail.com> | 2015-12-28 19:32:51 -0300 |
commit | 30c12297dc6df7d35df140475c0cec7308aea77a (patch) | |
tree | e09c1ed46a6d42bcaab5ff4f3a67749948ff6ff7 /modules/gdscript/gd_parser.cpp | |
parent | eecfeb1d7681bf9186bc231e9ee8c5d36f2afcea (diff) |
- added 'onready' keyword to gdscript. Defers initialization of member variables until _ready() is run.
Diffstat (limited to 'modules/gdscript/gd_parser.cpp')
-rw-r--r-- | modules/gdscript/gd_parser.cpp | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/modules/gdscript/gd_parser.cpp b/modules/gdscript/gd_parser.cpp index 4339a13edf..95e172e26e 100644 --- a/modules/gdscript/gd_parser.cpp +++ b/modules/gdscript/gd_parser.cpp @@ -2767,6 +2767,21 @@ void GDParser::_parse_class(ClassNode *p_class) { } }; //fallthrough to var + case GDTokenizer::TK_PR_ONREADY: { + + if (tokenizer->get_token(-1)==GDTokenizer::TK_PR_EXPORT) { + current_export=PropertyInfo(); + _set_error("Expected 'var' (can't combine with 'onready')."); + return; + } else { + + tokenizer->advance(); + if (tokenizer->get_token()!=GDTokenizer::TK_PR_VAR) { + _set_error("Expected 'var'."); + return; + } + } + }; //fallthrough to var case GDTokenizer::TK_PR_VAR: { //variale declaration and (eventual) initialization @@ -2777,6 +2792,8 @@ void GDParser::_parse_class(ClassNode *p_class) { current_export=PropertyInfo(); } + bool onready = tokenizer->get_token(-1)==GDTokenizer::TK_PR_ONREADY; + tokenizer->advance(); if (tokenizer->get_token()!=GDTokenizer::TK_IDENTIFIER) { @@ -2807,6 +2824,21 @@ void GDParser::_parse_class(ClassNode *p_class) { return; } + //discourage common error + if (!onready && subexpr->type==Node::TYPE_OPERATOR) { + + OperatorNode *op=static_cast<OperatorNode*>(subexpr); + if (op->op==OperatorNode::OP_CALL && op->arguments[0]->type==Node::TYPE_SELF && op->arguments[1]->type==Node::TYPE_IDENTIFIER) { + IdentifierNode *id=static_cast<IdentifierNode*>(op->arguments[1]); + if (id->name=="get_node") { + + _set_error("Use 'onready var "+String(member.identifier)+" = get_node(..)' instead"); + return; + + } + } + } + member.expression=subexpr; if (autoexport) { @@ -2853,12 +2885,19 @@ void GDParser::_parse_class(ClassNode *p_class) { op->arguments.push_back(id); op->arguments.push_back(subexpr); + #ifdef DEBUG_ENABLED NewLineNode *nl = alloc_node<NewLineNode>(); nl->line=line; - p_class->initializer->statements.push_back(nl); + if (onready) + p_class->ready->statements.push_back(nl); + else + p_class->initializer->statements.push_back(nl); #endif - p_class->initializer->statements.push_back(op); + if (onready) + p_class->ready->statements.push_back(op); + else + p_class->initializer->statements.push_back(op); @@ -3009,6 +3048,8 @@ Error GDParser::_parse(const String& p_base_path) { ClassNode *main_class = alloc_node<ClassNode>(); main_class->initializer = alloc_node<BlockNode>(); main_class->initializer->parent_class=main_class; + main_class->ready = alloc_node<BlockNode>(); + main_class->ready->parent_class=main_class; current_class=main_class; _parse_class(main_class); |