luaplus Windows使用(二)

文章介绍了如何在Windows环境下使用VisualStudio2015编译和使用LuaPlus库,包括创建元表,注册对象方法,以及通过Lua调用C++函数的过程。示例中展示了如何在C++中设置和调用Lua脚本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言
上章已经讲述怎么在windows编译luaplus5.3了,这章主要讲述怎么使用
1:准备
vs2015

2:code
1>luaplusmanager.h

#ifndef LUAPLUS_MACHINE_H
#define LUAPLUS_MACHINE_H
#include "LuaPlus.h"
using namespace LuaPlus;
#include <string>
using namespace std;

class CMultiObject1
{
public:
	CMultiObject1(int num) :m_num(num)
	{     }

	int Print1(LuaState* state)//LuaState* state,int x
	{
		printf("[Print]%d \n", m_num);
		//	printf("%d %d \n", m_num,x);
		return 0;
	}
	int GetNum(LuaState* state)
	{
		printf("[GetNum]%d \n", m_num);
		return  m_num;
	}
	void Print2(int num)
	{
		printf("[Print2]%d %d\n", m_num, num);
	}
	int Sum1(int x, int y)
	{
		printf("[Sum1]%d %d\n", x, y);
		return x + y;
	}
protected:
	int m_num;
};

class CLuaplusMachine
{
public:
	CLuaplusMachine();
	virtual ~CLuaplusMachine();
public:
	void  TestLuaFunction();
	void  TestLuaObj(CMultiObject1* pObj);
private:
	LuaStateOwner m_luaState;
	string   m_strLastError;

	LuaObject m_metaTableFun;
	LuaObject m_metaTableObj;
};

#endif

2>luaplusmanager.cpp

#include "luaplusMachine.h"

CLuaplusMachine::CLuaplusMachine()
{

}
CLuaplusMachine::~CLuaplusMachine()
{

}
void CLuaplusMachine::TestLuaFunction()
{
	//LuaStateOwner state;
	//state->OpenLibs();      //create metaTable  
	m_luaState->OpenLibs();
	//LuaObject metaTableObj = m_luaState->GetGlobals().CreateTable("MultiObjectMetaTable1");
	m_metaTableFun = m_luaState->GetGlobals().CreateTable("testtesttest");
	m_metaTableFun.SetObject("__index", m_metaTableFun);     //register functor for multiobject  
														 //metaTableObj.RegisterObjectFunctor("Print", &CMultiObject::Print);      //get a instances of CMultiObject 
	m_metaTableFun.RegisterObjectFunctor("Print1", &CMultiObject1::Print1);
	m_metaTableFun.RegisterObjectFunctor("GetNum", &CMultiObject1::GetNum);

	m_metaTableFun.RegisterObjectDirect("Print2", (CMultiObject1*)0, &CMultiObject1::Print2);

	m_metaTableFun.RegisterObjectDirect("Sum1", (CMultiObject1*)0, &CMultiObject1::Sum1);

	m_luaState->GetGlobals().SetInteger("gParam1", 100);
	m_luaState->GetGlobals().SetInteger("gParam2", 200);

//	LuaObject table1Obj = m_luaState->GetGlobals().CreateTable("obj11");
	m_metaTableObj = m_luaState->GetGlobals().CreateTable("obj13");
}

void  CLuaplusMachine::TestLuaObj(CMultiObject1* pObj)
{
	
	m_metaTableObj.SetLightUserdata("__object", pObj);
	m_metaTableObj.SetMetatable(m_metaTableFun);
	m_luaState->DoFile("1.lua");
}

3>main.cpp

#include <iostream>
using namespace std;
//#include "luaplusmanager.h"

#include "LuaPlus.h"
using namespace LuaPlus;
#include <string>
using namespace std;

#include <stdio.h>
#include <stdlib.h>

int Add(int x, int y)
{
	printf("sum=%d \n", x + y);
	return x + y;
}

class CTest
{
public:
	CTest(){}
	~CTest() {}
public:
	int Sub(int x, int y)
	{
		printf("sub=%d \n", x - y);
		return x - y;
	}
};

class CMultiObject
{
public:
	CMultiObject(int num) :m_num(num)
	{     }

	int Print1(LuaState* state)//LuaState* state,int x
	{
		printf("[Print]%d \n", m_num);
	//	printf("%d %d \n", m_num,x);
		return 0;
	}
	int GetNum(LuaState* state)
	{
		printf("[GetNum]%d \n", m_num);
		return  m_num;
	}
	void Print2(int num)
	{
		printf("[Print2]%d %d\n", m_num, num);
	}
	int Sum1(int x, int y)
	{
		printf("[Sum1]%d %d\n", x, y);
		return x + y;
	}
protected:
	int m_num;

};
template<typename Callee, typename Func>
void RegisterObjectLink(LuaObject* pLuaObject,const char* szfunName, Callee* callee, Func func)//
{
	pLuaObject->RegisterObjectDirect(szfunName,(Callee*)0, func);
}


//1.lua
//obj1:Print()
//obj2:Print()
//obj1:Print2(123)
//obj1:Sum1(1, 2)
//obj1 : Sum1(gParam1, gParam2)

