• Create Account

## Lua scripting

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1BaneTrapper  Members

1493
Like
0Likes
Like

Posted 06 March 2013 - 04:06 AM

Hello.

Lua scripting

Any suggest for Lua scripting tutorials, are there any good ones?

Also why would learning lua scripting be any good?

### #2Ashaman73  Members

13651
Like
2Likes
Like

Posted 06 March 2013 - 06:33 AM

Also why would learning lua scripting be any good?

Why are you asking to learn it, when you don't see any benefit in it ?

There's no supreme language, there are only use-cases in which one language is better suited than an other one. So, what is your use-case ?

When you need tutorials about lua,take a look at its homepage doc-section. Most often googling for "lua + problem" lead me to examples. Lua isn't really hard at all.

Edited by Ashaman73, 06 March 2013 - 06:34 AM.

Ashaman

### #3minibutmany  Members

1998
Like
2Likes
Like

Posted 06 March 2013 - 07:22 AM

Also why would learning lua scripting be any good?

If you write the "base code" for a game in C++, you can use Lua scripts to write the logic for the game. Lua feels more like VB than C++, so if you are more familiar with VB, you can use Lua to capture that while still holding on to the advantages of C++.

Stay gold, Pony Boy.

### #4BaneTrapper  Members

1493
Like
0Likes
Like

Posted 06 March 2013 - 08:13 AM

Why are you asking to learn it, when you don't see any benefit in it ?

I see benefits from learning lua...

If you meant on

Also why would learning lua scripting be any good?

It would be nice to know what experienced users see most beneficial and most useful from lua. And also what is just good for or why would .

Also why would learning lua scripting be any good?

That should be my question, thanks.

There's no supreme language, there are only use-cases in which one
language is better suited than an other one. So, what is your use-case ?

Witch is supreme language... totally agreed, Each has it own "I would be better at this then others"

My use-case would be to expand my knowledge, also the tools i can use. I am pretty limited of what i can use, knowledge wise.

One example would be, why do people wanna learn how to kill vampire? its possible that they will never find them self fighting vampires, but still people learn it...

When you need tutorials about lua,take a look at its homepage doc-section. Most often googling for "lua + problem" lead me to examples. Lua isn't really hard at all.

I did say "Any suggest for Lua scripting tutorials"( As in, i was looking for a tutorial that is approved that is good ). And most possibly i didn't stretch the question enough, or well enough to put my point on, i had issues with c++ tutorials when i was starting ( i will explain with short code )

void func(int* a)
{
}
int main()
{
int b;
func(&b);//Here he spoke as it was passed as a reference ( but the operator & in this case provides a memory address)
}


As a newbie, i thought "that" was happening, but then when you see on other guy "what" happens you are like... But the other guy said that "this" is happening and the big clash of WTF is happening and what is correct and what is wrong appear?

Edited by BaneTrapper, 06 March 2013 - 08:14 AM.

### #5ZeroBeat  Members

530
Like
1Likes
Like

Posted 06 March 2013 - 11:43 AM

If the game code is structured right, Lua would allow rapid prototyping. Imagine that there is no compile button.

This makes it possible to be in a highly productive state for longer periods of time. Rather than making a change and then wait to re-compile. The user

just makes a change and re - run the game.

Embeding Lua is not the hardest thing in the world.

Here is one good example tutorial (in my opinion): http://forums.tigsource.com/index.php?topic=22524.0

If you think that scripting is needed to fulfill a certain goal in your game, go for it. If it doesnt work right, at the very least, you would have learned something new hehe.

### #6BloodyEpi  Members

1091
Like
0Likes
Like

Posted 06 March 2013 - 12:50 PM

I learned Lua there: http://www.lua.org/pil/contents.html

Its a whole book, they put it on their website when a new version of the book/Lua came out, but its still good I think.

A while ago I was writing some scripts for a Minecraft addon called Computercraft, so if you have a Minecraftaccount and like playing around while learning, have a look at that mod, its like programming a little robot (with Lua).

Although I don't really need Lua anymore, IMHO its always good to see how other languages look like, work and what patterns people use with different languages.

### #7Vortez  Members

2705
Like
1Likes
Like

Posted 06 March 2013 - 02:09 PM

Personally, i've just made my first project that use lua for a file renamer program. Since i could not find a way to build an UI that was easy to use and could do everything i wanted to, i decided to let lua do the dirty job of generating the files name using any algorithm i wished, without the need for recompiling the program. Another thing you could do with it is to use it to initialize variables and settings when you start a project for example, so you dont need to recompile it either.

I found this code somewhere, then modified it a little. It make lua very easy to use:

#ifndef _LUA_SCRIPT_H_#define _LUA_SCRIPT_H_#ifdef __cplusplus//----------------------------------------------------------------------//#pragma comment (lib, "lua51.lib")#pragma comment (lib, "lua5.1.lib")extern "C" {	#include "lua.h"	#include "lualib.h"	#include "lauxlib.h"}//----------------------------------------------------------------------//#include "Windows.h"#include //----------------------------------------------------------------------//using namespace std;class CLuaScript {protected:	lua_State *L;public:	CLuaScript();	~CLuaScript();	void InitLua();	void CloseLua();	void ResetLua();	bool LoadScript(string scriptfile);		void CallFunction(string name);	bool SafeCallFunction(string name, char *err = NULL);	string GetGlobalString(string name);	void   SetGlobalString(string name, string value);		double GetGlobalNumber(string name);	void   SetGlobalNumber(string name, double value);		bool GetGlobalBoolean(string name);	void SetGlobalBoolean(string name, bool value);};#endif#endif //_LUA_SCRIPT_H_
#include "LuaScript.h"CLuaScript::CLuaScript(){	L = NULL;	InitLua();}CLuaScript::~CLuaScript(){	CloseLua();}void CLuaScript::InitLua(){	if(!L){		L = lua_open();		luaL_openlibs(L);	}}void CLuaScript::CloseLua(){	if(L){		lua_close(L);		L = NULL;	}}void CLuaScript::ResetLua(){	CloseLua();	InitLua();}bool CLuaScript::LoadScript(string scriptfile){	ResetLua();	bool value = true;	try {		luaL_dofile(L, scriptfile.c_str());	} catch(...) {		value = false;	}	return value;}void CLuaScript::CallFunction(string name){	//call script function, 0 args, 0 retvals	lua_getglobal(L, name.c_str());	lua_call(L, 0, 0);}bool CLuaScript::SafeCallFunction(string name, char *err){	//call script function, 0 args, 0 retvals	lua_getglobal(L, name.c_str());	int res = lua_pcall(L, 0, 0, 0);	if(res > 0){		const char *pErrMsg = lua_tostring(L, 1);		if(!err){			string Caption;			Caption = "Lua: Error in function " + name + "().";			MessageBox(0, pErrMsg, Caption.c_str(), MB_OK);		} else {			sprintf(err, "%s", pErrMsg);		}		return false;	}	return true;}string CLuaScript::GetGlobalString(string name){	string value = "";	try {		lua_getglobal(L, name.c_str());		const char *pValue = lua_tostring(L, -1);		if(pValue)			value = pValue;		lua_pop(L, 1);	} catch(...) {		}	return value;}void CLuaScript::SetGlobalString(string name, string value){	lua_pushstring(L, value.c_str());	lua_setglobal(L, name.c_str());}double CLuaScript::GetGlobalNumber(string name){	double value = 0.0;		try {		lua_getglobal(L, name.c_str());		value = lua_tonumber(L, -1);		lua_pop(L, 1);	} catch(...) {		}	return value;}void CLuaScript::SetGlobalNumber(string name, double value){	lua_pushnumber(L, (int)value);	lua_setglobal(L, name.c_str());}bool CLuaScript::GetGlobalBoolean(string name){	bool value = false;		try {		lua_getglobal(L, name.c_str());		value = lua_toboolean(L, -1) != 0;		lua_pop(L, 1);	} catch(...) {		}		return value;}void CLuaScript::SetGlobalBoolean(string name, bool value){	lua_pushboolean(L, (int)value);	lua_setglobal(L, name.c_str());}

That might not be the best wrapper out there, but it do the job fine for a simple project.

Edited by jbadams, 04 April 2013 - 05:52 AM.
Restored post contents from history.

### #8minibutmany  Members

1998
Like
1Likes
Like

Posted 06 March 2013 - 03:00 PM

On Zerobeat's note about it not being compiled, you could use Lua to make it easier for others to write "mods" for your games(if your into that). I really like the open source model, and an easy to use and understand scripting language supports that well.

Stay gold, Pony Boy.

### #9BaneTrapper  Members

1493
Like
0Likes
Like

Posted 07 March 2013 - 02:38 PM

Embeding Lua is not the hardest thing in the world.

Here is one good example tutorial (in my opinion): http://forums.tigsource.com/index.php?topic=22524.0

If you think that scripting is needed to fulfill a certain goal in your game, go for it. If it doesnt work right, at the very least, you would have learned something new hehe.

I will check it up.

As you said, at least i will know more. I am not expecting to master lua, simply to learn and maybe use it to my advantages.

I learned Lua there: http://www.lua.org/pil/contents.html

Its a whole book, they put it on their website when a new version of the book/Lua came out, but its still good I think.

A while ago I was writing some scripts for a Minecraft addon called Computercraft, so if you have a Minecraftaccount and like playing around while learning, have a look at that mod, its like programming a little robot (with Lua).

Although I don't really need Lua anymore, IMHO its always good to see how other languages look like, work and what patterns people use with different languages.

That is some amount of tutorial i will surely check that up.

Also for computer craft ^^ as much as i would like to try it out, i got no minecraft .

Learning to see how i like it, if i don't like it at least i know it partially and if i ever need it, well its gonna be easier to pick up.

...

Thanks for sharing the code

On Zerobeat's note about it not being compiled, you could use Lua to make it easier for others to write "mods" for your games(if your into that). I really like the open source model, and an easy to use and understand scripting language supports that well.

Open source is what i am aiming for.

### #103Ddreamer  Members

3822
Like
0Likes
Like

Posted 07 March 2013 - 03:42 PM

Lua is one of the common languages being used.  It has some advantages for certain uses and many people prefer it in those situations.  Lua is fairly common in game development, especially when multiple languages are included in the game source code. Learn what the strengths and weaknesses are of Lua in order to know when and where to use it.

A comprehensive book with exercises is the best way to learn, but there are some decent online tutorials for beginners.

Personal life and your private thoughts always effect your career. Research is the intellectual backbone of game development and the first order. Version Control is crucial for full management of applications and software.  The better the workflow pipeline, then the greater the potential output for a quality game.  Completing projects is the last but finest order.

by Clinton, 3Ddreamer

206
Like
0Likes
Like

Posted 09 March 2013 - 02:18 AM

Plow through the documentation to decide if it's smething you want to apply to what you already know about programming, if anything.  Otherwise you could learn it with an Engine like Love2d which should give you room to experiment.  Lua is also very "this is how it is" as far as syntax so I say give it a shot if you find other languages a bit tedious.

Edited by iGoogleThis, 09 March 2013 - 02:19 AM.

Editor // Joy-Toilet.com

Anything But Shitty Entertainment!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.