|
|
|
 |
Reply From: |
USBashka |
I can’t belive that enemy is a direct child of the player, so you can’t refer it like this. Imagine a scene tree as a folder structure and make relative path to the node. If player and enemy are siblings (direct childs of the same parent) you need to refer it as ../enemy
, where ..
is one step up in the tree structure. The code is:
onready var enemy = get_node("../enemy").global_position
Also, the enemy
variable type is Vector2
not Node
. So you need to write just enemy.x
, not enemy.position.x
Thank you for the help USbashika!
If I understand correctly, the code should be like this?
onready var enemy = get_node("res://Scenes/enemy.tscn").global_position
But another error started popping up. "Invalid get index ‘global_position’ (on base: ‘null instance’)
amaou310 | 2022-07-16 09:39
Noooooooooooo! Sorry, I meant imagine like it’s folder structure, it’s actually aren’t. get_node()
can get nodes only from loaded scenes, until scene is loaded and instanced it’s just a text file.
USBashka | 2022-07-16 09:49
Ohhh Im Sorry. I now understood that another scene node cannot get by “get_node”.
But how do you get another scene node instead of using get_node then?
amaou310 | 2022-07-16 10:01
With load()
and instance()
. But why you don’t just add enemy to the scene in the editor? Just drag enemy.tscn to the scene view
USBashka | 2022-07-16 10:05
Thank you again. Okay, so if I understand correctly, I can use get_node to call the enmy node, if I put enemy scene in the same scene as the player scene.
Like this:
Maine scene
>Level
>Player
>Enemy
>HUD
amaou310 | 2022-07-16 10:18
Yes. That’s how the scene tree works
USBashka | 2022-07-16 17:26
Okay. I found out why its not working. I was actually following this tutorial video and at around 38:00, he implement function ouch()
in body enter attached to enemy. The difference is that my player and enemy use area enter function. I first thought that I can just use area.ouch(position.x)
instead of body.ouch(position.x)
, however since function ouch()
does not exist inside function area entered
attached to player, the error “Nonexistent function ‘ouch’ in base ‘Area2D’” occurs. How should I rearrange this to make it work ?
Tutorial Script:
***#Player Script***
func ouch(ver enemyposx):
get_node("Position2D").set_modulate(Color(1, 0.3, 0.4, 1))
_velocity.y = -jump_strength
if position.x > enemyposx:
_velocity.x = -800
elif position.x < enemyposx:
_velocity.x = 800
Input.action_release("move_left")
Input.action_release("move_right")
***#Enemy Script***
func _enemy_body_entered(body):
body.ouch(position.x)
My Script:
***#Player Script***
func ouch(var enemyposx):
get_node("Position2D").set_modulate(Color(1, 0.3, 0.4, 1))
_velocity.y = -jump_strength
if position.x > enemyposx:
_velocity.x = -800
elif position.x < enemyposx:
_velocity.x = 800
Input.action_release("move_left")
Input.action_release("move_right")
func _on_hurtbox_area_entered(area):
area.ouch(position.x)
***#Enemy Script***
func _on_hitbox_area_entered(area):
pass
amaou310 | 2022-07-17 06:05