Added an easy API to yield to a specific time by doing:

yield( get_tree().create_timer(5.0), "timeout" )
This commit is contained in:
Juan Linietsky 2016-08-06 21:39:50 -03:00
parent 0ed5dbb24d
commit 3d7c10e9ce
3 changed files with 74 additions and 0 deletions

View file

@ -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<Ref<SceneTreeTimer> >::Element *E=timers.front();E;) {
List<Ref<SceneTreeTimer> >::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<String>& p_files,int p_from_screen) {
MainLoop::drop_files(p_files,p_from_screen);
}
Ref<SceneTreeTimer> SceneTree::create_timer(float p_delay_sec) {
Ref<SceneTreeTimer> 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);

View file

@ -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<Ref<SceneTreeTimer> > timers;
static SceneTree *singleton;
friend class Node;
@ -339,6 +357,8 @@ public:
Error change_scene_to(const Ref<PackedScene>& p_scene);
Error reload_current_scene();
Ref<SceneTreeTimer> create_timer(float p_delay_sec);
//used by Main::start, don't use otherwise
void add_current_scene(Node * p_current);

View file

@ -632,6 +632,7 @@ void register_scene_types() {
ObjectTypeDB::register_type<PackedScene>();
ObjectTypeDB::register_type<SceneTree>();
ObjectTypeDB::register_virtual_type<SceneTreeTimer>(); //sorry, you can't create it
OS::get_singleton()->yield(); //may take time to init