Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | kuroski |
Hello everyone,
I’m working on my first game and trying to implement a generic system that would allow me to define the monster waves of my game + apply custom properties in case I need them.
For example:
- Wave 1
- 4 Knight enemies
- Wave 2
- 4 Knight enemies
- 1 Archer enemy
- Wave 3
- 4 Knight enemies + They all have 1 extra health point
- 1 Boss
But I’m still struggling to make things work, primarily by my lack of understanding of how Resources
and how the language works.
The idea is to have a ResourceStash
I can pre-load all my enemy’s scenes + export an enum
which would be used by a Resource
# ResourceStash.gd
extends Node
enum ENEMIES {KNIGHT}
const EnemiesScenes = {
KNIGHT = preload("res://Enemies/Knight.tscn")
}
func load_enemy_scene_for(enemy: ENEMIES):
match enemy:
ENEMIES.KNIGHT:
return EnemiesScenes.KNIGHT
Then I have a EnemyStats
resource:
# EnemyStats.gs
extends Resource
class_name EnemyStats
signal enemy_died
@export var max_speed: int
@export var max_health: int
@export var enemy: ResourceStash.ENEMIES
var health: set = set_health
# Here, the values are not being properly set
func _init(max_speed = 15, max_health = 1):
max_speed = max_speed
max_health = max_health
health = max_health
func set_health(value):
health = clamp(value, 0, max_health)
if health == 0:
enemy_died.emit()
And finally, I can define my “generic” Resource that would allow me to build my waves dynamically:
## Waves.gs
extends Resource
class_name Waves
@export var waves: Array[Wave]
#########
## Wave.gs
extends Resource
class_name Wave
@export var level: String
# Maybe later I could use a dictionary to provide more metadata about the waves
@export var enemies: Array[EnemyStats]
func _init(level = 0, enemies = []):
level = level
enemies = enemies
# .....
func _on_has_waved_cleared():
pass
#########
## WaveSpawner.gd
extends Node2D
var Waves = preload("res://Waves/Waves.tres")
@onready var Spawner = $Spawner
func _ready():
for enemy in Waves.waves[0].enemies:
var enemy_scene = ResourceStash.load_enemy_scene_for(enemy.enemy)
var main = get_tree().current_scene
var instance = enemy_scene.instantiate()
instance.stats = enemy
main.add_child(instance, true)
instance.global_position = position
await get_tree().create_timer(randf_range(0.5, 1.5)).timeout
But I still do not understand:
- Why is my
WaveSpawner
when I doinstance.stats = enemy
does not start my instance with the custom data - Does this approach seems reasonable? + Am I doing something wrong?
Thank you so much for your attention
Could you please explain what a “wave spammer” is?
Ertain | 2023-02-20 21:27
Hello Ertain,
Sorry for not providing details + I was also using the wrong word… I meant an “Enemy Spawner”.
I tried to add details to the description.
But in general, I’m building a game similar to
Norman the Necromancer | js13kGames
The idea is to have defined a resource that would allow me to specify my enemy waves.
For example:
- Wave 1
- 4 Knight enemies
- Wave 2
- 4 Knight enemies
- 1 Archer enemy
- Wave 3
- 4 Knight enemies + They all have 1 extra health point
- 1 Boss
Then based on that config I can create a class that would spam that + takes if the wave is done or not.
kuroski | 2023-02-21 05:20