Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Rev |
Is there anyway for me to load animations from a .tres file to a SpriteFrames? I have rummaged everywhere for answer and this link is the closest to what I want to find:
https://forum.godotengine.org/8654/change-spriteframes-loaded-animatedsprite-using-gdscript
Though, it’s only applied to gdscript:
var frames = preload("path/to/your_sprite_frames.tres") animated_sprite.set_sprite_frames(frames)
I have referred to the docs, and see that preload
returns an object of type Resource, so I tried something similar in C++:
void Fighter::_init()
{
_Anim_Sprite=get_node<AnimatedSprite>("AnimatedSprite2D");
auto Res=godot::Resource();
Res.set_path("res://assets/anims/human/human_anim.tres");
//_Anim_Sprite->set_sprite_frames(Res);
_Anim_Sprite->set_sprite_frames(Ref<SpriteFrames>(&Res));
}
But it fails. Playing the scene on Godot would crash it right away.
That’s all I want to ask. Thank you in advance <3
To begin with, you’re calling get_node()
from Fighter::_init()
which is a kind of ctor and probably will fail, since node haven’t been added to a tree yet, nor its children.
You should use _ready()
Playing the scene on Godot would crash it right away.
Use debugger, or at least Godot::print() / print_warning()
to trace down a problem, then you will know more than just “Godot crashes”.
sash-rc | 2020-10-09 12:06
Yeah, after many hours of trials and errors, i finally come to that conclusion. Thank you for your piece of advice about print() and print_warning(). They are indeed useful.
Rev | 2020-10-21 15:46