Hi Nate
I've managed to find/determine the bones under a parent bone (see code below - not sure if it's the best way).
QUESTION: I'm not sure now (can find the write functions) how to get for each bone the slots & attachments under them? Which Lua file/method would I need to use here. I note in the JSON export there is a slot section that does seem to get for Slot/Attachment/Bone combinations but trying to find in the Corona LUA API how to get this.
That is to ultimately then be able to do the required "skeleton:setAttachment("slotNameX", "attachmentNameX")" for each slot/attachment for each of the bones identified.
Getting all Bones Hierarchically Under the Parent (including the Parent)
local function getChildBoneNames(parentBoneName)
local childBoneNames = {}
for i,boneData in ipairs(skeletonData.bones) do
if boneData.parent then
if boneData.parent.name == parentBoneName then
table.insert(childBoneNames, boneData.name)
end
end
end
return childBoneNames
end
local function getChildBoneNamesRecursive(parentBoneName, includeParent)
local allBoneNames = {}
if includeParent then
table.insert(allBoneNames, parentBoneName)
end
local nextChildBoneNames = getChildBoneNames(parentBoneName)
if nextChildBoneNames then
for i,childBoneName in ipairs(nextChildBoneNames) do
local grandchildrenBoneNames = getChildBoneNamesRecursive(childBoneName, true)
for i,boneName in ipairs(grandchildrenBoneNames) do
table.insert(allBoneNames, boneName)
end
end
end
return allBoneNames
end
local childrenBones = getChildBoneNamesRecursive("capeBone1", true)
pretty.dump(childrenBones)
Update:
Ok - think I have it - this seems to work 🙂 If anyone happens to use/optimize I'd be interested is seeing:
local function getChildBoneNames(parentBoneName)
local childBoneNames = {}
for i,boneData in ipairs(skeletonData.bones) do
if boneData.parent then
if boneData.parent.name == parentBoneName then
table.insert(childBoneNames, boneData.name)
end
end
end
return childBoneNames
end
local function getChildBoneNamesRecursive(parentBoneName, includeParent)
local allBoneNames = {}
if includeParent then
table.insert(allBoneNames, parentBoneName)
end
local nextChildBoneNames = getChildBoneNames(parentBoneName)
if nextChildBoneNames then
for i,childBoneName in ipairs(nextChildBoneNames) do
local grandchildrenBoneNames = getChildBoneNamesRecursive(childBoneName, true)
for i,boneName in ipairs(grandchildrenBoneNames) do
table.insert(allBoneNames, boneName)
end
end
end
return allBoneNames
end
local function getBoneSlotAttachmentTable()
local outputTable = {}
local slots = skeleton.slots
for i,slot in ipairs(slots) do
outputTable[slot.data.boneData.name] = { slotName = slot.data.name, attachmentName = slot.data.attachmentName}
end
return outputTable
end
local function getBSAforParentBone(parentBoneName)
local outputTable = {}
local boneSlotAttachments = getBoneSlotAttachmentTable()
local childrenBones = getChildBoneNamesRecursive(parentBoneName, true)
for i,boneName in ipairs(childrenBones) do
local slotAttachment = boneSlotAttachments[boneName]
if slotAttachment then
outputTable[boneName] = slotAttachment
end
end
return outputTable
end
local function setPowerUp(parentBoneName, turnOn)
local BSA = getBSAforParentBone(parentBoneName)
for k,v in pairs(BSA) do
local attachmentNameValue = turnOn and v.attachmentName or nil
skeleton:setAttachment(v.slotName, attachmentNameValue)
end
return BSA
end
---
Testing
timer.performWithDelay(2000, function(event)
setPowerUp("capeBone1", false)
end)
timer.performWithDelay(4000, function(event)
setPowerUp("capeBone1", true)
end)