Jump to content

LE3 FindChild Lua


tjheldna
 Share

Recommended Posts

Hi All,

 

Im having a little trouble with FindChild and Lua..

 

I have a function that mounts the inventory item to the player's hand which works fine see below.

 


function Script:MountCurrentWeapon()

 local currentItem = self.inventory:GetCurrentItem()
 self.mountedItem = Model:Load(currentItem.itemModelPath)
 local itemMountNode = self.mountedItem:FindChild("mountPoint")
 local playerMountNode = self.entity:FindChild("Bip001 R Hand")
 itemMountNode:SetParent(playerMountNode);
 itemMountNode:SetPosition(0,0,0);
 itemMountNode:SetRotation(playerMountNode:GetRotation())

end

 

I always need a reference to self.mountedItem so I can swap weapons etc. The problem is as soon as I parent the self.mountedItem to the hand. I can't find any of it's children anymore also self.mountedItem:CountChildren() returns 0.

 

Is this a lua thing? because I can do something similar in C++ it works fine. I'm just starting out using Lua so am I may be doing something wrong?

 

Also I can't seem to return the first object when using FindChild(). I have had to add an extra node when exporting which is the parent of "mountPoint".

 

Cheers

trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
Link to comment
Share on other sites

Hi Josh

 

Just put together a little sample. Look at the outputs to the console.

 

 


function App:Start()
       --Create a window
       self.window = Window:Create()
       self.context = Context:Create(self.window)
       self.world = World:Create()
       local camera = Camera:Create()
       camera:SetRotation(35,0,0)
       camera:Move(0,0,-3)
       local light = DirectionalLight:Create()
       light:SetRotation(35,35,0)

       --Load a model
       self.entity = Model:Box()

       local child = Model:Box(1,1,1)
       child:SetColor(1.0,0.0,0.0)
       child:SetPosition(2,-2,0)
       child:SetKeyValue("name","MyBox1")

       child1 = Model:Box(1,1,1)
       child1:SetColor(0.0,1.0,0.0)
       child1:SetPosition(-2,-2,0)
       child1:SetKeyValue("name","MyBox2")

       child2 = Model:Box(1,1,1)
       child2:SetColor(0.0,1.0,0.0)
       child2:SetPosition(-2,-2,0)
       child2:SetKeyValue("name","MyBox3")

       child2:SetParent(child1)
       child1:SetParent(child)

System:Print("Value below.... should this be returning 3? now that box 1, 2, and 3 are parented")
System:Print(child:CountChildren())

--Find the first box should this work?
System:Print("Find the first box using FindChild")

local box = self.entity:FindChild("MyBox1")

if box then
       System:Print("MyBox1 Found")
else
       System:Print("Could not find MyBox1")
end

       return true
end

function App:Loop()

       if self.window:Closed() or self.window:KeyHit(Key.Escape) then return false end

       Time:Update()
       self.world:Update()
       self.world:Render()

       self.context:Sync()

       return true
end
end

trindieprod.png?dl=0spacer.png?dl=0steam-icon.png?dl=0twitter-icon.png?dl=0spacer.png?dl=0
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...