Custom function not working

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

Ive been trying to create a “spring” function that does a springy tween from one vector to another. The function ive tried to make, does absolutely nothing. Im very new to creating custom functions so the problem could be simple, or could be completely out of the ball park.

code:

func _process(delta):
	spring(parent.rotation,camera.global_transform.basis.get_euler(),0.5,0.1)
	

func spring(f:Vector3,t:Vector3,s:int,w:int):
	var sprspd:Vector3
	sprspd = lerp(sprspd,(t-f)*s,w)
	f+=sprspd
	
:bust_in_silhouette: Reply From: wyattb

“In GDScript, only base types (int, float, string and the vector types) are passed by value to functions (value is copied). Everything else (instances, arrays, dictionaries, etc) is passed as reference.”

So in your case parent.rotation is passed by value and does not affect the parents rotation. Maybe you should pass just parent and use f.rotation inside the function instead of just f.

OR just return the value f and set parent.rotation in the calling function. E.g.

func _process(delta):
    parent.rotation =spring(parent.rotation,camera.global_transform.basis.get_euler(),0.5,0.1)
    

func spring(f:Vector3,t:Vector3,s:int,w:int):
    var sprspd:Vector3
    sprspd = lerp(sprspd,(t-f)*s,w)
    return f + sprspd
    

I cant seem to understand why its not working

This works:

    extends Spatial


var sprspd  = Vector3()



func _process(delta):
	var from = global_transform.origin
	var to = Vector3(0.0,0.0,0.0)
	var spring = 0.5
	var weight = 0.1
	
	sprspd = lerp(sprspd,(to-from)*spring,weight)
	from+=sprspd
	
	global_transform.origin=from

But doing it this way, i have to create a unique sprspd variable for each thing i want to tween. I cant figure out how to turn that into a custom function for easy reuse.

dari0us | 2021-06-02 09:31

You need to re-read my post above. Did you try it?

wyattb | 2021-06-02 12:43

tried it your way, it did the same thing as mine did, nothing was moving.

dari0us | 2021-06-02 12:44

ok this works. I’ll let you figure out the what your problem is :slight_smile: In all honesty this is a Godot editor problem for not catching it.

extends Spatial

var sprspd  = Vector3()
func _process(delta):

	var from = global_transform.origin
	var to = Vector3(0.0,0.0,0.0)
	var spring = 0.5
	var weight = 0.1	
	global_transform.origin=spring(from,to,spring,weight)

func spring(f:Vector3,t:Vector3,s:float,w:float):
	sprspd = lerp(sprspd,(t-f)*s,w)
	return f + sprspd

wyattb | 2021-06-02 15:59

Alright, that definitely works, big thanks for your help. there’s only one last minor issue of having to add a unique “sprspd” variable at the top of the script for every tween, any thought or ideas on how to tackle this?

dari0us | 2021-06-02 18:53

Depends how many tweens you planning to store and how you are going to use them. An array is probably the way you want to go.

wyattb | 2021-06-02 19:43

Thanks for all your help, i really apreciate it.

dari0us | 2021-06-02 20:49