Im a nowbie and attempted to write this code whith chatgpt and other tutorials. I want to make door opening when the box falls on the button, but its doesnt work! whats wrong with my door code?
extends Node2D
var item: Node = null
funk _ready() → void:
# Find the cube in the scene. Let’s assume that he is in the same scene.
item = get_node(“.”) # Specify the correct path to the cube
if item:
item.connect(“cube_full_on_button”, Callable(self, “_on_cube_fell_on_button”))
else:
print(“Item node not found”)
func _on_cube_fell_on_button() → void:
# The code for opening the door
is open_door()
func open_door() → void:
# The logic of opening the door (for example, moving)
position.y -= 100 # Example of moving the door down
print(“Door opened”) # For debugging
Chatgpt always writes the codes of Godot 3, it does not have knowledge on Godot 4. So you need to correct those mistakes by yourself when you are using any codes from Chatgpt.
Make sure to paste your scripts with proper formatting
Your current script is getting self in a round about slow way, the comment here suggests you should fill it with a different path,
item = get_node(".") # Specify the correct path to the cube
but I would recommend using @export instead. With @export you must select the node from the inspector, this variable will appear as a property like any other.
extends Node2D
@export var Item: Node2D
Except you only want a signal from this item yes? Does your box have a signal “cube_full_on_button”? If so you can connect it through the editor in the “Node” tab next to the “Inspector”, if you do not have that signal then you will need to make it.
Then you can connect the signal “cube_full_on_button” directly to your open_door function on this Node.
But so much of this script has been wrong, I’m willing to bet it’s been a misleading starting point.
Bonus ChatGPT cringe, please never code with this program:
item is in class body though only used locally in _ready()
func not funk _ready()
Old 3.x connection as KingGD pointed out
uneccesary intermediate function _on_cube_fell_on_button()
is open_door() syntax error, remove is
position.y -= 100 actually moves the self node up in 2D
All together I think you would be better off with a new script, an Area2D, and connecting to it’s body_entered signal
extends Area2D
@export var door: Node2D
func _on_body_entered(body: Node2D) -> void:
if body.name == "Box":
door.position.y += 100
It looks like there are a few issues in your code. First, the function definition should be func _ready() instead of funk _ready(). Also, in your item.connect() call, make sure the signal “cube_full_on_button” is properly emitted from the cube. Lastly, in the open_door() function, is should be removed; it should be just open_door() to call the function correctly. I was feeling completely stuck while working on my thesis. The research part was overwhelming, and I needed guidance. That’s when I decided to try this academized.com service, and it was a game-changer. They provided expert assistance that made the process so much smoother. My thesis was well-written, with all my requirements met. It was the boost I needed to complete my degree successfully.