diff --git a/scene/main/scene_main_loop.cpp b/scene/main/scene_main_loop.cpp index a7ef7ca7c1c..77156203b81 100644 --- a/scene/main/scene_main_loop.cpp +++ b/scene/main/scene_main_loop.cpp @@ -45,6 +45,30 @@ #include "scene/resources/material.h" #include "scene/resources/mesh.h" + +void SceneTreeTimer::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_time_left","time"),&SceneTreeTimer::set_time_left); + ObjectTypeDB::bind_method(_MD("get_time_left"),&SceneTreeTimer::get_time_left); + + ADD_SIGNAL(MethodInfo("timeout")); +} + + +void SceneTreeTimer::set_time_left(float p_time) { + time_left=p_time; +} + +float SceneTreeTimer::get_time_left() const { + return time_left; +} + + +SceneTreeTimer::SceneTreeTimer() { + time_left=0; +} + + void SceneTree::tree_changed() { tree_version++; @@ -547,6 +571,23 @@ bool SceneTree::idle(float p_time){ _flush_delete_queue(); + //go through timers + + for (List >::Element *E=timers.front();E;) { + + List >::Element *N = E->next(); + + float time_left = E->get()->get_time_left(); + time_left-=p_time; + E->get()->set_time_left(time_left); + + if (time_left<0) { + E->get()->emit_signal("timeout"); + timers.erase(E); + } + E=N; + } + return _quit; } @@ -1604,6 +1645,16 @@ void SceneTree::drop_files(const Vector& p_files,int p_from_screen) { MainLoop::drop_files(p_files,p_from_screen); } + +Ref SceneTree::create_timer(float p_delay_sec) { + + Ref stt; + stt.instance(); + stt->set_time_left(p_delay_sec); + timers.push_back(stt); + return stt; +} + void SceneTree::_bind_methods() { @@ -1634,6 +1685,8 @@ void SceneTree::_bind_methods() { ObjectTypeDB::bind_method(_MD("is_paused"),&SceneTree::is_paused); ObjectTypeDB::bind_method(_MD("set_input_as_handled"),&SceneTree::set_input_as_handled); + ObjectTypeDB::bind_method(_MD("create_timer:SceneTreeTimer","time_sec"),&SceneTree::create_timer); + ObjectTypeDB::bind_method(_MD("get_node_count"),&SceneTree::get_node_count); ObjectTypeDB::bind_method(_MD("get_frame"),&SceneTree::get_frame); diff --git a/scene/main/scene_main_loop.h b/scene/main/scene_main_loop.h index 38d13c04478..6129a124467 100644 --- a/scene/main/scene_main_loop.h +++ b/scene/main/scene_main_loop.h @@ -47,6 +47,22 @@ class Viewport; class Material; class Mesh; + + +class SceneTreeTimer : public Reference { + OBJ_TYPE(SceneTreeTimer,Reference); + + float time_left; +protected: + static void _bind_methods(); +public: + + void set_time_left(float p_time); + float get_time_left() const; + + SceneTreeTimer(); +}; + class SceneTree : public MainLoop { _THREAD_SAFE_CLASS_ @@ -155,6 +171,8 @@ private: void _change_scene(Node* p_to); //void _call_group(uint32_t p_call_flags,const StringName& p_group,const StringName& p_function,const Variant& p_arg1,const Variant& p_arg2); + List > timers; + static SceneTree *singleton; friend class Node; @@ -339,6 +357,8 @@ public: Error change_scene_to(const Ref& p_scene); Error reload_current_scene(); + Ref create_timer(float p_delay_sec); + //used by Main::start, don't use otherwise void add_current_scene(Node * p_current); diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index bc0951e4368..be2c12d63a6 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -632,6 +632,7 @@ void register_scene_types() { ObjectTypeDB::register_type(); ObjectTypeDB::register_type(); + ObjectTypeDB::register_virtual_type(); //sorry, you can't create it OS::get_singleton()->yield(); //may take time to init