Talk:Lua tutorial

From Valve Developer Community
Jump to: navigation, search

Garry's mod was Epic fail, still good tutorial!--Gear 07:23, 16 Apr 2008 (PDT)

I don't see how GMod fails. Anyhow, I want to find out how to make a model entity that can actually move around. Deriving from CBaseAnimating with a few extra bindings did not help. --Philpax 22:40, 17 Apr 2008 (PDT)

Haha, Aside that, this tutorial needs to be converted from 1st Person, to Third person. So if anyone added anything then they should have a go and fix it.--Gear 23:51, 17 May 2008 (PDT)

errors with console command

Hi. So, i done this tutorial up to the part before you add the code for 'run_luafile', so when i was testing, con commands dosample1 worked, but once i had added all the run_luafile code stuff, ingame no commands for any lua files worked. They didn't give any errors, they just simply didn't do anything. They are activated, but won't work. I tried many lua files, including the ones dosample. Bu still the same. Anyone else experience this??? Please help. --Craziestdan 08:55, 28 Sep 2008 (PDT)

Extras

This section has been saved here due to it is okay, but it requires permission, which would be hard to aquire in some situations, plus LUA Code is not really owned by someone, therefore it makes not much sense to have it here in the first place since this section should be more about doing your own code, without the real time examples. It must also be converted from 1st Person to Third Person. Sorry yo.--Gear 23:57, 17 May 2008 (PDT)

Lua is open source, permission is not needed. -- John6000
Exactly.--Gear 20:41, 19 May 2008 (PDT)

Stop trying to be a clever-ass and tell me what it is you want? - to take the article down? That's not going to happen... --John6000

Who said I was being Clever, I think I stated what is need previously. If caps will Help that can work out. "NEEDS TO BE CONVERTED FROM 1ST PERSON TO THIRD PERSON". I hope that helps :D. There is also no need to take down the whole article, no one ever mentioned that either, I just simply stated that the code listed was used from a real example, which it should not be, and should be re-written, that way is a User grabs and uses the code, they won't require permission at all.--Gear 00:15, 21 May 2008 (PDT)
Aside that this should be more of a Job for the person who wrote most of this tutorial.--Gear 00:16, 21 May 2008 (PDT)

Extras

You might want to have a command to run a file/string on the go, such as Garry's Mod (lua_run ply:SetHealth(100) and whatnot)

Lua_Runfile console command

Here's the code you need for the runfile concommand:

CON_COMMAND( lua_runfile, "Run a Lua file")
{
	lua_State *luaState = GetLuaState();

	if(UTIL_IsCommandIssuedByServerAdmin())
	{
		char luafile[ MAX_PATH ];
		char luafunc[ MAX_PATH ];
		try
		{
			Q_snprintf( luafile, sizeof( luafile ), "lua\\%s", args[1] );
			Q_snprintf( luafunc, sizeof( luafunc ), args[2] );
			RunLuaFile( luafile );
			luabind::call_function<void>(luaState, ( luafunc ));
		}
		catch( luabind::error ex)
		{
			lua_State* L = ex.state();
			cvar->ConsoleColorPrintf( LUA_ERROR_PRINT_COLOUR, "[Lua] %s\n", lua_tostring(L, -1));
			lua_pop(L, 1); 
		}
	}
	else
	{
		cvar->ConsoleColorPrintf( LUA_PROHIBIT_PRINT_COLOUR, "[Lua] Admin-only command!\n");
	}
}

This will check if the command was issued by a server admin. If so, it'll append args[1] to lua\ (eg to run mymod\lua\helloworld.lua you'd do lua_runfile helloworld.lua), args[2] appends to the function you want to run within the lua file you ran. (eg lua_runfile sample1.lua sample)

Lua_Run console command

For dostring, the command is fairly easy. You need to add a new helper function like runfile, however:

bool RunLuaString( const char* string )
{
	if (int error = luaL_dostring(luaState, string) != 0) 
	{
		cvar->ConsoleColorPrintf( LUA_ERROR_PRINT_COLOUR, "[LUA] Error while doing 'dostring': %s\n", string);
		return false;
	}
	return true;
}

After adding this, you only have to add the concommand:

CON_COMMAND( lua_run, "Run a Lua string")
{
	if(UTIL_IsCommandIssuedByServerAdmin())
	{
		try
		{
			RunLuaString( args.ArgS() );
		}
		catch( luabind::error ex)
		{
			lua_State* L = ex.state();
			cvar->ConsoleColorPrintf( LUA_ERROR_PRINT_COLOUR, "[Lua] %s\n", lua_tostring(L, -1));
			lua_pop(L, 1); 
		}
	}
	else
	{
		cvar->ConsoleColorPrintf( LUA_PROHIBIT_PRINT_COLOUR, "[Lua] Admin-only command!\n");
	}
}

It will not work if you put the command in "" (like lua_run "ply:SetHealth(100)"). args.ArgS() takes the entire argument string minus the actual lua_run command.

Also, you may see the new define: LUA_PROHIBIT_PRINT_COLOUR. Drop this into your header file to add it:

#define LUA_PROHIBIT_PRINT_COLOUR Color( 40,160,255,255 )

Example of usage

You start up your mod, and you want to test a new Lua file you made and a new function you added. Now, it would be impractical to make a new Lua file just for the new command, so what you would do is:

Start a new game. Now, let's test the new Lua file first. Drop the file into the lua directory in your mod and give it a name of amazinglua.lua. Now, ingame, type in the console:

lua_runfile amazinglua.lua amazingfunction

It will run amazingfunction in amazinglua.lua. Let's say now that this had a different name, and was in a folder called Amazing in the Lua folder.

lua_runfile amazing/newname.lua amazingfunction

Now, to test the new command. Let's assume this new command is Ignite the player.

lua_run ply = GetPlayerByID(1)

(I'm assuming you have a GetPlayerByID function)

Alright, now we have the player we want to ignite in our grasps. Now to ignite them!

lua_run ply:Ignite(30)

They're on fire! This will work for generally anything. You cannot split lua commands using ; (eg lua_run ply = GetPlayerByID(1);ply:Ignite(30)) because the Source Engine will think "Oh, it's a new command." and it'll think anything after the ; is a different command.

Regarding the contents of this page

The person who originally wrote this page wants all of it removed, however, lua is being used more and more in mods, and having a tutorial around would really help for them. Should the page be kept until a new tutorial is written? Otherwise, this page should either redirect to another wiki/website which has a tutorial, be removed, or locked. Solokiller 11:04, 11 June 2009 (UTC)

If someone wants to create a new page and rewrite it fixing the errors, they can go ahead, but I'm going to mark it for deletion for now. I do not think it's appropriate to keep an article if the original author wants it removed as it's their work. The exception would be if their had been significant contributions by other authors, but that does not seem to be the case here. I don't agree with blanking a linked page though. If an author wants their page blanked, I don't think it's to much to ask that they at least make sure nothing links to it first and they use a proper deletion tag so no one mistakenly restores it. --brandished 04:02, 12 June 2009 (UTC)

I think that it should be kept, at least for the information it contains, until someone has the time to fix it up. It should, however, have a heading which states this fact, that it is not fully working. Marineio 21:42, 7 July 2009 (UTC)