Skip to content

Instantly share code, notes, and snippets.

@erodozer
Last active January 28, 2020 10:43
Show Gist options
  • Save erodozer/c0a47c81798eb9260f1b38ae729e72f6 to your computer and use it in GitHub Desktop.
Save erodozer/c0a47c81798eb9260f1b38ae729e72f6 to your computer and use it in GitHub Desktop.

Godot Scene Manager

Simple scene manager with simple transition effects using RPG Maker greyscale images. Doesn't do async loading at all, but that's fine for small games that just want a little pizzaz to their visuals. Just drop the file into your project and add it to your Autoloads. Then start doing SceneManager.change_scene('res://myscene.tscn') instead of get_tree().change_scene('res://myscene.tscn') and you'll be good to go.

[gd_scene load_steps=5 format=2]
[sub_resource type="GDScript" id=1]
script/source = "extends Node
var next_scene = null
var is_ready setget _finish_transition
onready var anim = $Fader/AnimationPlayer
func _finish_transition(value):
if value:
is_ready = true
anim.play(\"Fade\")
$Fader/Label.visible = false
get_tree().paused = false
func transition_menu(anim_name):
var tree = get_tree()
tree.change_scene(next_scene)
anim.disconnect(\"animation_finished\", self, \"transition_menu\")
if is_ready:
self.is_ready = true
else:
$Fader/Label.visible = true
next_scene = null
func change_scene(goto, hold=false):
\"\"\"
Changes scene with a transition.
You can hold the fade back in transition if desired until
the next scene is in a state that is considered \"ready\"
\"\"\"
next_scene = goto
anim.play_backwards(\"Fade\")
anim.connect(\"animation_finished\", self, \"transition_menu\")
is_ready = not hold
get_tree().paused = true
"
[sub_resource type="Shader" id=2]
code = "shader_type canvas_item;
render_mode blend_mix;
uniform float transition: hint_range(0, 1);
float val(vec3 color) {
return max(max(color.r, color.g), color.b);
}
void fragment() {
vec4 sample = texture(TEXTURE, UV.xy);
if (val(sample.rgb) < transition) {
COLOR = vec4(0,0,0,0)
} else {
COLOR = vec4(0,0,0,1);
}
}"
_sections_unfolded = [ "Resource" ]
[sub_resource type="ShaderMaterial" id=3]
render_priority = 0
shader = SubResource( 2 )
shader_param/transition = 0.0
_sections_unfolded = [ "shader_param" ]
[sub_resource type="Animation" id=4]
length = 0.8
loop = false
step = 0.1
tracks/0/type = "value"
tracks/0/path = NodePath(".:material:shader_param/transition")
tracks/0/interp = 1
tracks/0/loop_wrap = true
tracks/0/imported = false
tracks/0/enabled = true
tracks/0/keys = {
"times": PoolRealArray( 0, 0.7 ),
"transitions": PoolRealArray( 1, 1 ),
"update": 0,
"values": [ 0.0, 1.0 ]
}
tracks/1/type = "value"
tracks/1/path = NodePath(".:visible")
tracks/1/interp = 1
tracks/1/loop_wrap = true
tracks/1/imported = false
tracks/1/enabled = true
tracks/1/keys = {
"times": PoolRealArray( 0, 0.7, 0.8 ),
"transitions": PoolRealArray( 1, 1, 1 ),
"update": 1,
"values": [ true, true, false ]
}
[node name="SceneManager" type="CanvasLayer" index="0"]
pause_mode = 2
layer = 128
offset = Vector2( 0, 0 )
rotation = 0.0
scale = Vector2( 1, 1 )
transform = Transform2D( 1, 0, 0, 1, 0, 0 )
script = SubResource( 1 )
[node name="Fader" type="TextureRect" parent="." index="0"]
material = SubResource( 3 )
anchor_left = 0.0
anchor_top = 0.0
anchor_right = 1.0
anchor_bottom = 1.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 1
expand = true
stretch_mode = 0
[node name="Label" type="Label" parent="Fader" index="0"]
visible = false
anchor_left = 0.0
anchor_top = 0.0
anchor_right = 0.0
anchor_bottom = 0.0
margin_left = 236.0
margin_top = 221.0
margin_right = 316.0
margin_bottom = 235.0
rect_pivot_offset = Vector2( 0, 0 )
rect_clip_content = false
mouse_filter = 2
mouse_default_cursor_shape = 0
size_flags_horizontal = 1
size_flags_vertical = 4
text = "Loading..."
percent_visible = 1.0
lines_skipped = 0
max_lines_visible = -1
[node name="AnimationPlayer" type="AnimationPlayer" parent="Fader" index="1"]
root_node = NodePath("..")
autoplay = "Fade"
playback_process_mode = 1
playback_default_blend_time = 0.0
playback_speed = 1.0
anims/Fade = SubResource( 4 )
blend_times = [ ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment