I want to spawn pipes for my flappybird game, but when i change the resolution, it's weird

Godot Version

Godot 4.0

Question

Is there any way to fix this? when I change the resolution of the game, the pipe position just not in the middle anymore, here’s my window settings and a video about the bug

PlayerScreen.gd

extends Node2D

@export var pipe_scene: PackedScene

var time: float = 0

# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	time += delta
	if time > 1:
		print(DisplayServer.window_get_size())
		var pipe = pipe_scene.instantiate()
		pipe.global_position = get_viewport().size / 2
		add_child(pipe)
		time = 0.

pipe.gd

extends CharacterBody2D

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")


func _physics_process(delta):
	position.x -= 100 * delta
	move_and_slide()

You can configure how the window behaves in the project settings.

Open the Project Settings
Go to Display → Window
And change the Stretch mode there.

I tried, but it didn’t work, pipe’s position isn’t in the middle of the screen when I change the window size

Aloa fptisthebest,

the code you provided is not the issue.

I used the following Minimal Project and everything works accordingly, as you can see here:
ChangingResolution
or on GH.

I would assume you are doing some math somewhere wrong but as always precise help can only provided with a minimal project of your own.

Keep in mind the Circle objects are never destroyed in my code, hence your are leaking.

Kind regards,
KowalewskajA

1 Like

In your video, the circles sometimes stack or space really far to each others, just like mine, i don’t know how to fix that

Aloa fptisthebest,

you asked:

“Is there any way to fix this? when I change the resolution of the game, the pipe position just not in the middle anymore, here’s my window settings and a video about the bug”

The minimal code example spawns the new objects in the middle and keeps the position width ratio of the existing circles. Hence question answered and problem solved.

I definately recommend you read the docs about multiple resolutions. Maybe that can help you and answer the additional questions you have.

Whenever you change the resolution of a running game, you should stop drawing until you finished what you have to do with the new resolution. In singleplayer game you would pause the game do whatever you need to do and than let the gameloop continue.

EDIT: Some other thoughts that might be worth thinking about:

  • resolution depending speed
  • as you use sprites → scaling

Kind regards,
KowalewskajA

2 Likes

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