Difference between revisions of "ETPro:Lua Sample Code"

From WolfWiki
Jump to: navigation, search
m
m (Added link to toturals page)
 
(8 intermediate revisions by 4 users not shown)
Line 1: Line 1:
This code does nothing useful besides demonstrate and exercise the [[ETPro:Lua Mod API]]
+
This code does nothing useful besides demonstrate and exercise the [[Lua Mod API]]
  
<pre>
+
Real tutorials about ETPro Lua scripting can be found [[Lua Tutorials|here]].
-- printf wrapper
+
 
function et.G_Printf(...)
+
<font color="green">-- printf wrapper</font>
 +
<font color="blue">function</font> et.G_Printf(...)
 
         et.G_Print(string.format(unpack(arg)))
 
         et.G_Print(string.format(unpack(arg)))
end
+
<font color="blue">end</font>
 
+
-- test some of the supported etpro lua functions
+
function test_lua_functions()
+
<font color="green">-- test some of the supported etpro lua functions</font>
         et.trap_Cvar_Set( "bla1", "bla2" )
+
<font color="blue">function</font> test_lua_functions()
         et.G_Printf( "sv_hostname [%s]\n", et.trap_Cvar_Get( "sv_hostname" ) )
+
         et.trap_Cvar_Set( <font color="darkblue">"bla1"</font>, <font color="darkblue">"bla2"</font> )
         et.G_Printf( "configstring 1 [%s]\n", et.trap_GetConfigstring( 1 ) )
+
         et.G_Printf( <font color="darkblue">"sv_hostname [%s]\n"</font>, et.trap_Cvar_Get( <font color="darkblue">"sv_hostname"</font> ) )
         et.trap_SetConfigstring( 4, "yadda test" )
+
         et.G_Printf( <font color="darkblue">"configstring 1 [%s] \n"</font>, et.trap_GetConfigstring( <font color="blue">1</font> ) )
         et.G_Printf( "configstring 4 [%s]\n", et.trap_GetConfigstring( 4 ) )
+
         et.trap_SetConfigstring( <font color="blue">4</font>, <font color="darkblue">"yadda test"</font> )
         et.trap_SendConsoleCommand( et.EXEC_APPEND, "cvarlist *charge*\n" )
+
         et.G_Printf( <font color="darkblue">"configstring 4 [%s]\n"</font>, et.trap_GetConfigstring( <font color="blue">4</font> ) )
         et.trap_SendServerCommand( -1, "print \"Yadda yadda\"" )
+
         et.trap_SendConsoleCommand( et.EXEC_APPEND, <font color="darkblue">"cvarlist *charge*\n"</font> )
         et.G_Printf( "gentity[1022].classname = [%s]", et.gentity_get( 1022, "classname" ) )
+
         et.trap_SendServerCommand( -<font color="blue">1</font>, <font color="darkblue">"print \"Yadda yadda\""</font> )
end
+
         et.G_Printf( <font color="darkblue">"gentity[1022].classname = [%s]"</font>, et.gentity_get( <font color="blue">1022</font>, <font color="darkblue">"classname"</font> ) )
 
+
<font color="blue">end</font>
-- called when game inits
+
function et_InitGame( levelTime, randomSeed, restart )
+
         et.G_Printf( "et_InitGame [%d] [%d] [%d]\n", levelTime, randomSeed, restart )
+
<font color="green">-- called when game inits</font>
         et.RegisterModname( "bani qagame " .. et.FindSelf() )
+
<font color="blue">function</font> et_InitGame( levelTime, randomSeed, restart )
--     test_lua_functions()
+
         et.G_Printf( <font color="darkblue">"et_InitGame [%d] [%d] [%d]\n"</font>, levelTime, randomSeed, restart )
end
+
         et.RegisterModname( <font color="darkblue">"bani qagame "</font> .. et.FindSelf() )
 
+
<font color="green">--     test_lua_functions()</font>
-- called every server frame
+
<font color="blue">end</font>
function et_RunFrame( levelTime )
+
         if math.mod( levelTime, 1000 ) == 0 then
+
--             et.G_Printf( "et_RunFrame [%d]\n", levelTime )
+
<font color="green">-- called every server frame</font>
         end
+
<font color="blue">function</font> et_RunFrame( levelTime )
end
+
         <font color="blue">if</font> math.mod( levelTime, <font color="blue">1000</font> ) == <font color="blue">0 then</font>
 
