Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestate/data/lua/LuaStateInit.lua @ 6623

Last change on this file since 6623 was 6623, checked in by rgrieder, 14 years ago

The lua "print" function was previously stored in "debug" —> changed that to "original_print".
Please use logMessage(level, message)
or its shortcut cout(level, message)

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1-- Note: luaState is a pointer to the LuaState instance that created this lua state
2
3-- Redirect print to the C++ print function
4original_print = print
5print = function(s)
6  luaState:luaPrint(s)
7end
8
9-- Create function to log text like COUT, but always prints a line!
10logMessage = function(level, message)
11  luaState:luaLog(level, message)
12end
13cout = logMessage
14
15-- Redirect dofile in order to load with the resource manager
16original_dofile = dofile
17dofile = function(filename)
18  luaState:doFile(filename)
19  -- Required because the C++ function cannot return whatever might be on the stack
20  return LuaStateReturnValue -- C-injected global variable
21end
22doFile = dofile
23
24-- Create includeFile function that preparses the file according
25-- to a function provided to the LuaState constructor (in C++)
26include = function(filename)
27  luaState:includeFile(filename)
28  -- Required because the C++ function cannot return whatever might be on the stack
29  return LuaStateReturnValue -- C-injected global variable
30end
31
32-- Replace require function with almost similar behaviour
33-- The loaded modules are then stored with their names (where name has no .lua extension)
34-- Furthermore the ".lua" extension is appended to the moduleName parameter when looking for the file
35original_require = require
36require = function(moduleName)
37  if not luaState:fileExists(moduleName .. ".lua") then
38    logMessage(2, "Warning: Lua function require() could not find file '" .. moduleName .. ".lua' ")
39    return nil
40  end
41  if not _LOADED then
42    _LOADED = {}
43  end
44  if not _LOADED[moduleName] then
45    -- save old value
46    _REQUIREDNAME_OLD = _REQUIREDNAME
47    _REQUIREDNAME = moduleName
48    luaState:doFile(moduleName .. ".lua")
49    _LOADED[moduleName] = LuaStateReturnValue or true
50    -- restore old value
51    _REQUIREDNAME = _REQUIREDNAME_OLD
52  end
53  return _LOADED[moduleName]
54end
55
56-- Convenience function for console commands
57orxonox.execute = function(command)
58  orxonox.CommandExecutor:execute(command)
59end
60
61-- Convenience function for config values
62orxonox.getConfig = function(section, entry)
63  return orxonox.SettingsConfigFile:getInstance():getConfig(section, entry)
64end
65orxonox.config = function(section, entry, value)
66  return orxonox.SettingsConfigFile:getInstance():config(section, entry, value)
67end
68orxonox.tconfig = function(section, entry, value)
69  return orxonox.SettingsConfigFile:getInstance():tconfig(section, entry, value)
70end
Note: See TracBrowser for help on using the repository browser.