I put a Call Method track on my main idle animation in order to transition to other idle animations after a certain amount of time, but it's not working for some reason. Code is:


var stateMachine;
var idleCounter: int = 0;
    
func _ready():
	stateMachine = $AnimationTree.get("parameters/playback");
	print(idleCounter);

func idleLoop():
	idleCounter += 1;
	if idleCounter >= 11:
		stateMachine.travel("IdleFlourish");
		idleCounter = 0;

If the idleCounter variable has a value assigned to it, it'll stay on that value without updating while if declared as an int, it'll give an error, "Invalid operands 'Nil' and 'int' in operator '+'." Not really sure what's happening here.

10 days later

Bump since I'm still struggling with this one. I made a test project with just the above code and nothing else. Everything seemed to work fine there, so I'm wondering if there's something else I have that's causing it, but I'm not sure what. Also, I'm open to other suggestions on how to do different idle flourishes if there's an easier method.

2 months later

Printing from _ready will only send the value once the game starts. Also, if you are using gdscript, you shouldn't need the ; at the end of lines since it is coded similarly to python :)

I am not super versed in the animation stuff sadly, but one thing I noticed is that the idleLoop doesn't loop. (Unless you are calling it from the _process(delta) method or another method altogether in which case it most likely will.) But another way you could write it is like so: (Keep in mind that count, IdleCounter in your code, starts at 0, so if your idleCounter is supposed to be 11, it will have 12 frame to count before executing the if's part and not 11.)

func idler(duration = 0): # Feel free to rename how you'd like. The 0 is only there as a default value, so you can change it. This way you can simply call the function if you want to instantly trigger the idleFlourish.
	var completed = 0 # you can change it to true or false if you prefer using a boolean when checking
	for count in range(duration):
		print(count)
		if count >= duration: 
			print(count) # Just so you can see if it actually increased the count variable before executing here. Feel free to comment or remove that print if it does increase. Same with the previous print. This way you don't clutter your debug console
			stateMachine.travel("IdleFlourish")
			completed = 1
	return completed # This way if you use print(idler()), or need to make sure it actually executed, it should return 1 which would indicate everything went through without any crash-worthy problem.

Then when you call your function from elsewhere, you can simply call idler(11) and use the return value as a way to know when the forloop is done this way you don't send the function every frame depending on where you use it.

Hopefully this helps a little. In any case I am wishing you the absolute best and a wonderful day!

a year later