Why am I getting the "null instance" on a null instance error in this situation.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Spafnar

Hello, so I recently spent a very long time putting together a shop for my game, but I realized it was much harder to load the sprites from the store to being what the player played as because they aren’t stationary icons, but rather animated sprites. Even for the first skin just being a color palette change, but all the same animations, I’m still having a lot of trouble figuring it out. Anyways this is the script I currently have for trying to have it load the new “skin.” Instead of putting a texture variable for an icon path, I have it set as “$AnimatedSprite.set_animation(“red right, red up”)” The relevant script looks like this:

var textures = [
$AnimatedSprite.set_animation(“red right, red up”)
]

func _ready():
screen_size = get_viewport_rect().size
Global.load_store()
$AnimatedSprite.textures = load(textures[Global.store.Selected])
hide()

The script above is from the Player.gd, and the parent node is “Player”, followed by “AnimatedSprite”, and “CollisionShape2D” as the children. The error described in the title occurs when I tried to make the “textures” variable. I’ve also tried $Player/AnimatedSprite, as well as “set_animations” but neither make the error go away. Any tips or am I completely backwards with how I’m trying to do this?

:bust_in_silhouette: Reply From: kidscancode

You’re getting this error because $AnimatedSprite, aka get_node("AnimatedSprite"), can’t work before the node is in the tree and ready. Set the animation in _ready() when you know the child node will exist and be ready.

Rule of thumb: You can’t access nodes before _ready().

Secondly, set_animation() doesn’t return anything, so why are you trying to do it in an array?

(Fair warning, I’m rather new ) Hm, so are you saying I don’t need the var “textures” at all? Also thanks for clarifying the error for me. I used set_animation simply because I didn’t know how to work any other alternative. I thought I could substitute an icon’s path with set_animation, and instead of having to make a separate player scene for every single character I planned on adding to the shop, I could make the respective animated sprites in the original player script, and just have it load the “new character”

Spafnar | 2020-12-30 07:15

I actually wonder where you got set_animation() from at all. If you look in the Inspector for the AnimatedSprite, you’ll see that animation is a property. That means it’s like a variable. To change or use it, you just use its name.

$AnimatedSprite.animation = "up"

It’s really not that clear what you’re trying to do, but here’s how an AnimatedSprite works:

In the frames property is a SpriteFrames resource. This resource contains the animations that the sprite can play. Each animation has a name, so you can choose the one you want by setting animation.

Inside the SpriteFrames object are all the individual images that make up the animations. Since it can contain multiple animations, you shouldn’t need to be loading anything - you can set them all up to begin with. You can create a new SpriteFrames library by choosing “New SpriteFrames” when you click the frames property in the Inspector.

The official tutorial uses an AnimatedSprite, and is a good example of how to work with one: Your first game — Godot Engine (3.2) documentation in English

You can reference all of this here:

AnimatedSprite — Godot Engine (3.2) documentation in English

SpriteFrames — Godot Engine (3.2) documentation in English

kidscancode | 2020-12-30 18:28

And if there were two animations, each with their own set of frames, it would then be $AnimatedSprite.animation = “animation1, animation2” ? I guess I’m confused on how to change a character’s entire animation set for all directions and assign it to the respective button in shop. I already drew the spriteframes and arranged them correctly, as well as put them in the Player.

Spafnar | 2021-01-01 09:42

An AnimatedSprite can only play one animation at a time, so no, you can’t set animation like that.

To change the set of animations, you’d need to make separate SpriteFrames resources and set them with the frames property.

kidscancode | 2021-01-01 18:54