The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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
actionpressed("Eat"):
if get
overlappingbodies().size() > 0:
queue
free()

And that's scripted for the truffle. I was thinking maybe using an onenteredbody 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.

in Engine by (73 points)

1 Answer

0 votes

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.

by (526 points)

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?

This has been resolved thank you.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.