Godot Version
4
Question
I want to change something based on what scene it’s in. i assume it would use elif or something but i don’t know where to start and couldn’t find anything. Basically i want to have code where the item will change a property depending on what scene it’s currently in.
I have mobs that take you to certain scenes when hit, and i need them to be able change which ones depending on which scene they’re currently in. (Right now they’re programmed to send you to 1 specific scene and i need to them to send you to a different one depending on which scene you’re already in) Here’s my current mob code:
extends Node2D
@onready var parent = get_parent() as PathFollow2D
func _physics_process(delta):
if parent is PathFollow2D:
parent.set_progress(parent.get_progress() + 200 *delta)
func _on_area_2d_body_entered(area):
if area.is_in_group("playerreal"):
get_tree().change_scene_to_file("res://cutscene_1.tscn")
You can determine which scene is current by this
get_tree().current_scene.scene_file_path
This line will return a string along the lines of “res://my_scene.tscn”. You could use an if statement to branch based on this information.
1 Like
can you put an example of how this would look in the code?
if get_tree().current_scene.scene_file_path == "res://main.tscn":
print("in main scene")
create an autoload (Project settings->globals) and keep track of the level by setting it when changing level, then have the mobs check there.
autoload level_manager.gd
var current_level : int = 0
in your mob:
if LevelManager.current_level == 0:
#DO STUFF
when level is changed:
LevelManager.current_level = 2 #or your level
how do i apply the last one to my code for the levels??
after you instantiate or change the level you change the current_level
.
you can use Enums to make it easier to remember, or just use Strings.
add_child(new_level)#assuming you are adding the new level to the tree
LevelManager.current_level = 3#give each level a different number and write it down
we just assign the new current_level.
sorry im still very confused. (im still kinda new to all this) heres my code for the autoload if it helps:
add_child(new_level)#assuming you are adding the new level to the tree
LevelManager.current_level = 3#give each level a different number and write it down
var current_level : int = 0
sorry if i completely misinterpreted your instructions
no, the autoload should be just this:
var current_level : int = 0
you can add more variables later for controlling other things. autoloads can be used to keep score or hold paths for instantiating scenes, like your levels. but for now all you need is an int (a variable that can hold a single number)
you go to project settings and create one and name it level_manager
.
you can access it from any script by the name LevelManager
, which will be given by godot.
This goes in the script of your mob, you access the autoload to check what level you are at when doing something:
and this goes on whatever script you are using to add a level or change it. this depends on what method you are using to change level, but whatever it is, you just add a line setting current_level
to a different number corresponding to the level you are swapping to.
if you are instantiating the level, there will be an add_child()
method being called, that’s probably where you put it in this case.
there are other methods that other people use but I don’t, so I’m not very familiar with them, such as change_scene_to_file()
. you also put the code there.
here’s more information:
1 Like
Oh ok that makes more sense!
ive added it but now when the player interacts with the mobs they freeze