- Edited
I'm not sure what I'm doing wrong but I am writing some scaffolding for a game where there can be multiple different day / night colors and scenes using a procedural sky material.
In the editor the artist can create a folder of procedural sky materials resources and drag them into the time of day they can be used.
Everything seems to be working fine when I run just one of the functions by itself but when I go to run the scene I get a stack overflow from infinite recursion. I think this means that me awaiting the tween finished signal is not working for some reason by I'm not sure why.
Anyways when I litter the script with a bunch of timers, it seems to work for some reason! I really have no idea why it should work with timers and that the tween finished would not wait like I thought it should. Any ideas?
This works
extends WorldEnvironment
@export var MorningSkies : Array[ProceduralSkyMaterial] = []
@export var DaySkies: Array[ProceduralSkyMaterial] = []
@export var EveningSkies: Array[ProceduralSkyMaterial] = []
@export var NightSkies: Array[ProceduralSkyMaterial] = []
# Called when the node enters the scene tree for the first time.
func _ready():
sunrise()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func tween_to_random_sky(skies: Array):
var sky = skies[randi() % skies.size()]
var tween = get_tree().create_tween()
tween.set_parallel()
tween.tween_property(self, "environment:sky:sky_material:sky_top_color", sky.get("sky_top_color"), 5)
tween.tween_property(self, "environment:sky:sky_material:sky_horizon_color", sky.get("sky_horizon_color"), 5)
tween.tween_property(self, "environment:sky:sky_material:ground_bottom_color", sky.get("ground_bottom_color"), 5)
tween.tween_property(self, "environment:sky:sky_material:ground_horizon_color", sky.get("ground_horizon_color"), 5)
await tween.finished
func sunrise():
tween_to_random_sky(MorningSkies)
var timer = get_tree().create_timer(5)
await timer.timeout
print("The sun rose")
day()
func sunset():
tween_to_random_sky(EveningSkies)
var timer = get_tree().create_timer(5)
await timer.timeout
print("The sun set")
night()
func day():
tween_to_random_sky(DaySkies)
var timer = get_tree().create_timer(5)
await timer.timeout
print("It's day")
sunset()
func night():
tween_to_random_sky(NightSkies)
var timer = get_tree().create_timer(5)
await timer.timeout
print("It's night")
sunrise()
This does not work
extends WorldEnvironment
@export var MorningSkies : Array[ProceduralSkyMaterial] = []
@export var DaySkies: Array[ProceduralSkyMaterial] = []
@export var EveningSkies: Array[ProceduralSkyMaterial] = []
@export var NightSkies: Array[ProceduralSkyMaterial] = []
# Called when the node enters the scene tree for the first time.
func _ready():
sunrise()
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
pass
func tween_to_random_sky(skies: Array):
var sky = skies[randi() % skies.size()]
var tween = get_tree().create_tween()
tween.set_parallel()
tween.tween_property(self, "environment:sky:sky_material:sky_top_color", sky.get("sky_top_color"), 5)
tween.tween_property(self, "environment:sky:sky_material:sky_horizon_color", sky.get("sky_horizon_color"), 5)
tween.tween_property(self, "environment:sky:sky_material:ground_bottom_color", sky.get("ground_bottom_color"), 5)
tween.tween_property(self, "environment:sky:sky_material:ground_horizon_color", sky.get("ground_horizon_color"), 5)
await tween.finished
func sunrise():
tween_to_random_sky(MorningSkies)
print("The sun rose")
day()
func sunset():
tween_to_random_sky(EveningSkies)
print("The sun set")
night()
func day():
tween_to_random_sky(DaySkies)
print("It's day")
sunset()
func night():
tween_to_random_sky(NightSkies)
print("It's night")
sunrise()