This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 = getviewportrect().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?

Godot version 3.2.3
in Engine by (55 points)

1 Answer

+1 vote

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?

by (22,191 points)

(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 setanimation simply because I didn't know how to work any other alternative. I thought I could substitute an icon's path with setanimation, 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"

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: https://docs.godotengine.org/en/3.2/getting_started/step_by_step/your_first_game.html

You can reference all of this here:

https://docs.godotengine.org/en/3.2/classes/class_animatedsprite.html

https://docs.godotengine.org/en/3.2/classes/class_spriteframes.html#class-spriteframes

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.