How can I make an enemy drop some powerup ?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Scavex

Hello guys I have made an enemy with this script :

extends KinematicBody2D
const GRAVITY =10
export(int) var SPEED =20
const FLOOR=Vector2(0,1)
var velocity=Vector2()

var direction=1

var is_dead=false
export(int) var hp=30


func _ready() -> void:
	pass # Replace with function body.

func dead():
	hp-=1
	if hp<=0:
		is_dead=true
		velocity=Vector2(0,0)
		$AnimatedSprite.play("Dead")
		$CollisionShape2D.disabled=true
		
		$Timer.start()


func _physics_process(delta):
	if is_dead==false:
		velocity.x=SPEED*direction*3
		if hp<=15:
			velocity.x=SPEED*direction*6
			
		if direction == 1:
			$AnimatedSprite.flip_h=false 
		else:
			$AnimatedSprite.flip_h=true
		$AnimatedSprite.play("Corona")
		velocity.y+=GRAVITY
		velocity=move_and_slide(velocity, FLOOR)
		
	if is_on_wall():
		direction=direction*-1
		$RayCast2D.position.x*=-1
		
	if $RayCast2D.is_colliding()==false:
		direction=direction*-1
		$RayCast2D.position.x*=-1
		
	if get_slide_count()>0:
			for i in range(get_slide_count()):
				if "Player" in get_slide_collision(i).collider.name:
					get_slide_collision(i).collider.dead()


func _on_Timer_timeout() -> void:
	queue_free()

And I have made this powerup allowing my player to jump higher for 5 times :

extends Area2D



func _ready() -> void:
	pass # Replace with function body.

func _on_Long_Jump_body_entered(body: Node) -> void:
	if "Player" in body.name:
		queue_free()

The code for the logic is this and is in Player Script :

func _on_Long_Jump_body_entered(body: Node) -> void:
		JUMP_HEIGHT=-600

My question is that how can I actually make the powerup appear only when my enemy is dead. For example my enemy is of type KinematicBody2D and powerup is an Area2D. How can I make that area2D appear only when KinematicBody2D is dead ?

:bust_in_silhouette: Reply From: nonchip

to begin with: don’t put the Powerup into the scene in the editor, since it’s not supposed to be there yet.

then, in your powerup, just below the extends line, add:

class_name LongJumpingPowerup

then the engine will know that this is a LongJumpingPowerup.

in your enemy’s dead() function you can then do:

# make a new instance of the powerup
var powerup = LongJumpingPowerup.instance()
# set its position to where the enemy is
powerup.position = position
# add it to the scene as a "sibling" object of the enemy (=next to it in the tree)
get_parent().add_child(powerup)

that’s pretty much it.

you should also look more into the gdscript documentation about classes and inheritance, e.g. i would make generic Enemy, Powerup, etc classes, put the general stuff in there, then extends those into the actual ones and just implement the differences.
then i would also try to not have the per-powerup logic inside the Player, but instead have the powerup just do the thing they’re doing, so you don’t have to look at/change a lot of files if you want to change one thing. generally you should try to keep all the logic to do with a thing inside that thing’s script.