Spawning at (0, 0) Before Moving to Intended Spawn Position

Godot Version

4.3

Question

Everything seems to spawn in the top left before moving to the position I want to spawn it in. Let’s take projectiles as an example.

Simplified code below. We call _cast_throwable, which in turn calls _create_projectile, and then hits the Factory to create the relevant Projectile class and add it to the tree.

# combat_active.gd
## create ProjectileThrowable and activate
func _cast_throwable() -> void:
	if _cast_position is Marker2D:
		var projectile: ProjectileThrowable = _create_projectile(
			_cast_position.global_position,
			_effect_chain.on_hit
		)
		projectile.set_target_actor(target_actor)
		projectile.activate()
		_restart_cooldown()
	else:
		push_error("CombatActive: `_cast_position` not defined.")

## foundational method for creation of a projectile, used by calling specific `cast_*` method, e.g. `_cast_throwable`. 
func _create_projectile(cast_position: Vector2, on_hit_callable: Callable) -> ABCProjectile:
	var projectile: ABCProjectile = Factory.create_projectile(
		_projectile_name,
		_allegiance.team,
		cast_position,
		on_hit_callable
	)
	return projectile

# factory.gd
## create a projectile based on data in the library, then run setup when that projectile is ready.
func create_projectile(
	projectile_name: String,
	team: Constants.TEAM,
	spawn_pos: Vector2,
	on_hit_callable: Variant = null
	) -> ABCProjectile:
	# get base info
	var dict_data: Dictionary = Library.get_projectile_data(projectile_name)
	var data_class: DataProjectile = DataProjectile.new()
	data_class.define([...])

	# get specific subclass
	var projectile: ABCProjectile
	match dict_data["effect_delivery_method"]:
		Constants.EFFECT_DELIVERY_METHOD.throwable:
			# finish setting up data class
			data_class.define_throwable([...])

			projectile = _PROJECTILE_THROWABLE.instantiate() as ProjectileThrowable

			if on_hit_callable != null:
				projectile.hit_valid_target.connect(on_hit_callable)

	# create and setup instance
	projectile.ready.connect(projectile.setup.bind(spawn_pos, data_class), CONNECT_ONE_SHOT)
	get_tree().get_root().add_child(projectile)

	return projectile

The problem appeared in this commit, and was not apparent in the previous commit. I’ve reviewed the diffs and I can’t see anything that would cause this issue! It’s the same overall approach and most of the same code.

Example:
godot_JEBXVor72r

Any help would be greatly appreciated!
Snayff

I think your

projectile.ready.connect(projectile.setup.bind(spawn_pos, data_class), CONNECT_ONE_SHOT)

line might be the problem since set the projectiles position on its ready signal. This might be delayed (after frame one of its existence). If you want to make sure the position is set correctly from its first frame on you should be able to set it before you instantiate your projectile:

...

projectile.ready.connect(projectile.setup.bind(data_class), CONNECT_ONE_SHOT)
projectile.global_position = spawn_pos
get_tree().get_root().add_child(projectile)


return projectile

Thank you for the suggestion! Doing it that way seems to work - thank you!

I have no clue what changed between those commits, as I was still going it in the way shown above, even when it was previously working. :person_shrugging:

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