This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

I would like to make sure that all 2D images positional coordinates in the Godot UI and during animation are constrained to only whole number values.

I am new to GDScript, and I suspect it would need to be done this way. Anyone willing to point me in the right direction to figure this out?

I am suspecting I need to apply a round() fucntion on something?

As always greatly appreciated. If I could group hug all of Godot I would <3

in Engine by (89 points)
recategorized by

UPDATE 1: The following doesn't work but this is what I currently have...

# root node for scene
extends Control

# learned somewhere that using set_fixed_process will execute things every frame
func _ready():

# set the current position to a whole number instead of a float. not sure if I need delta.
func _fixed_process(delta):

    set_pos(int(round(get_pos())) * delta)

Have you tried enabling 2d pixel snap in project settings?

Thank you for your suggestion. I have already set the 2D pixel snap, and it works fine.

My goal is to make it so animation and positional data in Godot don't use floating point values. For example, normally when making an object move from left to right starting from position 1, the motion would proceed to increment to 1.1, 1.2, 1.3, etc. I want to change the behavior to so the object will move from left to right such that the values will increment from 1 to 2, to 3, and so on. No floating point numbers at all.

I know I probably need to use round() in GDScript, but I am unfamilar with how to get the position of a sprite and change it in such a way that all animation interpolates in whole number integer values.

Regarding in-GUI 2D snapping while mouse dragging a sprite, it doesn't seem to work. I definitely have it enabled. Is the expected pixel snapping supposed to disable the possibility of floating point placement? Do I need to hold down a key or something to use it?

Regarding rounding off the floating point values during every frame of animation, I whipped up something that seems to work. I need to test in more deeply though...

extends AnimatedSprite

# get position of the sprite 
onready var sprite = get_node(".")
onready var xy = sprite.get_pos()

# enable every frame processing
func _ready():

# round off and typecast any floating point values into integers
func _fixed_process(delta):
    sprite.set_pos(Vector2(int(round(xy[0])), int(round(xy[1]))))

Sorry, the option in game settings is for the renderer. There is also a use pixel snap option for the scene editor. It completely slipped my mind!

Nailed it! Thanks very much!

In case anyone has the same question, it also solved my problems related to perfect pixel collision using KinematicBody2D / TileMaps:

onready var player = get_node(".")
func _process(delta):
    var xy = player.position
    player.position = (Vector2(int(round(xy[0])), int(round(xy[1]))))

thank you!

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.