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?