4.5.1
i was trying to create health system but i dont know how reference health var that is in character body 2d script in area 2d script ( both are diffrent scenes ) i wanted to make if player enters area 2d health -= 1
4.5.1
i was trying to create health system but i dont know how reference health var that is in character body 2d script in area 2d script ( both are diffrent scenes ) i wanted to make if player enters area 2d health -= 1
When using areas, you usually connect the body_entered signal to a function like func _on_body_entered(body):. The body parameter is a reference to the PhysicsBody that entered the area, so you can use it to access the player.
func _on_body_entered(body):
body.health -= 1
Make sure that the area detects only the player (or check if body is the player), because the game would crash if the detected PhysicsBody doesn’t have a var health.
the problem is it says it detected colison2d but the code is a parent of colison2d so it dosent work do you know how to connect it?
Hello! Can you send a picture of your scene tree and maybe some code? It might be easier to help with those details.
Thanks
character body 2d scene
character body 2 d
└ collision shape 2d
extends CharacterBody2D
@export var health = 4
area 2d scene
area2d
extends Area2D
func _on_body_entered(body) → void:
body.health -= 1
i think thats all of whats important for this ( say if something else also plays role in this )
I am a total beginner so i have to apologize if I am giving you an answer that is way too beginner for you. You can make a reference to the script that is in another scene with a variable. so at the top you put @onready var my_area_2d_script, or whatever you want to call it, then in your code you can refer to it as my_area_2d_script.on_body_entered()
you can also connect a signal between scenes by declaring your other scenes variable the same way and then in the ready function you write my_area_2d_script.body_entered.connect(this is where you write the name of the function that does the thing you want to do)
is that what you are asking?
Did you get any error messages to share? I’m not quite getting what happened there.
The body_entered signal should always give a reference to a PhysicsBody (either CharacterBody, RigidBody or StaticBody) or a TileMap. So, body should be the CharacterBody2D of the player scene (if it is the player object that entered the area).
What do you mean by collision2d? CollisionObject2D, CollisionShape2D and KinematicCollision2D are very different things.
(And just to be sure, since you didn’t mention it for the area’s scene: Does your Area2D also have a CollisionShape2D as child with a set shape?)
okay nevermind it dosent work again and i get error Invalid access to property or key ‘health’ on a base object of type ‘StaticBody2D’. only thing i changed was resize something in other scene that just uses that area and thats only conection i dont know
This means the area detects something other than the player.
If the player has a unique physics layer, make sure the area’s mask is set to detect only that layer, and that no other objects use this layer.
If not, you could also use something like a class name to only affect the player:
class_name Player
extends CharacterBody2D
@export var health = 4
extends Area2D
func _on_body_entered(body) -> void:
if body is Player:
body.health -= 1