Jump to content

Entity:SetParent issue or bad code


AlexGabriel
 Share

Recommended Posts

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

Link to comment
Share on other sites

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)

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

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

2d58ed431b91469b913ddf8ce07cd08a.png

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.

Link to comment
Share on other sites

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.

post-14-0-58594300-1483012353_thumb.jpg

 

My guess is that would need to see the models in question to determine the issue. Scaling is suspect.

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

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 :)

Link to comment
Share on other sites

It is working for me.

post-14-0-21314000-1483013353_thumb.jpg

 

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.

  • Upvote 1

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

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...