How to change SpriteFrames from AnimatedSprite in GDNative?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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

:bust_in_silhouette: Reply From: Rev

I rummaged on the internet and stumbled upon a github repository that has all the nativescript coded in C++. Big thanks to that guy:

So, the correct way to load any resource in C++ is by using:

godot::ResourceLoader::get_singleton()->load("your path to resource file here");