• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Help to get started with spine?

Nicky

Member
Hello,

I'm new to game maker 2 and coding in general. I'm trying to slowly learn how to use GM2, and i'd like to use Spine to make the animations for my characters, and playing with the possibility of changing the skins of the characters while keeping the animations.
I'm using spine 3.7,94 as i've read that it was the supported version for game maker.
I have an issue when trying to test an animation of a sprite in an empty room. I've imported the .json sprite (with the .atlas in the same directory), created an object with this sprite, and dragged it into the room. If i just do that, the game starts alright, but nothing appears. Just black screen. I've tried draw_self without success, i've read about skeleton_animation_set(), but when i enter the name of the animation in the brackets, the game gets an error for not finding this name anywhere in the game.
Saddly, all the tutorials i can find online are for previous versions of either game maker 2 or spine.
Do you know how i could fix both the sprite not appearing in game and the animation's name being unrecognized? Is there an obvious code line that i don't know about?

I'm probably using all the wrong words i'm sorry, i'm very much a newbie and not a native english speaker.
Thank you for any help, i'd love to make some progress!
 

rIKmAN

Member
Hello,

I'm new to game maker 2 and coding in general. I'm trying to slowly learn how to use GM2, and i'd like to use Spine to make the animations for my characters, and playing with the possibility of changing the skins of the characters while keeping the animations.
I'm using spine 3.7,94 as i've read that it was the supported version for game maker.
I have an issue when trying to test an animation of a sprite in an empty room. I've imported the .json sprite (with the .atlas in the same directory), created an object with this sprite, and dragged it into the room. If i just do that, the game starts alright, but nothing appears. Just black screen. I've tried draw_self without success, i've read about skeleton_animation_set(), but when i enter the name of the animation in the brackets, the game gets an error for not finding this name anywhere in the game.
Saddly, all the tutorials i can find online are for previous versions of either game maker 2 or spine.
Do you know how i could fix both the sprite not appearing in game and the animation's name being unrecognized? Is there an obvious code line that i don't know about?

I'm probably using all the wrong words i'm sorry, i'm very much a newbie and not a native english speaker.
Thank you for any help, i'd love to make some progress!
Is the sprite showing correctly when you drag the object into the room editor?
Is the root bone position of the skeleton (in Spine) set at 0,0?

For the animation name, you should be using the animation as you named it inside Spine as a string: skeleton_animation_set("animation_name");

So if the error is saying that it cannot find that animation name then I would guess you are missing the quotes, as even if the animation name itself is wrong it would not give an error it would just silently fail and not play the animation.
 

Nicky

Member
Oh thanks a lot! The quotes solved the error!
But yeah, the sprite is showing in the room when i drag it, and not when the game starts. I have some simple small sprites on the same instance layer that are working just fine.
 

rIKmAN

Member
Oh thanks a lot! The quotes solved the error!
But yeah, the sprite is showing in the room when i drag it, and not when the game starts. I have some simple small sprites on the same instance layer that are working just fine.
Is the root bone position of the skeleton (in Spine) set at 0,0?
Does the animation you are setting it to have visible slots (in Spine) and also check that the object is set to visible in the object properties inside GMS2.

If you draw the x/y of the instance to the screen using draw_text(), what is it reporting?
Remember that if you have code in the Draw Event you will need to manually add draw_self() to make sure it is drawn.

Are you changing the x/y position anywhere that could be causing it to be off screen?
Are you sure that it isn't being hidden by something on a layer above it?
 

Nicky

Member
Damn, thank you again! I'm not sure exactly what solved it, setting the root bone at 0,0 and adding draw_self (Was i putting that in the wrong place?) seem to have worked! I'm so relieved, thanks a LOT!
Now the animation is not running, but if i keep asking questions i'll not do anything truly myself i guess ^^' As a last one, if i may, do you have a recommandation of a place/book to look at to learn that kind of things? Or is that gathered knowledge after years of trial and errors?
(Edit: ok the animation runs! Progress made! Thank you SO much!)
 
