Forum Thread
  Posts  
[Source ] Adding Lua (Forums : Coding & Scripting : [Source ] Adding Lua) Locked
Thread Options 1 2
Jun 30 2009 Anchor

Hi!
I'm a middle class C++ Programmer and would like to add Lua support for my current mod project, so my Lua coding teammates have something to do too. I looked up in the Valve Developer Wiki and found two tutorials: one using LuaBind, that i remember to have used a while ago, but the author said it was broken, and another one that was hard to understand and didn't work altogether. Well, so what I'm trying to do is to get my Code to load all the lua files that are specified. I have thought of two approuches: Make a Master.lua that then loads all other luas, or a C++ function that loads all luas inside the lua folder. I'm not sure which one is smarter.
So if someone has a link to a tut or knows how to do it himself and maybe how i should get it to load everything, i would appreciate it

greetz,
Drake

Jun 30 2009 Anchor

There is a new tutorial for adding lua up on the wiki it uses a newer version of lua if I am not mistaken and doesn't use luabind. I recommend you check that out.
Developer.valvesoftware.com

lodle
lodle I am Lodle
I pwn Henley.
Jun 30 2009 Anchor

It was writen by me, feel free to ask any questions here about it. :P

--

User Posted Image
Jun 30 2009 Anchor

Ok, i tried it, but couldn't get that interface thingy to work. Could you help me alittle? :)

lodle
lodle I am Lodle
I pwn Henley.
Jun 30 2009 Anchor

Well you need to tell me what you have done and what errors your getting.

--

User Posted Image
Jul 2 2009 Anchor

Well, may problem's a little more generic. It's just that the description "Now you can make a lua interface" is alittle vague. I mean, what is implemented and what do i have to implement?

Jul 2 2009 Anchor

I've also been using the "Adding Lua" tutorial and have been having some trouble with it. Firstly, I'm very greatful you put up a tutorial on Lua. Thanks :D But I'd like to request some additions:

You say "You will need to make your own class that inherets from LuaHandle ..." and "And thus you will need to define this functions in another file". Would it be possible to instead include complete examples of these files in the wiki as you did with the ge_luamanager files? That would be really helpful.

Thanks :)

lodle
lodle I am Lodle
I pwn Henley.
Jul 2 2009 Anchor

The reason i dont include complete versions of these is because they will be dependent on what you want to do. So the first thing you need to do is make a new class that inherits from LuaHandle

class MyLuaHandle : public LuaHandle
{
}

Next thing to do is make a constructor and the 4 functions you need to override

class MyLuaHandle : public LuaHandle

{
public:
	MyLuaHandle();
	void Init();
	void Shutdown();
	void RegFunctions();
	void RegGlobals();
}

Now you need to make sure your constuctor calls Register so in a cpp file start the class def:

MyLuaHandle::MyLuaHandle()
{
	Register();
}

and implment the other functions like on the wiki.


So what you have to do is dependent on what you want to do. You have to provide the mapping between c++ and lua in the form of functions (lua calls into c++) and callbacks (c++ calls into lua). If you tell me what your trying to do i could help a bit better.

Edited by: lodle

Jul 2 2009 Anchor

Omg! Epic post! please add to wiki! Add an example of how to have lua register a C++ func and You're perfecx!

lodle
lodle I am Lodle
I pwn Henley.
Jul 2 2009 Anchor

This is on the wiki all ready plus that as well! Look at the 4 functions i have there and read the text!

Edited by: lodle

Jul 2 2009 Anchor

kk, guess that be my bad. Well, then I'll be on programming a lua base

Edited by: Drake25

Jul 2 2009 Anchor

When I get this all working, I'll update the wiki page a little. I got it all to compile, now I just need to have Lua functions called in C++ and C++ functions called in Lua.

Two quick questions before I go off and try that. In the init function, you call a lua file like so:

FileHandle_t f = filesystem->Open( luaFile, "rb", "MOD" );