+
<font color="green">--             et.G_Printf( et_RunFrame [%d]\n", levelTime )</font>
-- called for every clientcommand
+
         <font color="blue">end</font>
-- return 1 if intercepted, 0 if passthrough
+
<font color="blue">end</font>
function et_ClientCommand( clientNum, cmd )
+
         et.G_Printf( "et_ClientCommand: [%d] [%s]\n", et.trap_Argc(), cmd )
+
         return 0
+
<font color="green">-- called for every clientcommand</font>
end
+
<font color="green">-- return 1 if intercepted, 0 if passthrough</font>
 
+
<font color="blue">function</font> et_ClientCommand( clientNum, cmd )
-- called for every consolecommand
+
         et.G_Printf( <font color="darkblue">"et_ClientCommand: [%d] [%s]\n"</font>, et.trap_Argc(), cmd )
-- return 1 if intercepted, 0 if passthrough
+
         <font color="blue">return</font> <font color="blue">0</font>
function et_ConsoleCommand()
+
<font color="blue">end</font>
         et.G_Printf( "et_ConsoleCommand: [%s] [%s]\n", et.trap_Argc(), et.trap_Argv(0) )
+
         if et.trap_Argv(0) == "listmods" then
+
                 i = 1
+
<font color="green">-- called for every consolecommand</font>
                 repeat
+
<font color="green">-- return 1 if intercepted, 0 if passthrough</font>
 +
<font color="blue">function</font> et_ConsoleCommand()
 +
         et.G_Printf( <font color="darkblue">"et_ConsoleCommand: [%s] [%s]\n"</font>, et.trap_Argc(), et.trap_Argv(<font color="blue">0</font>) )
 +
         <font color="blue">if</font> et.trap_Argv(<font color="blue">0</font>) == <font color="darkblue">"listmods"</font> <font color="blue">then</font>
 +
                 i = <font color="blue">1</font>
 +
                 <font color="blue">repeat</font>
 
                         modname, signature = et.FindMod( i )
 
                         modname, signature = et.FindMod( i )
                         if modname and signature then
+
                         <font color="blue">if</font> modname <font color="blue">and</font> signature <font color="blue">then</font>
                                 et.G_Printf( "vm slot [%d] name [%s] signature [%s]\n", i, modname, signature )
+
                                 et.G_Printf( <font color="darkblue">"vm slot [%d] name [%s] signature [%s]\n"</font>, i, modname, signature )
                                 et.IPCSend( i, "hello" )
+
                                 et.IPCSend( i, <font color="darkblue">"hello"</font> )
                         end
+
                         <font color="blue">end</font>
                         i = i + 1
+
                         i = i + <font color="blue">1</font>
                 until modname == nil or signature == nil
+
                 <font color="blue">until</font> modname == <font color="blue">nil</font> <font color="blue">or</font> signature == <font color="blue">nil</font>
                 return 1
+
                 <font color="blue">return</font> <font color="blue">1</font>
         end
+
         <font color="blue">end</font>
         return 0
+
         <font color="blue">return</font> <font color="blue">0</font>
end
+
  <font color="blue">end</font>
 
+
-- called when we receive an IPC from another VM
+
function et_IPCReceive( vmnumber, message )
+
<font color="green">-- called when we receive an IPC from another VM</font>
         et.G_Printf( "IPCReceive [%d] from [%d] message [%s]\n", et.FindSelf(), vmnumber, message )
+
<font color="blue">function</font> et_IPCReceive( vmnumber, message )
end
+
         et.G_Printf( <font color="darkblue">"IPCReceive [%d] from [%d] message [%s]\n"</font>, et.FindSelf(), vmnumber, message )
 
+
<font color="blue">end</font>
-- called for every ClientConnect
+
function et_ClientConnect( clientNum, firstTime, isBot )
+
         et.G_Printf( "et_ClientConnect: [%d] [%d] [%d]\n", clientNum, firstTime, isBot )
+
<font color="green">-- called for every ClientConnect</font>
--     return "go away"
+
<font color="blue">function</font> et_ClientConnect( clientNum, firstTime, isBot )
         return nil
+
         et.G_Printf( <font color="darkblue">"et_ClientConnect: [%d] [%d] [%d]\n"</font>, clientNum, firstTime, isBot )
end
+
<font color="green">--     return "go away"</font>
 
+
         <font color="blue">return</font> <font color="blue">nil</font>
-- called for every ClientDisconnect
+
<font color="blue">end</font>
function et_ClientDisconnect( clientNum )
+
         et.G_Printf( "et_ClientDisconnect: [%d]\n", clientNum )
+
end
+
<font color="green">-- called for every ClientDisconnect</font>
 
+
<font color="blue">function</font> et_ClientDisconnect( clientNum )
-- called for every ClientBegin
+
         et.G_Printf( <font color="darkblue">"et_ClientDisconnect: [%d]\n"</font>, clientNum )
function et_ClientBegin( clientNum )
+
<font color="blue">end</font>
         et.G_Printf( "et_ClientBegin: [%d]\n", clientNum )
+
end
+
 
+
<font color="green">-- called for every ClientBegin</font>
-- called for every ClientUserinfoChanged
+
<font color="blue">function</font> et_ClientBegin( clientNum )
function et_ClientUserinfoChanged( clientNum )
+
         et.G_Printf( <font color="darkblue">"et_ClientBegin: [%d]\n"</font>, clientNum )
         et.G_Printf( "et_ClientUserinfoChanged: [%d] = [%s]\n", clientNum, et.trap_GetUserinfo( clientNum ) )
+
<font color="blue">end</font>
end
+
 
+
-- called for every trap_Printf
+
<font color="green">-- called for every ClientUserinfoChanged</font>
function et_Print( text )
+
<font color="blue">function</font> et_ClientUserinfoChanged( clientNum )
--     et.G_Printf( "et_Print [%s]", text )
+
         et.G_Printf( <font color="darkblue">"et_ClientUserinfoChanged: [%d] = [%s]\n"</font>, clientNum, et.trap_GetUserinfo( clientNum ) )
end
+
<font color="blue">end</font>
 +
 +
 +
<font color="green">-- called for every trap_Printf</font>
 +
<font color="blue">function</font> et_Print( text )
 +
<font color="green">--     et.G_Printf( "et_Print [%s]", text )</font>
 +
<font color="blue">end</font>
  
function et_ClientSay( cno, mode, text )
+
[[Category:ETPro]]
        et.G_Printf( "et_ClientSay [%d] [%d] [%s]\n", cno, mode, text )
+
        et.G_Say( cno, mode, text )
+
        return 1
+
end
+
</pre>
+

Latest revision as of 20:03, 28 August 2006

This code does nothing useful besides demonstrate and exercise the Lua Mod API

Real tutorials about ETPro Lua scripting can be found here.

-- printf wrapper
function et.G_Printf(...)
       et.G_Print(string.format(unpack(arg)))
end


-- test some of the supported etpro lua functions
function test_lua_functions()
       et.trap_Cvar_Set( "bla1", "bla2" )
       et.G_Printf( "sv_hostname [%s]\n", et.trap_Cvar_Get( "sv_hostname" ) )
       et.G_Printf( "configstring 1 [%s] \n", et.trap_GetConfigstring( 1 ) )
       et.trap_SetConfigstring( 4, "yadda test" )
       et.G_Printf( "configstring 4 [%s]\n", et.trap_GetConfigstring( 4 ) )
       et.trap_SendConsoleCommand( et.EXEC_APPEND, "cvarlist *charge*\n" )
       et.trap_SendServerCommand( -1, "print \"Yadda yadda\"" )
       et.G_Printf( "gentity[1022].classname = [%s]", et.gentity_get( 1022, "classname" ) )
end


-- called when game inits
function et_InitGame( levelTime, randomSeed, restart )
       et.G_Printf( "et_InitGame [%d] [%d] [%d]\n", levelTime, randomSeed, restart )
       et.RegisterModname( "bani qagame " .. et.FindSelf() )
--     test_lua_functions()
end


-- called every server frame
function et_RunFrame( levelTime )
       if math.mod( levelTime, 1000 ) == 0 then
--             et.G_Printf( et_RunFrame [%d]\n", levelTime )
       end
end


-- called for every clientcommand
-- return 1 if intercepted, 0 if passthrough
function et_ClientCommand( clientNum, cmd )
       et.G_Printf( "et_ClientCommand: [%d] [%s]\n", et.trap_Argc(), cmd )
       return 0
end


-- called for every consolecommand
-- return 1 if intercepted, 0 if passthrough
function et_ConsoleCommand()
       et.G_Printf( "et_ConsoleCommand: [%s] [%s]\n", et.trap_Argc(), et.trap_Argv(0) )
       if et.trap_Argv(0) == "listmods" then
               i = 1
               repeat
                       modname, signature = et.FindMod( i )
                       if modname and signature then
                               et.G_Printf( "vm slot [%d] name [%s] signature [%s]\n", i, modname, signature )
                               et.IPCSend( i, "hello" )
                       end
                       i = i + 1
               until modname == nil or signature == nil
               return 1
       end
       return 0
 end


-- called when we receive an IPC from another VM
function et_IPCReceive( vmnumber, message )
       et.G_Printf( "IPCReceive [%d] from [%d] message [%s]\n", et.FindSelf(), vmnumber, message )
end


-- called for every ClientConnect
function et_ClientConnect( clientNum, firstTime, isBot )
       et.G_Printf( "et_ClientConnect: [%d] [%d] [%d]\n", clientNum, firstTime, isBot )
--     return "go away"
       return nil
end


-- called for every ClientDisconnect
function et_ClientDisconnect( clientNum )
       et.G_Printf( "et_ClientDisconnect: [%d]\n", clientNum )
end


-- called for every ClientBegin
function et_ClientBegin( clientNum )
       et.G_Printf( "et_ClientBegin: [%d]\n", clientNum )
end


-- called for every ClientUserinfoChanged
function et_ClientUserinfoChanged( clientNum )
       et.G_Printf( "et_ClientUserinfoChanged: [%d] = [%s]\n", clientNum, et.trap_GetUserinfo( clientNum ) )
end


-- called for every trap_Printf
function et_Print( text )
--     et.G_Printf( "et_Print [%s]", text )
end