Why is my instantiation not working

Godot Version

4.3

Question

I have this in enemy.gd attached to enemy node

const HIT = preload("res://Assets/FX/hit.tscn")

in attack_action.gd which is a child of enemy node

func attack_fx()-> void:
	var new_attack := enemy.HIT.instantiate()
	enemy.add_child(new_attack)

however, new_attack ends up being null. I checked and the node has access to enemy script. Nothing else interacts with HIT aside from this instance. At enemy, Hit can be instantiated no problem. It also works in all previous versions of the project and there is no difference in the enemy or the attack script so I’m not sure exactly what is happening.

Okay, here is the weird thing. Duplicating Hit scene under a different name “test” and using it instead makes it work but then delete og Hit and changing test back to Hit makes it not work again. What exactly is the problem here?

Further testing:

  • instantiating both Test and Hit results in Test being instantiated and Hit being null
  • Deleting Hit from the project causes Test to stop loading and it faces the same problem. Even if test name remained TEST

What’s in the hit scene? When is the attack_fx function called?

I think instead of a const, it should be an @onready var (im not sure but try it)

1 Like

No, preload is run before @onready so there’s no need to make it an @onready variable. const is fine.

1 Like

In hit scene:

>animatedsprite
>>timer (it queue free the fx after one sec. I already tried disabling it)
>>area2d
>>>collisionshape2D

Basically the game is turn based, the attack_fx is called during enemy turn if enemy raycast is detecting area2D in front of it.

Right now, the fix seems to copy hit.tscn and put it under a different name. That way it works like before, but for some reason I can’t delete the original hit.tscn or even remove the reference from enemy.gd because it causes the duplicated hit.tscn to also stop working. I feel like maybe something is corrupted.

Edit: Another weird thing happening, if i deleted original hit.tscn and saved the project, it comes back at the project files

Edit2: After further testing, it seems that removing the script from hit.tscn allows it to instanitate normally, so nothing is corrupted?, but i’m not sure what exactly is the problem with it:

extends AnimatedSprite2D

var direction : Vector2 = Vector2.ZERO

func _process(delta: float) -> void:
	global_position += direction * 600 * delta



func _on_timer_timeout() -> void:
	queue_free() #already tried commenting this out


func _on_area_2d_area_entered(area: Area2D) -> void:
	area.owner.animation_player.play("Shake")
	area.owner.hit(direction)
	Shaker.shake(Globals.camera,4,0.2)

Edit3, commenting that last line in hit script solves it:

func _on_area_2d_area_entered(area: Area2D) -> void:
	area.owner.animation_player.play("Shake")
	area.owner.hit(direction)
	Shaker.shake(Globals.camera,4,0.2) #commenting this line out allows everything to run fine

I’m not sure why this happens though. Here is the autoload shaker

extends Node


func shake(thing , strength: float, duration: float = 0.2) -> void:
	if not thing:
		return

	var orig_pos = thing.position
	var shake_count := 10
	var tween := create_tween()
	
	for i in shake_count:
		var shake_offset := Vector2(randf_range(-1.0, 1.0), randf_range(-1.0, 1.0))
		var target = orig_pos + strength * shake_offset
		if i % 2 == 0: 
			target = orig_pos
		tween.tween_property(thing, "position", target, duration / float(shake_count))
		strength *= 0.75
	
	tween.finished.connect(func(): if thing: thing.position = orig_pos)

Edit4: replacing “Globals.camera” with anything allows it to work normally.

Shaker.shake(Globals.camera,4,0.2)

so now it’s

Shaker.shake(get_tree().get_first_node_in_group("camera"),4,0.2)

Problem solved. I still would like to know why it was not working though, especially that it was working before. Sorry for boring you guys with the edits :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.