Skip to main content
gamedev.net
🔒 Locked

Lua OOP question

Started by pine3 Jun 29, 2006 at 12:45 AM 1 replies 1.1k views
Original Post
pine3
pine3
Hi, I'm using Lunar for binding Lua with c++. Is it possible for Lua and C++ to access the same object? Member functions and variables are not static. Lua has a pointer to an object created in C++ or vice versa.
rileyriley
rileyriley
I have a system set up that is working quite nicely (also using lunar). Lua and C++ can both access objects (complete with non-static member variables and functions).

Here is an example of how I have my classes declared:

#pragma once#include "lunar.h"class GameObject;class GameObjectManager;class GameObjectLuaProxy{	friend GameObjectManager;	friend GameObject;	DECLARE_SCRIPT_CLASS(GameObjectLuaProxy);private:	GameObject * gameObject;	int luaObjectReference;public:		GameObjectLuaProxy(lua_State * L);	GameObjectLuaProxy(const GameObjectLuaProxy &);	~GameObjectLuaProxy();	const GameObject * getGameObject() const { return gameObject; }	int getLuaReference();	SCRIPT_FUNCTION(GetPosition);	SCRIPT_FUNCTION(SetPosition);	SCRIPT_FUNCTION(GetVelocity);	SCRIPT_FUNCTION(SetVelocity);	SCRIPT_FUNCTION(ApplyForce);	SCRIPT_FUNCTION(SetLook);	SCRIPT_FUNCTION(SetColor);	SCRIPT_FUNCTION(GetColor);	SCRIPT_FUNCTION(SetPhysicsShape);	SCRIPT_FUNCTION(SetLabel);	SCRIPT_FUNCTION(SetReactive);	SCRIPT_FUNCTION(Destroy);	SCRIPT_FUNCTION(GetNetworkID);};


I also have to let Lua know about the class by registering it:

	REGISTER_CLASS(l, GameObjectLuaProxy);


With that done, from Lua I can call GameObjectLuaProxy:new() to get a new object in Lua. I can access the object from C++ with the userdata functionality built into the lua C++ API.

Hope this helps ~
--Riley
pine3
pine3
Thanks that really helps!

So I just have to make a function in Lua that returns the pointer to the object.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.