Ref<TimerObject> NewTimer::wait_trigger(float p_time, Object *p_target, String p_method) { const String timer_key = "new_timer"; MainLoop *main_loop = OS::get_singleton()->get_main_loop(); SceneTree *tree = main_loop->cast_to<SceneTree>(); ERR_FAIL_COND_V(tree == NULL, NULL); Viewport *viewport = tree->get_root(); ERR_FAIL_COND_V(viewport == NULL, NULL); TimerNode *timerNode; if (timerNode == NULL) { timerNode = memnew(TimerNode); timerNode->set_name(timer_key); Vector<Variant> vector; vector.push_back(Variant(timerNode)); tree->connect("idle_frame", this, "_add_node", vector, 0); }else { timerNode = viewport->get_node(timer_key)->cast_to<TimerNode>(); } Ref<TimerObject> obj = memnew(TimerObject); obj->time = p_time; obj->target = p_target; obj->method = p_method; timerNode->timer_objs.push_back(obj); timerNode->check_queue(); return obj; }
void NewTimer::_add_node(Object *node) { MainLoop *main_loop = OS::get_singleton()->get_main_loop(); SceneTree *tree = main_loop->cast_to<SceneTree>(); ERR_FAIL_COND(tree == NULL); tree->disconnect("idle_frame", this, "_add_node"); tree->get_root()->add_child(node->cast_to<TimerNode>()); }
bool Main::start() { ERR_FAIL_COND_V(!_start_success,false); bool editor=false; String doc_tool; bool doc_base=true; String game_path; String script; String test; String screen; String optimize; String optimize_preset; String _export_platform; String _import; String _import_script; String dumpstrings; bool noquit=false; bool convert_old=false; bool export_debug=false; bool project_manager_request = false; List<String> args = OS::get_singleton()->get_cmdline_args(); for (int i=0;i<args.size();i++) { //parameters that do not have an argument to the right if (args[i]=="-nodocbase") { doc_base=false; } else if (args[i]=="-noquit") { noquit=true; } else if (args[i]=="-convert_old") { convert_old=true; } else if (args[i]=="-editor" || args[i]=="-e") { editor=true; } else if (args[i] == "-pm" || args[i] == "-project_manager") { project_manager_request = true; } else if (args[i].length() && args[i][0] != '-' && game_path == "") { game_path=args[i]; } //parameters that have an argument to the right else if (i < (args.size()-1)) { bool parsed_pair=true; if (args[i]=="-doctool") { doc_tool=args[i+1]; } else if (args[i]=="-script" || args[i]=="-s") { script=args[i+1]; } else if (args[i]=="-level" || args[i]=="-l") { OS::get_singleton()->_custom_level=args[i+1]; } else if (args[i]=="-test") { test=args[i+1]; } else if (args[i]=="-optimize") { optimize=args[i+1]; } else if (args[i]=="-optimize_preset") { optimize_preset=args[i+1]; } else if (args[i]=="-export") { editor=true; //needs editor _export_platform=args[i+1]; } else if (args[i]=="-export_debug") { editor=true; //needs editor _export_platform=args[i+1]; export_debug=true; } else if (args[i]=="-import") { editor=true; //needs editor _import=args[i+1]; } else if (args[i]=="-import_script") { editor=true; //needs editor _import_script=args[i+1]; } else if (args[i]=="-dumpstrings") { editor=true; //needs editor dumpstrings=args[i+1]; } else { // The parameter does not match anything known, don't skip the next argument parsed_pair=false; } if (parsed_pair) { i++; } } } if (editor) Globals::get_singleton()->set("editor_active",true); String main_loop_type; #ifdef TOOLS_ENABLED if(doc_tool!="") { DocData doc; doc.generate(doc_base); DocData docsrc; if (docsrc.load(doc_tool)==OK) { print_line("Doc exists. Merging.."); doc.merge_from(docsrc); } else { print_line("No Doc exists. Generating empty."); } doc.save(doc_tool); return false; } if (optimize!="") editor=true; //need editor #endif if (_export_platform!="") { if (game_path=="") { String err="Command line param "; err+=export_debug?"-export_debug":"-export"; err+=" passed but no destination path given.\n"; err+="Please specify the binary's file path to export to. Aborting export."; ERR_PRINT(err.utf8().get_data()); return false; } } if(script=="" && game_path=="" && String(GLOBAL_DEF("application/main_scene",""))!="") { game_path=GLOBAL_DEF("application/main_scene",""); } MainLoop *main_loop=NULL; if (editor) { main_loop = memnew(SceneTree); }; if (test!="") { #ifdef DEBUG_ENABLED main_loop = test_main(test,args); if (!main_loop) return false; #endif } else if (script!="") { Ref<Script> script_res = ResourceLoader::load(script); ERR_EXPLAIN("Can't load script: "+script); ERR_FAIL_COND_V(script_res.is_null(),false); if( script_res->can_instance() /*&& script_res->inherits_from("SceneTreeScripted")*/) { StringName instance_type=script_res->get_instance_base_type(); Object *obj = ObjectTypeDB::instance(instance_type); MainLoop *script_loop = obj?obj->cast_to<MainLoop>():NULL; if (!script_loop) { if (obj) memdelete(obj); ERR_EXPLAIN("Can't load script '"+script+"', it does not inherit from a MainLoop type"); ERR_FAIL_COND_V(!script_loop,false); } script_loop->set_init_script(script_res); main_loop=script_loop; } else { return false; } } else { main_loop_type=GLOBAL_DEF("application/main_loop_type",""); } if (!main_loop && main_loop_type=="") main_loop_type="SceneTree"; if (!main_loop) { if (!ObjectTypeDB::type_exists(main_loop_type)) { OS::get_singleton()->alert("godot: error: MainLoop type doesn't exist: "+main_loop_type); return false; } else { Object *ml = ObjectTypeDB::instance(main_loop_type); if (!ml) { ERR_EXPLAIN("Can't instance MainLoop type"); ERR_FAIL_V(false); } main_loop=ml->cast_to<MainLoop>(); if (!main_loop) { memdelete(ml); ERR_EXPLAIN("Invalid MainLoop type"); ERR_FAIL_V(false); } } } if (main_loop->is_type("SceneTree")) { SceneTree *sml = main_loop->cast_to<SceneTree>(); if (debug_collisions) { sml->set_debug_collisions_hint(true); } if (debug_navigation) { sml->set_debug_navigation_hint(true); } #ifdef TOOLS_ENABLED EditorNode *editor_node=NULL; if (editor) { editor_node = memnew( EditorNode ); sml->get_root()->add_child(editor_node); //root_node->set_editor(editor); //startup editor if (_export_platform!="") { editor_node->export_platform(_export_platform,game_path,export_debug,"",true); game_path=""; //no load anything } } #endif if (!editor) { //standard helpers that can be changed from main config String stretch_mode = GLOBAL_DEF("display/stretch_mode","disabled"); String stretch_aspect = GLOBAL_DEF("display/stretch_aspect","ignore"); Size2i stretch_size = Size2(GLOBAL_DEF("display/width",0),GLOBAL_DEF("display/height",0)); SceneTree::StretchMode sml_sm=SceneTree::STRETCH_MODE_DISABLED; if (stretch_mode=="2d") sml_sm=SceneTree::STRETCH_MODE_2D; else if (stretch_mode=="viewport") sml_sm=SceneTree::STRETCH_MODE_VIEWPORT; SceneTree::StretchAspect sml_aspect=SceneTree::STRETCH_ASPECT_IGNORE; if (stretch_aspect=="keep") sml_aspect=SceneTree::STRETCH_ASPECT_KEEP; else if (stretch_aspect=="keep_width") sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_WIDTH; else if (stretch_aspect=="keep_height") sml_aspect=SceneTree::STRETCH_ASPECT_KEEP_HEIGHT; sml->set_screen_stretch(sml_sm,sml_aspect,stretch_size); sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true)); String appname = Globals::get_singleton()->get("application/name"); appname = TranslationServer::get_singleton()->translate(appname); OS::get_singleton()->set_window_title(appname); } else { GLOBAL_DEF("display/stretch_mode","disabled"); Globals::get_singleton()->set_custom_property_info("display/stretch_mode",PropertyInfo(Variant::STRING,"display/stretch_mode",PROPERTY_HINT_ENUM,"disabled,2d,viewport")); GLOBAL_DEF("display/stretch_aspect","ignore"); Globals::get_singleton()->set_custom_property_info("display/stretch_aspect",PropertyInfo(Variant::STRING,"display/stretch_aspect",PROPERTY_HINT_ENUM,"ignore,keep,keep_width,keep_height")); sml->set_auto_accept_quit(GLOBAL_DEF("application/auto_accept_quit",true)); } if (game_path!="" && !project_manager_request) { String local_game_path=game_path.replace("\\","/"); if (!local_game_path.begins_with("res://")) { bool absolute=(local_game_path.size()>1) && (local_game_path[0]=='/' || local_game_path[1]==':'); if (!absolute) { if (Globals::get_singleton()->is_using_datapack()) { local_game_path="res://"+local_game_path; } else { int sep=local_game_path.find_last("/"); if (sep==-1) { DirAccess *da = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); local_game_path=da->get_current_dir()+"/"+local_game_path; memdelete(da) ; } else { DirAccess *da = DirAccess::open(local_game_path.substr(0,sep)); if (da) { local_game_path=da->get_current_dir()+"/"+local_game_path.substr(sep+1,local_game_path.length());; memdelete(da); } } } } } local_game_path=Globals::get_singleton()->localize_path(local_game_path); #ifdef TOOLS_ENABLED if (editor) { if (_import!="") { //editor_node->import_scene(_import,local_game_path,_import_script); if (!noquit) sml->quit(); game_path=""; //no load anything } else { Error serr = editor_node->load_scene(local_game_path); if (serr==OK) { if (optimize!="") { editor_node->save_optimized_copy(optimize,optimize_preset); if (!noquit) sml->quit(); } if (dumpstrings!="") { editor_node->save_translatable_strings(dumpstrings); if (!noquit) sml->quit(); } } } OS::get_singleton()->set_context(OS::CONTEXT_EDITOR); //editor_node->set_edited_scene(game); } else { #endif { //autoload List<PropertyInfo> props; Globals::get_singleton()->get_property_list(&props); //first pass, add the constants so they exist before any script is loaded for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) { String s = E->get().name; if (!s.begins_with("autoload/")) continue; String name = s.get_slicec('/',1); String path = Globals::get_singleton()->get(s); bool global_var=false; if (path.begins_with("*")) { global_var=true; } if (global_var) { for(int i=0;i<ScriptServer::get_language_count();i++) { ScriptServer::get_language(i)->add_global_constant(name,Variant()); } } } //second pass, load into global constants List<Node*> to_add; for(List<PropertyInfo>::Element *E=props.front();E;E=E->next()) { String s = E->get().name; if (!s.begins_with("autoload/")) continue; String name = s.get_slicec('/',1); String path = Globals::get_singleton()->get(s); bool global_var=false; if (path.begins_with("*")) { global_var=true; path=path.substr(1,path.length()-1); } RES res = ResourceLoader::load(path); ERR_EXPLAIN("Can't autoload: "+path); ERR_CONTINUE(res.is_null()); Node *n=NULL; if (res->is_type("PackedScene")) { Ref<PackedScene> ps = res; n=ps->instance(); } else if (res->is_type("Script")) { Ref<Script> s = res; StringName ibt = s->get_instance_base_type(); bool valid_type = ObjectTypeDB::is_type(ibt,"Node"); ERR_EXPLAIN("Script does not inherit a Node: "+path); ERR_CONTINUE( !valid_type ); Object *obj = ObjectTypeDB::instance(ibt); ERR_EXPLAIN("Cannot instance script for autoload, expected 'Node' inheritance, got: "+String(ibt)); ERR_CONTINUE( obj==NULL ); n = obj->cast_to<Node>(); n->set_script(s.get_ref_ptr()); } ERR_EXPLAIN("Path in autoload not a node or script: "+path); ERR_CONTINUE(!n); n->set_name(name); //defer so references are all valid on _ready() //sml->get_root()->add_child(n); to_add.push_back(n); if (global_var) { for(int i=0;i<ScriptServer::get_language_count();i++) { ScriptServer::get_language(i)->add_global_constant(name,n); } } } for(List<Node*>::Element *E=to_add.front();E;E=E->next()) { sml->get_root()->add_child(E->get()); } } Node *scene=NULL; Ref<PackedScene> scenedata = ResourceLoader::load(local_game_path); if (scenedata.is_valid()) scene=scenedata->instance(); ERR_EXPLAIN("Failed loading scene: "+local_game_path); ERR_FAIL_COND_V(!scene,false) //sml->get_root()->add_child(scene); sml->add_current_scene(scene); String iconpath = GLOBAL_DEF("application/icon","Variant()"""); if (iconpath!="") { Image icon; if (icon.load(iconpath)==OK) OS::get_singleton()->set_icon(icon); } //singletons #ifdef TOOLS_ENABLED } #endif } #ifdef TOOLS_ENABLED /*if (_export_platform!="") { sml->quit(); }*/ /* if (sml->get_root_node()) { Console *console = memnew( Console ); sml->get_root_node()->cast_to<RootNode>()->set_console(console); if (GLOBAL_DEF("console/visible_default",false).operator bool()) { console->show(); } else {P console->hide(); }; } */ if (project_manager_request || (script=="" && test=="" && game_path=="" && !editor)) { ProjectManager *pmanager = memnew( ProjectManager ); sml->get_root()->add_child(pmanager); OS::get_singleton()->set_context(OS::CONTEXT_PROJECTMAN); } #endif } OS::get_singleton()->set_main_loop( main_loop ); return true; }