I want to transition into the next level of my scene, when the character eats a truffle and the sound effect is played

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

I have my first level complete, and the pig has eaten the truffle, I want to be a transition into the next scene, which starts the next level with the pig at the start of the scene. How could I signal the end of the scene using the pig eating the truffle. The code for the pig eating the truffle looks like:

func _input(event):
if event.is_action_pressed(“Eat”):
if get_overlapping_bodies().size() > 0:
queue_free()

And that’s scripted for the truffle. I was thinking maybe using an on_entered_body signal, and then connecting that to the transition? Any ideas would be much appreciated. Also, I need it to be sort of like sonic when he collects the last ring and he is transported to a new world.

:bust_in_silhouette: Reply From: ponponyaya

The simple way to change scene:

var targetScene = "res://Levels/NextLevelScenePath.tscn" # note: It's String
get_tree().change_scene(targetScene)

The real question is about when to use the above codes.
It based on what effects you want to show.
I mean if you want a effects like:
player pig eat truffle → truffle disspear → do effects you want → change to new scene.

Suppose you just have one(unique) truffle to eat.
Then you can code like following:
(if there are lots truffles then you can check the quantity, if got enough quantity then change scene, else queue_free the truffle.)

[In truffle script]

extends Area2D

var targetScene = "res://Levels/NewScenePath.tscn"    ​

func _input(event):
	if event.is_action_pressed("ui_select"):
		if get_overlapping_bodies().size() > 0:
			visible = false # hide truffle
			# do effects you want
			change_to_next_level()

func change_to_next_level():
	get_tree().change_scene(targetScene)
	queue_free

Hope this help.

That was very helpful, many thanks. However, now when I try to enter the line for my var targetScene as a string of the path of my new level, i.e. res://Level1.tscn, first of all it doesn’t show the list of options, and second of all it highlights this line an error. I was wondering why it is an error for the targetScene variable. By the way it says it is an unknown character as a parsing error?

Juan | 2022-01-11 19:31

This has been resolved thank you.

Juan | 2022-01-11 22:23