Talk:Lua tutorial: Difference between revisions
No edit summary |
No edit summary |
||
Line 10: | Line 10: | ||
:Lua is open source, permission is not needed. -- John6000 | :Lua is open source, permission is not needed. -- John6000 | ||
::Exactly.--[[User:MrTwoVideoCards|Gear]] 20:41, 19 May 2008 (PDT) | ::Exactly.--[[User:MrTwoVideoCards|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? | 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 | ||
== Extras== | == Extras== |
Revision as of 09:17, 20 May 2008
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)
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
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") { if(UTIL_IsCommandIssuedByServerAdmin()) { char luafile[ MAX_PATH ]; try { Q_snprintf( luafile, sizeof( luafile ), "lua\\%s", args[1] ); RunLuaFile( luafile ); } 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)
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
It will run. 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
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.