int main()
{
	{
// 		CMyGlobal* pGlobal = new CMyGlobal;
// 		CLuaPlusManager*  pLuaManager = new CLuaPlusManager;
// 		pLuaManager->RegisterGlobalFun(pGlobal);
// 		pLuaManager->RunScripteFile("2.lua");

	}
	/*
	system("pause");
	CMyGlobal* pGlobal = new CMyGlobal;
	CLuaPlusManager*  pLuaManager = new CLuaPlusManager;
	pLuaManager->RegisterGlobalFun(pGlobal); //注册全局函数类
	long long n64Param1 = (1ll << 32) + 1;
	long long n64Param2 = (2ll << 32) + 1;
	pLuaManager->InitTwoParam(n64Param1, n64Param2);
	pLuaManager->RunScripteFile("2.lua");
	
	CTest* pTest = new CTest;
	LuaStateOwner state;
	state->OpenLibs();
	state->GetGlobals().SetInteger("gParam1", 10);
	state->GetGlobals().SetInteger("gParam2", 20);
	state->GetGlobals().RegisterDirect("add", Add);

	//state->GetGlobals().RegisterDirect("sub", *pTest, &CTest::Sub);
	//LuaObject luaObject1 =  state->GetGlobals().CreateTable("CTest");
	//luaObject1.SetMetatable(luaObject1);
	//luaObject1.SetObject("__index", luaObject1);

	//luaObject1.RegisterObjectDirect("Sub", &CTest::Sub)
	//luaObject1.RegisterObjectDirect("Sub", pTest, &CTest::Sub);
	// state->GetGlobals().RegisterDirect("add", test, &Test::add);
	//metaTableObj.RegisterObjectDirect("Print2", (CMultiObject*)0, &CMultiObject::Print2);
	state->DoFile("1.lua");
	*/

	////////////////////////////////////////
	LuaStateOwner state;
	state->OpenLibs();      //create metaTable  
	LuaObject metaTableObj = state->GetGlobals().CreateTable("MultiObjectMetaTable");
	metaTableObj.SetObject("__index", metaTableObj);     //register functor for multiobject  
	//metaTableObj.RegisterObjectFunctor("Print", &CMultiObject::Print);      //get a instances of CMultiObject 
	metaTableObj.RegisterObjectFunctor("Print1", &CMultiObject::Print1);
	metaTableObj.RegisterObjectFunctor("GetNum", &CMultiObject::GetNum);

	metaTableObj.RegisterObjectDirect("Print2", (CMultiObject*)0, &CMultiObject::Print2);

	metaTableObj.RegisterObjectDirect("Sum1", (CMultiObject*)0, &CMultiObject::Sum1);

	state->GetGlobals().SetInteger("gParam1", 100);
	state->GetGlobals().SetInteger("gParam2", 200);

//	metaTableObj.RegisterObjectDirect("Print2", (CMultiObject*)0, &CMultiObject::Print1);
//	metaTableObj.RegisterObjectDirect("GetNum", (CMultiObject*)0, &CMultiObject::GetNum);
	{
// 		CMultiObject obj1(10);     //"clone" a object in lua, the lua object(here is table) has obj1's data  
// 		LuaObject obj1Obj = state->BoxPointer(&obj1);
// 		obj1Obj.SetMetatable(metaTableObj);     //put lua object to Global scope, thus it can be accessed later. 
// 		state->GetGlobals().SetObject("obj1", obj1Obj);
// 		CMultiObject obj2(20);
// 		LuaObject obj2Obj = state->BoxPointer(&obj2);
// 		obj2Obj.SetMetatable(metaTableObj);
// 		state->GetGlobals().SetObject("obj2", obj2Obj);      //now call Print and Print2   
// 
// 		state->DoFile("1.lua");
	}
	{

		LuaObject table1Obj = state->GetGlobals().CreateTable("obj1");
		for (int i = 1; i < 2; i++)
		{
			CMultiObject obj1(i);
			table1Obj.SetLightUserdata("__object", &obj1);
			table1Obj.SetMetatable(metaTableObj);
			state->DoFile("1.lua");

			
			//state->DoString("obj1:Print2(5)");
		}
		
		
		

// 		CMultiObject obj2(20);
// 		LuaObject table2Obj = state->GetGlobals().CreateTable("obj2");
// 		table2Obj.SetLightUserdata("__object", &obj2);
// 		table2Obj.SetMetatable(metaTableObj);

	//	state->DoString("table1:Print()");
	//	state->DoString("table2:Print()");

// 		CMultiObject obj1(10);
// 		LuaObject table1Obj = state->GetGlobals().CreateTable("obj1");     
// 		table1Obj.SetLightUserdata("__object", &obj1);
// 		table1Obj.SetMetatable(table1Obj);
// 		CMultiObject obj2(20);
// 		LuaObject table2Obj = state->GetGlobals().CreateTable("obj2");    
// 		table2Obj.SetLightUserdata("__object", &obj2);     
// 		table2Obj.SetMetatable(table2Obj);
	}
	 
//	state->DoFile("1.lua");
//	state->DoString("obj1:Print();");
//	state->DoString("obj2:Print();");
	system("pause");
	return 0;
}

///////////////////////////////////////

void TestRegObjectDispatchFunctor()
{
// 	LuaStateOwner state;
// 	LuaObject table1Obj = state->GetGlobals().CreateTable("table1");     
// 	table1Obj.SetLightUserdata("__object", &obj1);    
// 	table1Obj.SetMetatable(table1Obj);
// 	LuaObject table2Obj = state->GetGlobals().CreateTable("table2");    
// 	table2Obj.SetLightUserdata("__object", &obj2);     
// 	table2Obj.SetMetatable(table2Obj);
// 	state->DoString("table1:Print()");     
// 	state->DoString("table2:Print()");
}

4:1.lua

--[[
obj1:Print1()
obj1:GetNum()
obj1:Print2(123)
obj1:Sum1(1,2)
--obj1:Sum1(gParam1,gParam2)

-----------------------------------------two object------
obj2:Print1()
obj2:GetNum()
--]]
--obj1:Print()
--obj2:Print()
obj13:Print2(123)
obj13:Sum1(1, 2)
obj13:Sum1(gParam1, gParam2)

3:运行结果
在这里插入图片描述
如果觉得有用,麻烦点个赞,加个收藏

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值