Godot Version
4.2
Question
I’m trying to program a little squash and stretch for when my character on screen turns, and I’m getting a very strange error
extends Node2D
@export var verticalSquash : float
@export var horizontalSquash : float
@export var squashSpeed : float
var facingRight : bool
var currentScale: Vector2
# Called when the node enters the scene tree for the first time.
func _ready():
facingRight = true
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
print(facingRight)
currentScale.y = 1
if facingRight :
currentScale.x = 1
if !facingRight :
currentScale.x = -1
func squashRight():
facingRight = true
func squashLeft():
facingRight = false
currently I have it simplified down because I was trying to troubleshoot. The idea is when the parent script moves it determines if it’s going left or right and triggers the squashLeft or squashRight function in it’s child. as far as I can tell this is working correctly. In this script however, if facing right is true it stays true the only time. But if it should be false, it swaps between true and false every frame the whole time until its meant to change to true again. I’ve put prints in every part of this script and its parent and none of my scripts that are changing the bool are triggering every frame.
I don’t see anything in the code snippet you’ve provided that could produce what you’re experiencing. The error must be happening somewhere else.
Until you provide more code snippets from your project, there’s no telling what is causing your issue.
1 Like
func ChoosePointToMoveTo():
quadrantIndex += 1
if(quadrantIndex>7) :
quadrantIndex =0
if quadrantIndex == 0 || quadrantIndex == 1:
pointToMoveTo.x = randf_range(TankBoundaries.topLeftCornerQ1.x, TankBoundaries.bottomRightCornerQ1.x)
pointToMoveTo.y = randf_range(TankBoundaries.topLeftCornerQ1.y, TankBoundaries.bottomRightCornerQ1.y)
if quadrantIndex == 2 || quadrantIndex == 3 :
pointToMoveTo.x = randf_range(TankBoundaries.topLeftCornerQ2.x, TankBoundaries.bottomRightCornerQ2.x)
pointToMoveTo.y = randf_range(TankBoundaries.topLeftCornerQ2.y, TankBoundaries.bottomRightCornerQ2.y)
if quadrantIndex == 4 || quadrantIndex == 5:
pointToMoveTo.x = randf_range(TankBoundaries.topLeftCornerQ3.x, TankBoundaries.bottomRightCornerQ3.x)
pointToMoveTo.y = randf_range(TankBoundaries.topLeftCornerQ3.y, TankBoundaries.bottomRightCornerQ3.y)
if quadrantIndex == 6 || quadrantIndex == 7:
pointToMoveTo.x = randf_range(TankBoundaries.topLeftCornerQ4.x, TankBoundaries.bottomRightCornerQ4.x)
pointToMoveTo.y = randf_range(TankBoundaries.topLeftCornerQ4.y, TankBoundaries.bottomRightCornerQ4.y)
if pointToMoveTo.x > global_position.x:
squashPoint.squashRight()
if pointToMoveTo.x < global_position.x:
squashPoint.squashLeft()
This is the section that picks a point to move to and also tells the squash point how to squash. I checked it and it’s only triggering when it’s supposed to
Do you use currentScale
at all?
You might wish to use a group of “ifs” like that, but most of time it will produce a logic error. if, elif, is mainly what you want to use. You only use more than one if, if you want the change to be made in the same loop. Not saying that’s the problem, just good practice.