Variables not updating - no idea why

Godot Version

4.2.1

Question

Hey, so i have this :

pos_frozen and velocity_frozen arent updating

func _process(delta):

#vars used in throwing parabola 
	var b : float
	var a : float
	var d : float
	var max_val : Vector2 # local maximum for the path2d curve 
	var end_point : Vector2

	var velocity_frozen : float
	var pos_frozen : Vector2

	#if !is_jumping: #update relevant variables when note jumping (jump vars shouldnt change while jumping)
		#curve.rotation = rotation
		#curve.global_position = global_position

	print(pos_frozen)

	if is_jumping:
		print("is jumping") #is working
		a = (-9.82) / 2 * pow(velocity_frozen / 50,2) * pow(cos(deg_to_rad(50)), 2)
		curve.global_position = pos_frozen
	else:
		print("is not jumping") #is working
		a = (-9.82) / 2 * pow(velocity.length() / 50,2) * pow(cos(deg_to_rad(50)), 2)
		curve.rotation = rotation #rotate to follow the player
		curve.global_position = global_position #move the actual curve
		pos_frozen = curve.position #update pos_frozen - not updating
		velocity_frozen = velocity.length() # update velocity frozen - not updating

	b = tan(deg_to_rad(50))
	d = pow(b , 2)
	end_point = Vector2(((-b-pow(d,0.5))/2*a),0) # formula for intersection with the x axis (this is the end point for the curve)

		#curve rotates with the player this makes sure the player is jumping "up" (y value in max_val is always negative)
	if abs(rotation) < 0.785: #facing right
		max_val = Vector2((-b/2*a), -  (-d/4*a)) 
	else: #facing left
		max_val = Vector2((-b/2*a),  (-d/4*a)) 




	if abs(rotation) > 2.61 or abs(rotation) < 0.52 or is_jumping: #floats are equal to 30 degrees deviation from 0 and 180 in both directions
		curve.curve.set_point_position(0,Vector2.ZERO ) # set point position is always relative to the player
		curve.curve.set_point_position(1,max_val)
		curve.curve.set_point_position(2,end_point) 


		curve.curve.set_point_in(1, Vector2(-velocity.length()/10,0))
		curve.curve.set_point_out(1, Vector2(velocity.length()/10,0))
	else:
		curve.curve.set_point_position(1,Vector2.ZERO)
		curve.curve.set_point_position(2,Vector2.ZERO)

This is the only code where I change the curve

But both pos_frozen and velocity_frozen arent updating at all. both of the prints are working but pos frozen returns (0,0) and velocity returns 0
pos_frozen (vector2) and velocity_frozen (float) are used when the player starts jumping to “freeze” the curve stop it from updating
i have no idea why

curve.position and velocity.length returns actual values

You are declaring pos_frozen and velocity_frozen every frame, which is wiping out your previous values.

The first print(pos_frozen) would be 0,0 since you just recently re-declared it 2 lines prior.

4 Likes

Have you tried putting the velocity_frozen and pos_frozen outside the process function?
Like:

var b : float
var a : float
var d : float
var max_val : Vector2 # local maximum for the path2d curve 
var end_point : Vector2

var velocity_frozen : float
var pos_frozen : Vector2
func _process(delta):
    #rest of code
2 Likes

… Thank you!
The dumb thing is that this isnt the first time this has happened for me :frowning:
Thanks to @GameDevGeezer and @Frozen_Fried helping

I did at frozen_fried suggested and this fixed the issue

2 Likes

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