Changes between Initial Version and Version 1 of LuaPlugin


Ignore:
Timestamp:
09/27/07 21:50:45 (17 years ago)
Author:
art
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • LuaPlugin

    v1 v1  
     1= Plugin Manual = 
     2 
     3== What's this == 
     4This is a simple description of the LuaPlugin provided with the engine. THe reader should be familar with the lua language. Visit [http://www.lua.org] to get more information about the lua scripting language. 
     5 
     6 
     7== Load plugin == 
     8 
     9Lua is a simple and powerfull scripting language developed mainly as embedded scripting langauge in mind. In our engine we provide a plugin to include lua scripting into your application.  
     10 
     11For a simple use of lua language please ensure, that luaScripting.so (Unix) or luaScripting.dll (Windows) is properly installed, for example under the "data/plugins/" path. To load the lugin you can just call: 
     12{{{ 
     13    Engine::sResourceManager()->loadResource("luaPlugin", "Plugins", "data/plugins/luaScripting", "Plugin"); 
     14}}} 
     15 
     16This will load the resource of type "Plugin" with the name "luaPlugin" into the resource group "Plugins" from the file "data/plugins/luaScripting" (here you do not have to specify the file extensions, since the engine will add a proper extensions, based on your operating system). 
     17 
     18When the plugin is loaded it will register itself by the engine and will provide an interface for loading and handling of *.lua files. It will also provide a new resource types "Lua" and "LuaScript", so that any resources of this type will be handled by the plugin.  
     19 
     20== Use userdefined lua_state == 
     21 
     22In the case you want to use your own lua state, so that all your manually defined functions are also accessable from the lua plugin, you can pass your lua state as a parameter to the plugin. This can be achieved in the following way: 
     23{{{ 
     24    // create my own lua state 
     25    mLuaState = luaL_newstate(); 
     26    if (mLuaState) 
     27    { 
     28        // open default lua libs  
     29        luaL_openlibs(mLuaState); 
     30 
     31        // now load plugin and pass the state as parameter 
     32        PropertyList _property; 
     33        _property["luastate"].setUserData(mLuaState); 
     34        Engine::sResourceManager()->loadResource("luaPlugin", "Plugins", "data/plugins/luaScripting", "Plugin", &_property); 
     35    } 
     36}}} 
     37 
     38This kind of code is usefull if you want to connect the complete lua environment by the luabind library, which is currently not provided by the plugin. To find more information about the luabind, visit their homepage at [http://www.rasterbar.com/products/luabind.html] 
     39 
     40== Default lua script == 
     41 
     42To separate different scripts you must be familar with handling of lua environments. 
     43I propose to load following lua script as first, which do this job for you. It provides a simple function enabling loading of scripts from c-strings. Also a simple printf function is provided.   
     44{{{ 
     45-- ----------------------------------------------------------- 
     46-- 
     47-- Setup the lua environment in the way we need for scripting. 
     48-- This script must be loaded and executed as first script 
     49-- before other scripts are loaded. The script will define 
     50-- a new function called scriptload() which load new scripts 
     51-- from sctrings defined by the c-programm. 
     52-- 
     53-- Loaded scripts get it's own local environment, so they 
     54-- can not overwrite global functions with the same name. 
     55-- 
     56-- ----------------------------------------------------------- 
     57 
     58-- ----------------------------------------------------------- 
     59-- Global function allowing us formatted print on the console 
     60-- ----------------------------------------------------------- 
     61function printf(...) io.write(string.format(unpack(arg))) end 
     62 
     63-- Environment to be used for all scripts 
     64local GLOBAL = { 
     65         
     66        -- Define functions usefull for all other scripts 
     67        printf = printf 
     68         
     69} 
     70 
     71-- Connect our environment with the global one 
     72setmetatable (GLOBAL, { __index = _G}) 
     73 
     74 
     75-- ----------------------------------------------------------- 
     76-- We define new function which is now capable to load 
     77-- new scripts from strings and connect them to the environment 
     78-- ----------------------------------------------------------- 
     79function loadstringscript (scriptstr) 
     80        local scriptenv = {} 
     81         
     82        setmetatable (scriptenv, {__index=GLOBAL}) 
     83        local chunk  = loadstring (scriptstr) 
     84        setfenv (chunk, scriptenv) 
     85                 
     86        chunk () 
     87        return scriptenv 
     88end 
     89 
     90 
     91}}}