[4.3] Static Function not calculating?

Hello there!

I had a bit of code on a particular script that I needed to use again for a different purpose, so I thought I’d try making it a custom class/static function.

I took this:

var targetBody : CharacterBody3D

var previousRotation : float
var targetRotation : float
var turnAmount : float

func _physics_process(_delta: float) -> void:
	
	if previousRotation != targetRotation :
		turnAmount = previousRotation - targetRotation
		previousRotation = targetRotation

	targetRotation = targetBody.rotation.y

And made it into a class/static function thingy

extends Node

class_name HelperFunctions

static func GetRotationDifference (targetBody: CharacterBody3D) -> float:
	var previousRotation : float
	var targetRotation : float
	var output : float

	if previousRotation != targetRotation :
		output = previousRotation - targetRotation
		previousRotation = targetRotation
	
	targetRotation = targetBody.rotation.y
	
	return output

…And then call it in the previous script like this:

var targetBody : CharacterBody3D

func _physics_process(_delta: float) -> void:
	turnAmount = HelperFunctions.GetRotationDifference(targetBody)

I put a debug Print in GetRotationDifference(), and it’s being called as it should, but it isn’t actually calculating anything- the output variable is just 0.

Does this sort of thing just not work with this method? They can’t update their variables without them being stored elsewhere or some such thing, perhaps?

previousrotation and targetrotation do not have a value, since you instance them with no value. these variables also have to be static if you want them to work that way

1 Like

Thanks for replying!

So the parts in the body of the code where they’re having values assigned aren’t working in this case?

EDIT

Oh, are they being re-created every frame when the Function is called again?

And if they’re made static, that would mean that I wouldn’t be able to use the function in several places simultaneously?

They are instantiated every time you call the method and yes if they are static you can only “remember” one place simoultaneously

1 Like

Darn, as I feared.

Thanks for helping me out!

1 Like

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