Skip to main content

On Sale: GamesAssetsToolsTabletopComics
Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Setting twisted_bone bone weights from gdscript

A topic by aros2 created Feb 07, 2024 Views: 202 Replies: 8
Viewing posts 1 to 8
(1 edit)

Hello,

I'd like to apply twistedbone weights to polygon2d from GDscript but am having problems.

If i paint the weights in the editor, the Polygon2D's bones property will be array that looks like this: [twistedbone1,[1,1,1,1,1,1], twistedbone2,[0,0,0,0,0]] and I thought it would be trivial to set in code. But when I try to do it, weird things occur. In fact, this happens:


var bones_array = [skeleton.get_child(0), weights]

polygon.bones = bones_array

print(bones_array)

Result:

[Twisted_Bone2D:[Bone2D:24122], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

print(polygon.bones)

Result:

[[Bone2D:24122], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

Then when I open the weight paint screen, the right hand column that shows the bones only displays [Bone2D, instead of the usual TwistedBone_1.  Weights appear set,  but when i rotate the twistedbone in the editor, the polygon doesnt move with it.

So, to summarize, it seems like the TwistedBone, when set into the polygon.bones array, is being turned into [Bone2D]. Is there any way around this? I'd be fine with just a hack to get it to work.

Cheers

Developer

Maybe try the set function? I suspect trying to set the property from GDScript could be causing the issue, so maybe doing something like set(“bones”, bone_array) instead of directly modifying the property would fix it since then it would go through Godot’s object setter system. I know that’s how you can set C# properties via GDScript so I’m hoping that will work here too.

Developer (1 edit)

Also, I don’t think you set the bone weight on the Bone2D itself but instead set it on the polygon2d. The Bone2D class doesn’t have a bones property, but the Polygon2D class does and I think modifying it should fix it.

Unless you are already doing that, in which case please disregard this!

Thanks for the reply, Twigleg!

I am indeed setting on the polygon. I tried polygon.set('bones', bones_array), but the exact same result occurs. In GDScript it's likely that polygon.bones = is just a convenience syntax for set.

This is the result of printing polygon.bones after doing polygon.set('bones', bones_array):

[[Bone2D:479641], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]


Any other suggestions? Much appreciated

(1 edit)


This is the erroneous result

This is what the result should look like (from when I do it manually)

Developer(+1)

Hmm.. maybe try clear_bones and https://docs.godotengine.org/en/stable/classes/class_polygon2d.html#class-polygon2d-method-add-bone? (I can’t add another link for some reason…)

That would be strange if that fixed it though, as I suspect it just sets the bone weight under the hood. The only thing I can think of that might fix it in this case would be setting the node again via the node path might bind it to the TwistedIK2 bone rather than the Bone2D node.

(+1)

okay -- this does work! because add_bone uses a path so it must be doing it internally

polygon.clear_bones()

polygon.add_bone(bone.get_path(), weights)

Thank you so much!

Developer

Great! I’m glad that worked and am happy to help 🙂