AlexGabriel Posted December 29, 2016 Share Posted December 29, 2016 Hello, As many other users around, i am new to LUA and Leadwerks even if i use it since 1 year ago. I mostly play with his functions... no big deal. Yesterday i have spent over 12 hours using it and i have managed to do some interesting things. While going 'advanced', i have tried to add a basic shield model to my character using FindChild and SetParent functions. It seems that if i attach the shield to self.entity while setting his position on the left hand bone, the shield will follow the character. If i attach it directly to the left hand bone, the shield position is the same but doesn't follow the character not even remains attached to the bone while doing anims. Here is a short video explaining better the situation. Codes : Start() function Script:Start() --other stuff self.shields={} self.shields.shield1 = Model:Load("Models/Shields/shield1.mdl") --other stuff end UpdateWorld() function Script:UpdateWorld() --other functions if window:KeyHit(Key.Enter) then self:AddShield() end --other functions end AddShield() function Script:AddShield() self.leftHandShield = self.entity:FindChild("mixamorig:LeftHand") self.shields.shield1:SetPosition(self.leftHandShield:GetPosition(true),true) self.shields.shield1:SetParent(self.leftHandShield[0])--noticed that there are more bones !? System:Print("SHield Position: "..self.shields.shield1:GetPosition(true):ToString()); end There is anything i can do to have this working ? Thanks Quote Link to comment Share on other sites More sharing options...
macklebee Posted December 29, 2016 Share Posted December 29, 2016 Without seeing the model in question to try myself, I am just going to guess that it has something to do with these two lines: self.leftHandShield = self.entity:FindChild("mixamorig:LeftHand") ... self.shields.shield1:SetParent(self.leftHandShield[0]) The variable 'self.leftHandShield' is not equal to the 'self.leftHandShield[0]' variable. So it appears you are setting the parent to nil which would explain why it does not follow. For future reference, the '[#]' denotes an item in an arrayed/tabled variable. So just make the line this and it should work: self.shields.shield1:SetParent(self.leftHandShield) Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
AlexGabriel Posted December 29, 2016 Author Share Posted December 29, 2016 Thank you for info, i have already tried to set parent as self.leftHandShield without success. I have added some extra prints to make sure that parent is set and it seems that everything should work. The issue persists, if i set parent as self.entity everything is ok but the shield follows the main body, if i set to self.leftHandShield i can't see it. Child AddShield() function Script:AddShield() self.leftHandShield = self.entity:FindChild("mixamorig:LeftHand") self.bonePosition = self.leftHandShield:GetPosition(true) self.shields.shield1:SetPosition(self.bonePosition,true) self.shields.shield1:SetParent(self.leftHandShield,true) if self.shields.shield1:GetParent() ~= nil then System:Print("Shield Position: "..self.shields.shield1:GetPosition(true):ToString()); System:Print("Shield current Parent: "..tostring(self.shields.shield1:GetParent()).." Set "..tostring(self.leftHandShield)) else System:Print("Shield has been set to an invalid bone"); end end Output : SHield Position: 6.364595, 1.808138, -0.224094 Shield current Parent: userdata: 0x002e5ef8 Set userdata: 0x002e5ef8 Maybe i am missing something. Quote Link to comment Share on other sites More sharing options...
macklebee Posted December 29, 2016 Share Posted December 29, 2016 Just looking at your updated code, nothing is jumping out as the issue since you resolved the 'nil' variable. Would likely need to see the character itself to test. You do not by chance have either of these models (character or shield) scaled to something other than (1,1,1)? It would not be the first time I have seen parenting issues due to extreme scaling problems. --Edit: just tried your code in the first post with some small edits for removing the nil variable and loading my own models, and I noticed that the "shield" is getting loaded when the scene starts - not when you press the "Enter" key. Is that what you wanted? In any case, the above code in general works as I applied it to a crawler model with a cagelight as the shield attached to the left hand bone. My guess is that would need to see the models in question to determine the issue. Scaling is suspect. Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
AlexGabriel Posted December 29, 2016 Author Share Posted December 29, 2016 Character : http://steamcommunity.com/sharedfiles/filedetails/?id=614465844 The shield was downloaded from tf3dm without any textures ( i've made some red ones ). Anyway, i have checked now the scaling and yes, that shield was scaled at 100 CM because was HUGE. I have replaced the shield with Models/Pipes/pipe_flange.mdl ( default model with leadwerks ) which is scaled at 1,1,1 and i have the same result, can't see the 'shield'. --EDIT: I have tested with crawler, it is working perfectly. It is also working on the other character, i have noticed that i didn't attached to a bone but to a node or something like that. I have attached it to a finger and is working. The only issue now is that the "shield" is rotating around the character's finger but i guess that this is another lesson to learn Thank you very much for your help macklebee, i've just made another step from those 1M steps i have to make to create my Skyrim clone Quote Link to comment Share on other sites More sharing options...
macklebee Posted December 29, 2016 Share Posted December 29, 2016 It is working for me. EDIT- This character does have a small scale applied to it (0.011,0.011,0.011) which in the past this would have caused numerous issues when parenting. But it looks like Josh changed the global parameter for the SetParent() command to only affect orientation instead of the child inheriting the parent's matrix (like it was done in LE2). I believe this is a recent change because i remember distinctly having scaling issues due to the parent's scaling this past year. Nope- still changes the scaling if the global parameter is set to false. The child's scale will be multiplied by the parent's scale. The information in the API Reference is incorrect as it implies only the rotation/position would be affected. 1 Quote Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590 LE / 3DWS / BMX / Hexagon macklebee's channel Link to comment Share on other sites More sharing options...
AlexGabriel Posted December 29, 2016 Author Share Posted December 29, 2016 Working perfectly now, thank you very much again. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.