Which line does Godot say is causing the issue? I don't see any class_name
in the script, so it's hard to guess right off.
I did make a few minor changes to the script though, like setting num
in _ready
to avoid it being null in _physics_process
. Here's the script:
using Godot;
using System;
public class swat3 : Spatial
{
private int num;
private AnimationPlayer anim_player;
private Timer timer;
private Random rng;
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
anim_player = GetNode("RootNode/AnimationPlayer") as AnimationPlayer;
timer = GetNode("RootNode/Timer") as Timer;
rng = new Random();
//timer.Connect("timeout", this, "_on_Timer_timeout");
// new - set num so it's not null on the first _physics_process call
num = rng.Next(1, 10);
}
public void _on_Timer_timeout()
{
num = rng.Next(1, 10);
(timer as Timer).Start();
}
public override void _PhysicsProcess(float delta)
{
// new - a tiny optimization, but with this it means we won't be trying to change the animation every _physics_process frame, and instead will only change it once the animation as finished. May or may not be needed, but it will prevent the code for repeatedly calling the play function every physics tick (30 times a second)
if (anim_player as AnimationPlayer).IsPlaying() == false) {
if (num == 1){
(anim_player as AnimationPlayer).Play("crouch to stand");
}
if (num == 2){
(anim_player as AnimationPlayer).Play("Idle");
}
if (num == 3){
(anim_player as AnimationPlayer).Play("mixamo.com");
}
if (num == 4){
(anim_player as AnimationPlayer).Play("run");
}
if (num == 5){
(anim_player as AnimationPlayer).Play("run_backwards");
}
if (num == 6){
(anim_player as AnimationPlayer).Play("stand to crouch");
}
if (num == 7){
(anim_player as AnimationPlayer).Play("strafe_left");
}
if (num == 8){
(anim_player as AnimationPlayer).Play("strafe_right");
}
if (num == 9){
(anim_player as AnimationPlayer).Play("walk");
}
if (num == 10){
(anim_player as AnimationPlayer).Play("walk_backward");
}
}
}
}