Jump to content

[Snippet] Global picking

Recommended Posts

I think it would be cool if we had a forum here just for snippets because I have a lot that aren't full fledged files so I don't want to put them in the asset store, and I'm horrible at organizing this stuff myself, plus I sometimes code on different machines so it would be nice to have it stored online and since they are LE oriented why not have a forum here for them?


Going to see how this is accepted smile.png


So using Lua I have a script attached to multiple entities that I want to know when they get picked. It would be inefficient to place picking checks in the entity script since it would run that code for every entity in the scene. So instead I decided to make a generic picking function that I'll place in App.lua and it'll call a function to an attached script to the entity that was picked if said function exists.


This assumed App has a camera variable. By default in the Lua template no camera is created. What I did was make a camera entity, attach a script, and inside Script::Start() I did:


App.camera = self.entity


function App:HandlePicking()
  -- look for mouse picks and fire a function in the attached script if one exists
  if self.window:MouseHit(1) == true then

     local pickinfo = PickInfo()
     local p = self.window:GetMousePosition()
     if self.camera:Pick(p.x, p.y, pickinfo, 0.5, true) == true then
        if pickinfo ~= nil then

           if pickinfo.entity.script ~= nil then

              if pickinfo.entity.script.Picked ~= nil then




Then in any script you want to handle these define:


function Script:Picked(info)

Share this post

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

  • Create New...