Jump to content

Sending objects or values to/from LUA


Shadoh
 Share

Recommended Posts

Hi guys I'm trying to mix c++ with Lua. But cant figure out how to get them to interact, how would I do the following

  • Send an object, such as a camera in to lua, or change the reference to a camera (that's stored in c++) from lua
  • Send a value to/from lua

Thanks :)

Link to comment
Share on other sites

If camera and world was created in c++ you can do something like this to pick it up in lua :

 

local world=World:GetCurrent()
for i=0,world:CountEntities() do
  if world:GetEntity(i):GetClass()==Object.CameraClass then
                self.camera=world:GetEntity(i)
                break
  end
end

HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB

Link to comment
Share on other sites

Do you have examples of what you are looking to do? Mixing between them via the way Aggror mentions is sort of a pain but once you have the objects setup it's slick to use them. Doing it via shadmar's method he mentions gets basic types from C++ to Lua and is easy to setup but again, it's basic types. I think mixing is a pain.

 

Something to test is what happens when you use SetUserData() in C++ and GetUserData() in Lua. SetUserData() generally let's you pass a class pointer casted as void*, but would be interesting to test what you get back in GetUserData() and how to use it in Lua.

Link to comment
Share on other sites

I have no particular thing I want to do, my projects at the moment are basically testing what I can/can't do :)

I'm just going to use a Lua project I think. That way any global variables and such are easily accessible from all my code.

 

In this case, I was making a day/night cycle simulation, and needed to access the camera (to SetClearColor) from c++ and control the camera from a FreeFlyCam object written in Lua.

 

On another note: is there an "option explicit" (from VB) equivalent in Lua that forces explicit declaration of variables? I keep mis-typing my variables, and spending ages trying to debug the original cause that's not obvious at all.

Link to comment
Share on other sites

That way any global variables and such are easily accessible from all my code.

 

:( this makes me sad :)

 

 

On another note: is there an "option explicit" (from VB) equivalent in Lua that forces explicit declaration of variables? I keep mis-typing my variables, and spending ages trying to debug the original cause that's not obvious at all.

 

No. Welcome to scripting :)

 

 

Finding specific LE entities between Lua and C++ is easy. Use SetKeyValue()/GetKeyValue() to give some kind of ID and your're good to go. Then just loop over the map's loaded entities and check that key value to see if it matches what you are expecting. I think what some of us thought you were asking is how can you get your own custom C++ class exposed in Lua, which is more work.

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...