1. What location is this looking for the file? SourceMods/MyMod?
2. The GE lua files seem be be encrypted ( I'm new to Lua :S ). Is it possible to decrypt these or something so I can look at how your code was done?
Thanks again :D

p.s. For other guys who haven't been able to get it to compile, here's my stuff so far:

myluamanager.h


#ifndef MYLUAMANAGER_H
#define MYLUAMANAGER_H
#ifdef _WIN32
#pragma once
#endif

#include "ge_luamanager.h"

class MyLuaHandle : public LuaHandle
{
public:
	void Init( void );
	void Shutdown( void );
 
	void RegFunctions( void );
	void RegGlobals( void );
 
	MyLuaHandle( void );
 
private:
	lua_State *pL;
	bool m_bLuaLoaded; // is this the right place for this?
};
extern MyLuaHandle* GetLuaHandle();

#endif //MC_GE_LUAMANAGER_H
 

myluamanager.cpp (I added in a Msg call to see if it reached that far. You can remove that when you're sure it's running)


//hl2aitools

#include "cbase.h"

#include "myluahandle.h"
#include "KeyValues.h"
#include "filesystem.h"
 
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

MyLuaHandle* g_LuaHandle = NULL;
MyLuaHandle* GetLuaHandle()
{
	return g_LuaHandle;
}

MyLuaHandle::MyLuaHandle() : LuaHandle()
{
	g_LuaHandle = this;
	Register();
}

void MyLuaHandle::Init()
{
	Msg("\nIn MyLuahandle Init\n");
	const char* luaFile = "myLuaFile.lua";
 
	//Load into buffer
	FileHandle_t f = filesystem->Open( luaFile, "rb", "MOD" );
	if (!f)
		return;
 
	// load file into a null-terminated buffer
	int fileSize = filesystem->Size(f);
	unsigned bufSize = ((IFileSystem *)filesystem)->GetOptimalReadSize( f, fileSize + 1 );
 
	char *buffer = (char*)((IFileSystem *)filesystem)->AllocOptimalReadBuffer( f, bufSize );
	Assert(buffer);
 
	((IFileSystem *)filesystem)->ReadEx( buffer, bufSize, fileSize, f ); // read into local buffer
	buffer[fileSize] = '\0'; // null terminate file as EOF
	filesystem->Close( f );	// close file after reading
 
	int error = luaL_loadbuffer( GetLua(), buffer, fileSize, luaFile );
	if (error)
	{
		Warning("[LUA-ERR] %s\n", lua_tostring(GetLua(), -1));
		lua_pop(GetLua(), 1);  // pop error message from the stack 
		Warning("[LUA-ERR] One or more errors occured while loading lua script!\n");
		return;
	}
	CallLUA(GetLua(), 0, LUA_MULTRET, 0, luaFile );
	m_bLuaLoaded = true;
}

void MyLuaHandle::RegGlobals()
{
	LG_DEFINE_INT("FOR_ALL_PLAYERS", -1);
	LG_DEFINE_INT("INVALID_ENTITY", -1);
	LG_DEFINE_INT("NULL", 0);
	//LG_DEFINE_INT("GE_MAX_HEALTH", MAX_HEALTH);
	//LG_DEFINE_INT("GE_MAX_ARMOR", MAX_ARMOR);
	LG_DEFINE_INT("MAX_PLAYERS", gpGlobals->maxClients);
 
	//Team Indices
	LG_DEFINE_INT("TEAM_NONE",TEAM_UNASSIGNED);
	LG_DEFINE_INT("TEAM_SPECTATOR",TEAM_SPECTATOR);
	//LG_DEFINE_INT("TEAM_MI6",TEAM_MI6);
	//LG_DEFINE_INT("TEAM_JANUS",TEAM_JANUS);
 
	//ClientPrintAll Types
	LG_DEFINE_INT("HUD_PRINTNOTIFY",HUD_PRINTNOTIFY);
	LG_DEFINE_INT("HUD_PRINTCONSOLE",HUD_PRINTCONSOLE);
	LG_DEFINE_INT("HUD_PRINTTALK",HUD_PRINTTALK);
	LG_DEFINE_INT("HUD_PRINTCENTER",HUD_PRINTCENTER);
}

void MyLuaHandle::RegFunctions()
{
	REG_FUNCTION( Msg );
	REG_FUNCTION( ConMsg );
	REG_FUNCTION( ClientPrintAll );
	REG_FUNCTION( GetTime );
}

void MyLuaHandle::Shutdown()
{
}

I added them both in server/source files

Edited by: SecretiveLoon

lodle
lodle I am Lodle
I pwn Henley.
Jul 2 2009 Anchor

You dont need to have lua_State *pL; in your class as this is part of LuaHandle (use GetLua() to get the lua handle) but besides that it looks fine.

Here is an example script we use for ges: Wiki.goldeneyesource.net

and yeah that will look for the file in your mod folder.

--

User Posted Image
Jul 3 2009 Anchor

Wait, I'm missing something. Do i now have to make sepperate Lua functions in C++ or are they already their? (Before i continue programming to death)

Jul 3 2009 Anchor

It looks like the ones that already have global scope (such as Msg) just have to use the REG_FUNCTION define. Trying to access something in a class like GetMotor in ai_basenpc.cpp seems to require making a new function ... which seems to defeat the purpose a little to me. I'm hoping my understanding of Lua is just flawed here. I can't figure out how else folks could get Lua to do anything useful in non-open source games like DoW or WoW. I hope to God I'm wrong.

lodle
lodle I am Lodle
I pwn Henley.
Jul 3 2009 Anchor

You have to make mappings from c++ to lua other wise how the hell does lua now what to do? Read the lua docs and look at my 4 examples there.

--

User Posted Image
Jul 3 2009 Anchor

Great. ok, then I'm on trying my best to connect....
P.S. How does GMod handle this problem?

lodle
lodle I am Lodle
I pwn Henley.
Jul 3 2009 Anchor

The same way every one else does, code the mappings.

--

User Posted Image
Jul 6 2009 Anchor

Hey Lodle,

I've been getting errors when trying to compile with your tutorial.

Error wrote: 1>MSVCRT.lib(MSVCR80.dll) : error LNK2005: _rand already defined in randoverride.obj
1>MSVCRT.lib(MSVCR80.dll) : error LNK2005: _srand already defined in randoverride.obj
1>MSVCRT.lib(MSVCR80.dll) : error LNK2005: _realloc already defined in memoverride.obj
1>MSVCRT.lib(MSVCR80.dll) : error LNK2005: _free already defined in memoverride.obj


Any ideas? I compiled the LUA 5.1 Lib and added it to the project and followed the rest of the tutorial.

--

Jordan Nolan
Situation Outbreak Developer
Lethal Stigma Developer
Agent Red Productions

lodle
lodle I am Lodle
I pwn Henley.
Jul 6 2009 Anchor

Make sure the lua lib is compiled using Multi thread lib (prop -> c++ -> code gen -> runtime lib should be /MT)

--

User Posted Image
Jul 6 2009 Anchor

That fixed those errors, but when I compile again I get more. Its never ending :P

1>LIBCMT.lib(crtheap.obj) : error LNK2005: __calloc_crt already defined in memoverride.obj
1>LIBCMT.lib(crtheap.obj) : error LNK2005: __recalloc_crt already defined in memoverride.obj
1>LIBCMT.lib(calloc.obj) : error LNK2005: _calloc already defined in memoverride.obj
1>LIBCMT.lib(sbheap.obj) : error LNK2005: __get_sbh_threshold already defined in memoverride.obj
1>LIBCMT.lib(sbheap.obj) : error LNK2005: __set_sbh_threshold already defined in memoverride.ob

--

Jordan Nolan
Situation Outbreak Developer
Lethal Stigma Developer
Agent Red Productions

lodle
lodle I am Lodle
I pwn Henley.
Jul 7 2009 Anchor

ignore the lib LIBCMT when compiling lua

--

User Posted Image
Jul 13 2009 Anchor

Hello, when i try to compile, i get this error:

C:\\?????\src\dlls\myluahandle.h(7) : fatal error C1083: Cannot open include file: 'ge_luamanager.h': No such file or directory

ge_luamanager.cpp

C:\\?????\adding lua to mod\ge_luamanager.h(20) : fatal error C1083: Cannot open include file: 'lua.h': No such file or directory

Any help would be appreciated

Jul 13 2009 Anchor

Where did you put your ge_luamanager and did you include the lua5.1 include folder?

--

Jordan Nolan
Situation Outbreak Developer
Lethal Stigma Developer
Agent Red Productions

Jul 26 2009 Anchor

I have added ge_luamanager but also i've added lua.h to the same folder but i still get errors that it cant find it.

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.