I am making an enemy for a platformer Big Tower Tiny Square style and I am making an enemy that alternates between a small and large pulse. The speed of pulsing should be able to be modified with a Cooldown metadata, and it successfully changes the two timers that affect pulse rate and the timers successfully trigger the _on_inner_ring_timer_timeout() and _on_outer_ring_timer_timeout() functions, but if I try to modify the timer’s speed using metadata despite the metadata and timers accounting for the change it acts like an unmodified enemy.
I know my code is a mess, please excuse it because it works (except this…)
extends Node2D
var decayValue = 0.5
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if get_parent().get_meta("Cooldown") == 0.5:
pass
get_child(0).texture.fill_to = Vector2(0.5 - decayValue, 0.5 - decayValue)
decayValue = max(decayValue - 1.0/60.0, 1.0/60.0)
if name == "InnerRing":
get_child(1).get_child(0).shape.radius = 30 * decayValue
else:
get_child(1).get_child(0).shape.radius = 60 * decayValue
func decay() -> void:
decayValue = 0.5
func _on_inner_ring_timer_timeout() -> void:
if name == "InnerRing":
decay()
func _on_outer_ring_timer_timeout() -> void:
if name == "OuterRing":
decay()
Here is the tree (The enemy scene root name is blurred because this is a game for my friends containing their real names):
The code above is tied to both InnerRing and OuterRing. Ignore the warnings on the timers, they are unrelated to the issue. The script tied to the root sets the timers based on the metadata and is fully functional. How can I fix this?
What exactly is the issue? The only metadata call I see is this line, which is strange to compare floats by equality, but ultimately does nothing with pass
Can you better explain what you expect to happen versus what really happens? Are you sure this code is relevant, maybe the CharacterBody2D script is better?
I am 100% sure the CharacterBody2D script is fully functional and working as intended but
extends CharacterBody2D
func _ready() -> void:
print(get_meta("Cooldown"))
get_child(-1).get_child(0).start(get_meta("Cooldown"))
get_child(-1).get_child(1).start(get_meta("Cooldown") * 2.0)
print(str(get_meta("Cooldown")) + " inner ring time and outer rign is " + str(get_meta("Cooldown") * 2.0))
I expect for enemies with a Cooldown value different from the default (1) to pulse faster or slower, but they actually pulse just as if their cooldown was only 1, despite their timers clearly changing.
I’m guessing the “decayValue” is what’s mostly tied to your effect, which always reduces at the same rate regardless of metadata here:
I would try to avoid timers if you are using process anyways. Just guessing I’d rewrite the script like this, to use get_child less in process, and to remove timers.
extends Node2D
var decayValue: float = 0.5
var decay_cooldown: float
var sprite_texture: Gradient2D
var collision_shape: CircleShape2D
func _ready() -> void:
decay_cooldown = get_parent().get_meta("Cooldown", 1.0)
sprite_texture = get_child(0).texture
collision_shape = get_child(1).get_child(0).shape
func _process(delta: float) -> void:
sprite_texture.fill_to = Vector2(0.5 - decayValue, 0.5 - decayValue)
decayValue -= delta / decay_cooldown
if decayValue <= 0:
decayValue = 0.5
if name == "InnerRing":
collision_shape.radius = 30 * decayValue
else:
collision_shape.radius = 60 * decayValue