How to connect 2 codes from a different scene

Godot Version

Godot 4

Question

I want to change the Process Mode from disabled to Inherit when I click a button from anoher scene like this

how can i do that

here are the script for the button
extends Node2D

func _on_button_pressed() → void:
get_tree().change_scene_to_file(“res://levels/game_level.tscn”)

The _process, _physics_process, _input, and _input_event functions will not be called. However signals still work and cause their connected function to run, even if that function’s script is attached to a node that is not currently being processed.

(From the documentation.)

So it seems your best bet is to use a signal. The button is part of the UI, right? In that case, the cleanest solution is probably to use a message bus.

Create a scene that inherits from Node and add the following script:

@ignore_warning("unused_signal")
signal change_processing_mode()

Save the scene as MessageBus and make it an autoload via the ‘project->project settings->global’ tab.

In your button script, add MessageBus.change_processing_mode.emit() to func _on_button_pressed()->void: (before the line of code where you switch scenes.)

In your LevelEditor node, add this as the first line of code of your _ready method: MessageBus.change_processing_mode.connect(change_mode.bind()).

Then add this method to your LevelEditor:

func change_mode()->void:
	process_mode = Node.PROCESS_MODE_INHERIT
2 Likes

thanks for the help , I try your solution there’s no error but the editor is still disabled

button script
extends Node2D

func _on_button_pressed() → void:
Signals.change_processing_mode.emit()
get_tree().change_scene_to_file(“res://levels/game_level.tscn”)

signals scirpt(global) // I change the name from MessageBus
extends Node2D

signal IncrementScore(incr: int)
signal IncrementCombo()
signal ResetCombo()
signal CreateFallingKey(button_name: String)
signal KeyListenerPress(button_name: String, array_num: int)
signal change_processing_mode()

level_editor script
extends Node2D

@export var in_edit_mode: bool = false
var current_level_name = “RHYTHM_HELL”

var fk_fall_time: float = 2.2
var fk_output_arr = [, , , ]

var level_info = {
“RHYTHM_HELL” = {
“fk_times”: “[[-0.95208616256714, 0.77582769393921, 2.57841281890869]]”,
“music”: load(“res://music/linga guli guli.wav”)
}
}

func _ready():
Signals.change_processing_mode.connect(change_mode.bind())
$MusicPlayer.stream = level_info.get(current_level_name).get(“music”)
$MusicPlayer.play()

if in_edit_mode:
	Signals.KeyListenerPress.connect(KeyListenerPress)
else:
	var fk_times = level_info.get(current_level_name).get("fk_times")
	var fk_times_arr = str_to_var(fk_times)
	
	var counter: int = 0
	for key in fk_times_arr:
		
		var button_name: String = ""
		match counter:
			0:
				button_name = "button_Q"
			1:
				button_name = "button_W"
			2:
				button_name = "button_E"
			3:
				button_name = "button_R"
		
		for delay in key:
			SpawnFallingKey(button_name, delay)
		
		counter += 1

func KeyListenerPress(button_name: String, array_num: int):
fk_output_arr[array_num].append($MusicPlayer.get_playback_position() - fk_fall_time)

func change_mode()->void:
process_mode = Node.PROCESS_MODE_INHERIT
print(“jalan”)

func SpawnFallingKey(button_name: String, delay: float):
if get_process_mode() == Node2D.PROCESS_MODE_INHERIT:
await get_tree().create_timer(delay).timeout
Signals.CreateFallingKey.emit(button_name)

func _on_music_player_finished():
print(fk_output_arr)
get_tree().change_scene_to_file(“res://levels/win_condition.tscn”)

func toggle_node_state(active: bool):
self.set_process(active) # Menghentikan atau melanjutkan logika _process
self.set_visible(active) # Menampilkan atau menyembunyikan node

so the point of my question is
game play scene

i want to make when the button from the before scene to this scene while enabling/ Inherit the level_editor so when this game scene start the level_editor is active/inherit

I see the problem now.

Signals.change_processing_mode.emit()
get_tree().change_scene_to_file(“res://levels/game_level.tscn”)

The signal is emitted before the scene that listens to it has been added to the tree, therefore the scene cannot respond to it at all. And after change_scene_to_file is called the button disappears, so relocating the emit() to after that line of code does nothing.

The quick and dirty way to solve this is to make an autoload that saves the current process mode. Instead of emitting the signal in button, change the process mode in the autoload. Then in LevelEditor’s _ready() method, access autoload and get the process mode.

There are cleaner ways of doing this.