Storing a dynamic value to check against itself.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Teethree89

Alrighty, so I have been trying to figure this one out for a minute now, and no combination of modifiers seems to be doing the trick. In a nutshell I am trying to get a variable that stores value A, then value A is added to, and subtracted from to get back to value A, this is for UI scaling interaction. The issue I am having is when I store value A as target_scale = target.scale.y, when used in the following code, the value increases as it is increased so it never subtracts, unless is use target = int(target.scale.y) but that cuts off any decimals that I might need to keep the scale the same from start to finish.

Below is the code snippet, it’s a real head scratcher for me!

_process(_delta: float) -> void:
if scaling==1:
	if mtarget==1:
		target = $Sprite2/AnimatedSprite
	if mtarget==2:
		target = $Sprite2/Sprite
	var target_scale = int(target.scale.y) #right here is the issue, this works but not really
	if target.scale.y<target_scale+1:
		if scale_set==0:
		if target.scale.y>=target_scale+1:
	if scale_set==1:
		if target.scale.y>target_scale:
		if target.scale.y<target_scale:
		if target.scale.y==target_scale:
:bust_in_silhouette: Reply From: timothybrentwood

Well first off float(target.scale.y) will give you the number with the decimals (a floating point number opposed to an integer 1,2,3,…).

I’m confused as to exactly what you’re trying to accomplish with your code though? A scale factor is generally thought of as being multiplied by another value to ‘scale’ it up or down. For example: 100 with a scale factor of 2 would be 200 = 100 * 2. If you wanted a 25 percent increase in size your scale factor would be 1 (the original size) + 0.25 = 1.25 so something that is 25% bigger than 100 is: 100 * 1.25 = 125. Similarly if you wanted a 40% decrease in size you would do 1 - 0.40 = 0.6 so 100 * 0.6 = 60.

If you’re getting this value from a user and you want to guarantee it’s in an acceptable range use the clamp() function:

var user_input = 0.6
var scale_factor = clamp(user_input, MINIMUM_SCALE_FACTOR, MAXIMUM_SCALE_FACTOR)
# scale_factor is now 1 because 0.6 < MINIMUM_SCALE_FACTOR = 1
target.scale.y = scale_factor

If you’re giving the user an option to choose a scale factor between like 0-5, simply convert that scale factor before using it. This code will increase the scale by 25% for every digit over 0:

var user_input = 3
var scale_factor = 1 + (0.25 * user_input)
# scale_factor is now 1.75
target.scale.y = scale_factor

I tried float earlier and it had the same result, basically the code checks for the scaling variable to be 1, if so it runs the scaling boolean operation, the function is shared by multiple instances in the hud from child nodes of the parent script, their scripts send their sprite path as target to this function, which needs to record the initial scale value to match against the rest of the operation, which is, if the current scale is less than the original scale+1, add 0.25 until it is equal or more than, then switch direction until it is equal to the original scale, and exit the function/make the scaling variable 0 to stop the operation.

In the hud, potion sprites, herb sprites, and others, once clicked, run this operation as a visual UI queue.

In writing this I thought to myself, why not just send the known scale from the child script to this function as well, which would make life easier, but add to the computations a touch. In the end, I opted for making the scales whole numbers and int() works just fine, the issue I was having initially was that int() was rounding up/down or just deleting the decimals from the scale if it was like 3.6 or 2.22 etc, so the final scale was smaller than the original.

Using float made the current scale update along with the original scale, so the UI elements just grew into infinity.

Teethree89 | 2021-04-29 02:23

I think Tweens are going to give you the effect you desire:

func scale_up_down(sprite):
	var tween =
	var original_scale = sprite.scale
	var max_scale = 5
	var duration = 1
	tween.interpolate_property(sprite, "scale",
        original_scale, Vector2(original_scale.x, max_scale), duration,
        Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)
	yield(tween, "tween_completed")
	tween.interpolate_property(sprite, "scale",
        Vector2(original_scale.x, max_scale), original_scale, duration,
        Tween.TRANS_LINEAR, Tween.EASE_IN_OUT)

The above code will increase the scale.y of an input sprite until it reaches max_scale over duration seconds then scale it back down to its original scale over the same duration.

timothybrentwood | 2021-04-29 19:41