|
|
|
|
Reply From: |
Zylann |
When you define a variable in a script attached to a node, you can write node.the_property
to get or set it. So you just have to get the player node from the enemy node and modify its health.
However, I would implement shooting behaviour a different way:
Have a player.tscn
scene, projectile.tscn
scene and enemy.tscn
scene. Each of them have a script.
First, define a take_damage(amount)
function in the player script.
Next, add a var damage
inside projectile scripts (if you have such an object).
When an enemy wants to shoot, it will do something like this:
var projectile = preload("projectile.tscn").instance()
projectile.damage = self.damage
world.add_child(projectile)
projectile.fire_at(target)
Projectiles will want to detect collisions so they know when to damage something, so it will handle a signal sent by the physics engine.
When the projectile collisions with an object, it will do this:
func on_body_enter(body):
# This assumes the player is a physics body (kinematic, rigid or static)
if body.has_method("take_damage"):
body.take_damage(self.damage)
As an example, you can have a look at the 2D platformer demo, in which you can shoot bullets at enemies.
Thanks for answering me …you solution is excellent
after reading Documentation and GDScript section , I use more simple approach and use setget functionality in GDScript
in Projectile Script :
var damage = setget set_damage , get_damage
func _ready():
pass
func set_damage(value):
damage = value
func get_damage():
return damage
now , When I create projectile node in enemy script , i Just set a value for damage
In Eneymy script :
export(int) var projectile_damage = 2
var projectile = preload("projectile.tsn")
# other codes hear !!!
func fire():
projectile_instance = projectile.instance()
projectile_instance.set_damage(projectile_damage )
# other codes are here
world.add_child(projectile_instance)
now , in player_node , I can access to damage field :
func _on_body_enter(body):
var damage = body.get_damage()
player_health -= damage
if player_health <= 0 :
print (" You are Dead!")
I thinks we should use setget keyword for this kind of situation .
RezaPouya | 2016-07-05 08:05
Looks fine too, althought I don’t see a reason to use setget in the projectile. Unless it is linked to something else, like projectile size, so setting damage would make the projectile bigger, things like that
Zylann | 2016-07-05 18:58
now that look at your solution , it is better and it is more suitable for complicated games … my game is simple game without too much collision , so handling damage in player node/script is more logical …
RezaPouya | 2016-07-05 21:38