Last edited:

rIKmAN

Member
Damn, thank you again! I'm not sure exactly what solved it, setting the root bone at 0,0 and adding draw_self (Was i putting that in the wrong place?) seem to have worked! I'm so relieved, thanks a LOT!
Now the animation is not running, but if i keep asking questions i'll not do anything truly myself i guess ^^' As a last one, if i may, do you have a recommandation of a place/book to look at to learn that kind of things? Or is that gathered knowledge after years of trial and errors?
The "root" bone is Spine is what will become the sprite origin inside GMS2 - that is that if you draw the Spine sprite at 100,100 then that is where the "root" bone will be in terms of the skeleton being positioned on screen.

Sounds like you maybe had things offset from inside Spine and it was drawing off screen due to this (hard to say without seeing it) but it could also have been a missing draw_self() as if you had your own Draw Event and didn't add that code then the skeleton would not be drawn. If you don't have a Draw Event then GMS will automatically draw the sprite for you.

For the animation not running, to test it you can set it in the Create Event using skeleton_animation_set("animation_name"); making sure that the spelling and case of the animation name matches exactly with what you have called it inside Spine.

Also make sure that if you have changed something in Spine that you remember to re-export the atlas and json and then re-import those into GMS otherwise you are working on old files. It's a simple thing to forget and something I still do myself every now and then even to this day.

For learning - not sure if you mean Spine or GML - but yeah lots of trial and error (especially in the GMS1.4 days) to find out why things weren't working as expected with various skeletons and animations imported from Spine into GMS, and if you meant GML then yeah just time served in other languages for years so GML wasn't too hard to pick up as I already knew how to code and wasn't starting from a blank slate - I was just learning new syntax and the "GameMaker way" of how to do things I already knew.

As a beginner I'd recommend the manual as a reference for checking how things work and what each function does.
There are some courses on GML Fundamentals by SamSpade which will help with general programming logic and the new YYG tutorial series Little Town is supposed to be quite good so you could follow that too.

Just make sure you try and understand what you are doing and why, play with the code, change it and see if the change you expected is actually what happened and if not, why not etc. Getting your hands dirty is the best way to learn, making mistakes is expected and a great way to learn what NOT to do - just don't copy/paste code blindly without investigating it and trying to understand what it's doing because if you do that you will never learn anything - and when it breaks you will have zero idea on how to fix it.

Good luck!
 
Last edited:

Nicky

Member
That's awesome, thank you so much for your time, and these recommandation! I have a lot to learn, but i'm so happy to see my character moving on screen :) Cheers!
 

Nicky

Member
Hello again!

I’m still trying to figure out the basics with spine and game maker 2.

I want to have a default animation, but when pressing a button, update the animation to another one (here from idle to test).

In the create event of the animated sprite, i used skeleton_animation_set(« idle ») and created the variable animation = 1.
In the button object, i made so that when i click on it, it adds 1 to animation, cycling between 1 and 5 for now.
In the animation update event of the animated sprite, i made if animation = 1, then set the animation to idle, and if animation = 2, set the animation to test.

I’ve tried different things, and managed at least to have something happen when i click the button, but ran into some issue and don’t understand why it is happening.

I’ve had a thing when i tried cycling between 1 and 5 animations, but only setting animation = 1 to idle and animation = 2 to test. In this case, the result was when the game launch and animation = 1, the idle animation is frozen, when animation =2, the test animation is frozen (i can see the sprite change slightly), and the test animation is only played when animation = 3 or above. It get frozen again when i cycle back to animation = 1 or 2.

I tried with cycling between only 2 animation, but the result is that both are frozen. I see something changing when i press the button, but the animation doesn’t play. I thought earlier that my mistake was putting the animation update code in the step event, but now I feel that the animation update event is the right place to change the animations after a variable.

Does someone have an idea of what i could do wrong?

Thank you for your time!
 

rIKmAN

Member
Hello again!

I’m still trying to figure out the basics with spine and game maker 2.

I want to have a default animation, but when pressing a button, update the animation to another one (here from idle to test).

