Spawning Location

Godot Version

4.3

Question

How can I make my emails spawn only within the visible area of the screen and not on the sides. Theyre spawning from the bottom.

var mailInstance = Mails.instantiate()
var mailLocation = %PathFollow2D

mailLocation.progress_ratio = randf()
mailInstance.position = mailLocation.position

add_child(mailInstance)

You should use global_position

They still spawn on the sides

Try

mailInstance.position = to_local(mailLocation.global_position)

If you still have issues, where is mailLocation relative to your parent node?

I usually handle it in the following order:

  • Instantiate the object
  • add it to the scene
  • set its position

Can I ask you to run the game and then check the Remote tab? I believe because you are adding the mailInstance to the SceneTree after setting the position, the position code is ignored.
If this is not the case, could you print out the position of the mailLocation.position?

  • I tried your suggestion, but the only spawn on the left.
  • mailLocation is positioned relative to its parent node using local coordinates.

When I print out the mailLocal.position I get:
Mail Location position: (229.4067, 480.648)
Mail Location position: (719.2686, 436.4833)
Mail Location position: (48.52057, 480.648)
Mail Location position: (722.0237, 443.8086)
Mail Location position: (163.1659, 433.0698)
Mail Location position: (722.0305, 469.337)
Mail Location position: (435.5422, 480.648)

They show when the mails spawn

Those positions you that printed seem a little random.

I was hoping for more details, is mailLocation a child of the parent? And where is mailLocation on the screen?

i would just add mail instance as a child to the location.

mailLocation.add_child(mailInstance)

This way you don’t have to manage the position manually. This is with the assumption that mailLocation isn’t intended to move.

Could you provide a screenshot of your scene tree? And describe in more detail?

Create a scroll container and place another container inside it, now you can place emails in the inner container to have their position adjusted automatically.

Screenshot 2024-08-28 144013
Here is the scene tree, then I have another Scene called ‘Mails’ that has the Animations

Right now I need you to verify if the mailLocation positions are correct.
While the game is running, check the latest value of %PathFollow2D and note down the position. Then you head into any mailInstance (that had been added as child) and then paste the position into it (while still inside the Remote tab).

Does it look correctly as it should if you do it that way?

Also are there any debugger errors logged?

Okay, that makes more sense.

So you have a path follow that is following the fish path. You want to drop pieces of mail along the path?

Since this script is part of the Game node when you add a child it will be a direct child of Game. This should be okay.

The fishpath will be placed on the screen somewhere. And i assume path follow is at zero local position. But will update with the path over time.

I think setting the global position like before:

mailInstance.position = to_local(mailLocation.global_position)

is okay. Maybe try @Locher suggestion of changing position after you add it as child. We probably need to check the mail scene. To make sure it’s all set to local position zero and doesn’t have a default offset internally.

I think I am losing you now, is it possible for you to assist me in discord maybe?

I used and it worked!

mailLocation.progress_ratio = randf_range(0, 0.3)

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