mirror of
https://github.com/godotengine/godot.git
synced 2025-11-01 22:21:18 +00:00
- added 'onready' keyword to gdscript. Defers initialization of member variables until _ready() is run.
This commit is contained in:
parent
eecfeb1d76
commit
30c12297dc
7 changed files with 78 additions and 8 deletions
|
|
@ -1181,7 +1181,7 @@ Error GDCompiler::_parse_block(CodeGen& codegen,const GDParser::BlockNode *p_blo
|
|||
}
|
||||
|
||||
|
||||
Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func) {
|
||||
Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *p_class,const GDParser::FunctionNode *p_func,bool p_for_ready) {
|
||||
|
||||
Vector<int> bytecode;
|
||||
CodeGen codegen;
|
||||
|
|
@ -1212,9 +1212,9 @@ Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *
|
|||
|
||||
/* Parse initializer -if applies- */
|
||||
|
||||
bool is_initializer=false || !p_func;
|
||||
bool is_initializer=!p_for_ready && !p_func;
|
||||
|
||||
if (!p_func || String(p_func->name)=="_init") {
|
||||
if (is_initializer || String(p_func->name)=="_init") {
|
||||
//parse initializer for class members
|
||||
if (!p_func && p_class->extends_used && p_script->native.is_null()){
|
||||
|
||||
|
|
@ -1232,6 +1232,17 @@ Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *
|
|||
|
||||
}
|
||||
|
||||
if (p_for_ready || (p_func && String(p_func->name)=="_ready")) {
|
||||
//parse initializer for class members
|
||||
if (p_class->ready->statements.size()) {
|
||||
Error err = _parse_block(codegen,p_class->ready,stack_level);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* Parse default argument code -if applies- */
|
||||
|
||||
Vector<int> defarg_addr;
|
||||
|
|
@ -1260,7 +1271,10 @@ Error GDCompiler::_parse_function(GDScript *p_script,const GDParser::ClassNode *
|
|||
|
||||
func_name=p_func->name;
|
||||
} else {
|
||||
func_name="_init";
|
||||
if (p_for_ready)
|
||||
func_name="_ready";
|
||||
else
|
||||
func_name="_init";
|
||||
}
|
||||
|
||||
codegen.opcodes.push_back(GDFunction::OPCODE_END);
|
||||
|
|
@ -1614,10 +1628,14 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
|
|||
//parse methods
|
||||
|
||||
bool has_initializer=false;
|
||||
bool has_ready=false;
|
||||
|
||||
for(int i=0;i<p_class->functions.size();i++) {
|
||||
|
||||
if (!has_initializer && p_class->functions[i]->name=="_init")
|
||||
has_initializer=true;
|
||||
if (!has_ready && p_class->functions[i]->name=="_ready")
|
||||
has_ready=true;
|
||||
Error err = _parse_function(p_script,p_class,p_class->functions[i]);
|
||||
if (err)
|
||||
return err;
|
||||
|
|
@ -1640,6 +1658,13 @@ Error GDCompiler::_parse_class(GDScript *p_script,GDScript *p_owner,const GDPars
|
|||
return err;
|
||||
}
|
||||
|
||||
if (!has_ready && p_class->ready->statements.size()) {
|
||||
//create a constructor
|
||||
Error err = _parse_function(p_script,p_class,NULL,true);
|
||||
if (err)
|
||||
return err;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ENABLED
|
||||
//validate setters/getters if debug is enabled
|
||||
for(int i=0;i<p_class->variables.size();i++) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue