mirror of
https://github.com/godotengine/godot.git
synced 2024-11-21 19:42:43 +00:00
added a built-in scene changer API, closes #1928
This commit is contained in:
parent
2696ecb769
commit
b6b346e8ae
4
demos/misc/scene_changer/engine.cfg
Normal file
4
demos/misc/scene_changer/engine.cfg
Normal file
@ -0,0 +1,4 @@
|
||||
[application]
|
||||
|
||||
name="Scene Changer"
|
||||
main_scene="res://scene_a.scn"
|
17
demos/misc/scene_changer/scene_a.gd
Normal file
17
demos/misc/scene_changer/scene_a.gd
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
extends Panel
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
func _ready():
|
||||
# Initalization here
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
func _on_goto_scene_pressed():
|
||||
get_tree().change_scene("res://scene_b.scn")
|
||||
pass # replace with function body
|
BIN
demos/misc/scene_changer/scene_a.scn
Normal file
BIN
demos/misc/scene_changer/scene_a.scn
Normal file
Binary file not shown.
17
demos/misc/scene_changer/scene_b.gd
Normal file
17
demos/misc/scene_changer/scene_b.gd
Normal file
@ -0,0 +1,17 @@
|
||||
|
||||
extends Panel
|
||||
|
||||
# member variables here, example:
|
||||
# var a=2
|
||||
# var b="textvar"
|
||||
|
||||
func _ready():
|
||||
# Initalization here
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
func _on_goto_scene_pressed():
|
||||
get_tree().change_scene("res://scene_a.scn")
|
||||
pass # replace with function body
|
BIN
demos/misc/scene_changer/scene_b.scn
Normal file
BIN
demos/misc/scene_changer/scene_b.scn
Normal file
Binary file not shown.
@ -1248,7 +1248,8 @@ bool Main::start() {
|
||||
|
||||
ERR_EXPLAIN("Failed loading scene: "+local_game_path);
|
||||
ERR_FAIL_COND_V(!scene,false)
|
||||
sml->get_root()->add_child(scene);
|
||||
//sml->get_root()->add_child(scene);
|
||||
sml->add_current_scene(scene);
|
||||
|
||||
String iconpath = GLOBAL_DEF("application/icon","Variant()""");
|
||||
if (iconpath!="") {
|
||||
|
@ -41,7 +41,7 @@
|
||||
#include "scene/scene_string_names.h"
|
||||
#include "io/resource_loader.h"
|
||||
#include "viewport.h"
|
||||
|
||||
#include "scene/resources/packed_scene.h"
|
||||
|
||||
void SceneTree::tree_changed() {
|
||||
|
||||
@ -51,6 +51,9 @@ void SceneTree::tree_changed() {
|
||||
|
||||
void SceneTree::node_removed(Node *p_node) {
|
||||
|
||||
if (current_scene==p_node) {
|
||||
current_scene=NULL;
|
||||
}
|
||||
emit_signal(node_removed_name,p_node);
|
||||
if (call_lock>0)
|
||||
call_skip.insert(p_node);
|
||||
@ -984,6 +987,63 @@ Node *SceneTree::get_edited_scene_root() const {
|
||||
}
|
||||
#endif
|
||||
|
||||
void SceneTree::set_current_scene(Node* p_scene) {
|
||||
|
||||
ERR_FAIL_COND(p_scene && p_scene->get_parent()!=root);
|
||||
current_scene=p_scene;
|
||||
}
|
||||
|
||||
Node* SceneTree::get_current_scene() const{
|
||||
|
||||
return current_scene;
|
||||
}
|
||||
|
||||
void SceneTree::_change_scene(Node* p_to) {
|
||||
|
||||
if (current_scene) {
|
||||
memdelete( current_scene );
|
||||
current_scene=NULL;
|
||||
}
|
||||
|
||||
if (p_to) {
|
||||
current_scene=p_to;
|
||||
root->add_child(p_to);
|
||||
}
|
||||
}
|
||||
|
||||
Error SceneTree::change_scene(const String& p_path){
|
||||
|
||||
Ref<PackedScene> new_scene = ResourceLoader::load(p_path);
|
||||
if (new_scene.is_null())
|
||||
return ERR_CANT_OPEN;
|
||||
|
||||
return change_scene_to(new_scene);
|
||||
|
||||
}
|
||||
Error SceneTree::change_scene_to(const Ref<PackedScene>& p_scene){
|
||||
|
||||
Node *new_scene=NULL;
|
||||
if (p_scene.is_valid()) {
|
||||
new_scene = p_scene->instance();
|
||||
ERR_FAIL_COND_V(!new_scene,ERR_CANT_CREATE);
|
||||
}
|
||||
|
||||
call_deferred("_change_scene",new_scene);
|
||||
return OK;
|
||||
|
||||
}
|
||||
Error SceneTree::reload_current_scene() {
|
||||
|
||||
ERR_FAIL_COND_V(!current_scene,ERR_UNCONFIGURED);
|
||||
String fname = current_scene->get_filename();
|
||||
return change_scene(fname);
|
||||
}
|
||||
|
||||
void SceneTree::add_current_scene(Node * p_current) {
|
||||
|
||||
current_scene=p_current;
|
||||
root->add_child(p_current);
|
||||
}
|
||||
|
||||
void SceneTree::_bind_methods() {
|
||||
|
||||
@ -1016,10 +1076,11 @@ void SceneTree::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_screen_stretch","mode","aspect","minsize"),&SceneTree::set_screen_stretch);
|
||||
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("queue_delete","obj"),&SceneTree::queue_delete);
|
||||
|
||||
|
||||
|
||||
|
||||
MethodInfo mi;
|
||||
mi.name="call_group";
|
||||
mi.arguments.push_back( PropertyInfo( Variant::INT, "flags"));
|
||||
@ -1033,6 +1094,16 @@ void SceneTree::_bind_methods() {
|
||||
|
||||
ObjectTypeDB::bind_native_method(METHOD_FLAGS_DEFAULT,"call_group",&SceneTree::_call_group,mi,defargs);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("set_current_scene","child_node"),&SceneTree::set_current_scene);
|
||||
ObjectTypeDB::bind_method(_MD("get_current_scene"),&SceneTree::get_current_scene);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("change_scene","path"),&SceneTree::change_scene);
|
||||
ObjectTypeDB::bind_method(_MD("change_scene_to","packed_scene"),&SceneTree::change_scene_to);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("reload_current_scene"),&SceneTree::reload_current_scene);
|
||||
|
||||
ObjectTypeDB::bind_method(_MD("_change_scene"),&SceneTree::_change_scene);
|
||||
|
||||
ADD_SIGNAL( MethodInfo("tree_changed") );
|
||||
ADD_SIGNAL( MethodInfo("node_removed",PropertyInfo( Variant::OBJECT, "node") ) );
|
||||
ADD_SIGNAL( MethodInfo("screen_resized") );
|
||||
@ -1077,6 +1148,7 @@ SceneTree::SceneTree() {
|
||||
//root->set_world_2d( Ref<World2D>( memnew( World2D )));
|
||||
root->set_as_audio_listener(true);
|
||||
root->set_as_audio_listener_2d(true);
|
||||
current_scene=NULL;
|
||||
|
||||
stretch_mode=STRETCH_MODE_DISABLED;
|
||||
stretch_aspect=STRETCH_ASPECT_IGNORE;
|
||||
|
@ -42,7 +42,7 @@
|
||||
|
||||
|
||||
class SceneTree;
|
||||
|
||||
class PackedScene;
|
||||
class Node;
|
||||
class Viewport;
|
||||
|
||||
@ -136,7 +136,9 @@ private:
|
||||
|
||||
Array _get_nodes_in_group(const StringName& p_group);
|
||||
|
||||
Node *current_scene;
|
||||
|
||||
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);
|
||||
|
||||
friend class Node;
|
||||
@ -234,6 +236,17 @@ public:
|
||||
Node *get_edited_scene_root() const;
|
||||
#endif
|
||||
|
||||
void set_current_scene(Node* p_scene);
|
||||
Node* get_current_scene() const;
|
||||
Error change_scene(const String& p_path);
|
||||
Error change_scene_to(const Ref<PackedScene>& p_scene);
|
||||
Error reload_current_scene();
|
||||
|
||||
//used by Main::start, don't use otherwise
|
||||
void add_current_scene(Node * p_current);
|
||||
|
||||
|
||||
|
||||
SceneTree();
|
||||
~SceneTree();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user