(Solved) Get the total number of animations for a sprite?

Before proceeding, please use the forum search feature at the top of the page to check if your question has already been answered.

How do I…

I would like to choose a random animation for a given sprite. Is there a way to do this without knowing beforehand how many animations exist? Because I want to do this for multiple sprites, many of which might have 30-40 animations, I’d rather not always have to count each one individually and hard code it.

What is the expected result

something like:
this_sprite.current_animation = random.int(0, this_sprite.total_num_of_animations())

There are no expressions that get the total number of animations a sprite has, unfortunately. You would need to manually track this as an object variable or variable, and then you can do random(ObjectName.Variable(youranimationcountvariable))

Sounds good, thanks!

made this extension, hope it helps.
(copy it into a text file and import it as an extension)

{ "author": "", "category": "", "extensionNamespace": "", "fullName": "animation number", "helpPath": "", "iconUrl": "", "name": "GetAnimationNumber", "previewIconUrl": "", "shortDescription": "animation number", "version": "1.0.0", "description": "animation number", "tags": [], "authorIds": [], "dependencies": [], "eventsFunctions": [ { "description": "checks how many animations does an object have", "fullName": "object animation count", "functionType": "Expression", "name": "animation_count", "sentence": "", "events": [ { "type": "BuiltinCommonInstructions::Standard", "conditions": [], "actions": [ { "type": { "value": "ModVarObjet" }, "parameters": [ "object", "old_animation", "=", "object.Animation()" ] } ] }, { "type": "BuiltinCommonInstructions::Repeat", "repeatExpression": "99", "conditions": [], "actions": [ { "type": { "value": "ChangeAnimation" }, "parameters": [ "object", "+", "1" ] } ] }, { "type": "BuiltinCommonInstructions::Standard", "conditions": [], "actions": [ { "type": { "value": "SetReturnNumber" }, "parameters": [ "object.Animation()" ] } ] }, { "type": "BuiltinCommonInstructions::Standard", "conditions": [], "actions": [ { "type": { "value": "ChangeAnimation" }, "parameters": [ "object", "=", "object.Variable(old_animation)" ] } ] } ], "expressionType": { "type": "expression" }, "parameters": [ { "description": "sprite object to check for.", "name": "object", "supplementaryInformation": "Sprite", "type": "objectList" } ], "objectGroups": [] } ], "eventsBasedBehaviors": [], "eventsBasedObjects": [] }

4 Likes

Totally works! Thank you!

1 Like

Thanks a lot for this extension. It works very well up to 100 animation I assume. Keep that in mind. Not that I will have more.