In the create event of the animated sprite, i used skeleton_animation_set(« idle ») and created the variable animation = 1.
In the button object, i made so that when i click on it, it adds 1 to animation, cycling between 1 and 5 for now.
In the animation update event of the animated sprite, i made if animation = 1, then set the animation to idle, and if animation = 2, set the animation to test.

I’ve tried different things, and managed at least to have something happen when i click the button, but ran into some issue and don’t understand why it is happening.

I’ve had a thing when i tried cycling between 1 and 5 animations, but only setting animation = 1 to idle and animation = 2 to test. In this case, the result was when the game launch and animation = 1, the idle animation is frozen, when animation =2, the test animation is frozen (i can see the sprite change slightly), and the test animation is only played when animation = 3 or above. It get frozen again when i cycle back to animation = 1 or 2.

I tried with cycling between only 2 animation, but the result is that both are frozen. I see something changing when i press the button, but the animation doesn’t play. I thought earlier that my mistake was putting the animation update code in the step event, but now I feel that the animation update event is the right place to change the animations after a variable.

Does someone have an idea of what i could do wrong?

Thank you for your time!
It sounds like you are setting the animation every frame, which means it constantly gets reset to the beginning over and over and will appear frozen.
It is actually playing, but you just see the first frame repeatedly due to the resetting.

If you want the animations to play and then switch to the other when it's finished then you can use the Animation End Event - this event only fires once the current animation has ended. So in here check what animation has just ended with skeleton_animation_get() and start the other one.

If you want to change the animation when you click a button then you should only set the animation inside a block of code that fires when the button is clicked (and not every frame as explained above).
 

Nicky

Member
Ah thank you again! I changed it so the step event of the button triggers a change in the animated object when clicked, and the animations actually play! Progress!
Now the issue is that it doesn't cycle between the different animations. It changes once, and clicking the button again starts the new animation again. I thought that by assigning a variable to each animation, and changing this variable through the button, i would change the animation each time i click the button, but i'm not there yet. (Coding is hard!)
 

rIKmAN

Member
Ah thank you again! I changed it so the step event of the button triggers a change in the animated object when clicked, and the animations actually play! Progress!
Now the issue is that it doesn't cycle between the different animations. It changes once, and clicking the button again starts the new animation again. I thought that by assigning a variable to each animation, and changing this variable through the button, i would change the animation each time i click the button, but i'm not there yet. (Coding is hard!)
What are you storing in the variables?
skeleton_animation_set() expects a string which is the name of the animation as you named it inside Spine, so those variables need to be strings which match the animation names.

When you detect your button being pressed, check which animation is currently playing and then play the other one:
GML:
// if the current animation is 'running'
if(skeleton_animation_get() == "running")
{
    // change it to jumping
    skeleton_animation_set("jumping");
}
else // else the current animation isn't 'running' - which means it must be 'jumping'
{
    // so set it to running
    skeleton_animation_set("running");
}
If you have more animations you'd better using a switch statement, but for the sake of a simple example with 2 animations as you mentioned then using an if/else is fine in this situation.

If you are still struggling then post the code you are using and what events they are in so we can see what's going on.
No, coding isn't easy but just like anything - the more you do it the easier it gets!
 

Nicky

Member
Thanks a lot! I'll look into the switch statement, i'm using two animations for now but i'd like to use more in the future!
I've tried if else in the mean time and i have the same issue of the animations not cycling back to the first one.
In theory, to bypass the switch statment in an unelegant way, can i make a long list of if statments?
Like
if variable = 1, then animation = 1,
if varaible = 2, then animation = 2,
if variable = n, then animation = n?
Would that be supposed to work?
Really, thank you for your help!
 

Nicky

Member
I am the dumbest of them all. I forgot a capital letter in the name of the first animation!! Weirdly it was launching when the animation was drawn, but it would not update correctly!
I have now set the switch statement and set the update to wait until the end of the animation, so that's a big progress! :D Thank you again so much for your help!
 
Top