Change the scenario (TextureRect) after the timer reach a number

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Tittigames

**Hi guys, ive just started to use this software and im very newbie, but i already have a notion of the basics, i have complete my first game from the Godot tutorial “Dodget the creeps” this one and succesfully make a nice visual game, im musician too and want to add some extra features to make game more interesting, so i made a song that change the melody at a certain second, lets say when the player reach the 25 second, but also want to add something more, there is a way to change the game background (TextureRect) when the timer reach a second ?, dont need to be an specific second, want to change from a city to the beach, then to a snow place and finally a forest, mean change 4 times, this is what i got in the Main script

enter code herefunc game_over():

func new_game():

score = 0
$HUD.show_message("Get Ready")

func _on_MobTimer_timeout():
var mob = Mob.instance()
var direction = $MobPath/MobSpawnLocation.rotation + PI/2
mob.position = $MobPath/MobSpawnLocation.position

direction += rand_range (-PI/4, PI/4)
mob.rotation = direction

mob.set_linear_velocity(Vector2(rand_range(mob.MIN_SPEED, mob.MAX_SPEED) ,0).rotated(direction))

func _on_ScoreTimer_timeout():
score +=1

func _on_StartTimer_timeout():

i add a game sc, ty!

very nice, I’m also new in this, so far going good.

theoMCI | 2020-03-19 15:44

:bust_in_silhouette: Reply From: njamster

Assuming your TextureRect-node is called “BackgroundImage”:

get_node("BackgroundImage").texture = load("<PathToImage>")

This assumes that the node is a direct child of the node your script is attached to. If not, you need to adapt the path in the get_node-call accordingly. In order to get the path to an image inside your project, simply drag it from the FileSystem-tab into the script editor - this will insert the path as a string (starting with “res://”).

ok Will be clear as posible, cause not sure if you was just saying how to add a simple background with a script, i have already added a background just adding a TextureRect-node to the main-node and just dragging the image, no needed any script, but i have an static image, what i wanted to do is change the image to another one while playing after a certain time, idk if that has something to do whit programming something to the score timer, i have been traying with the paralax option, but thats only works with repeated images and since the main character dont move out of the same scenario the screen cant move too

Tittigames | 2020-03-20 05:44

I found how to do it!!! i added a videoplayer node in the background with a video with the differents backgrounds! the only problem now is when the game reset the video dont reset as well so get stuck in the last image =(

Tittigames | 2020-03-20 07:46

If you know how to change the texture from code (which my answer was about), you can add that code to your timer-callback as well:

func _on_Timer_timeout():
	get_node("BackgroundImage").texture = load("<PathToImage>")

Create a Timer with autostart checked, one_start unchecked and wait_time of 1 (second). Connect it’s timeout-signal to the callback from above. That way the image will be changed after one second from the one you set in the editor to the one set in code. If you wan a whole sequence, just introduce a variable to keep track of the time:

var time_passed = 0.0

func _on_Timer_timeout():
    time_passed += 1.0
    match time_passed:
            $BackgroundImage.texture = load("<PathToImage1>")
            $BackgroundImage.texture = load("<PathToImage2>")

Depending on your programming skills and the length of the sequence, you might be better off using an AnimationPlayer. However, the idea is the same: define a default in the editor and change the texture after certain time intervals have passed.

njamster | 2020-03-20 12:49