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

: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