JoeBullet Posted April 11, 2010 Share Posted April 11, 2010 LUA offers some nice functions for including LUA file in LUA file( loadfile, dofile, require ). They simply don't work when tested in MTA environment. All three returns false or nil. Example: main.lua require "other" other.lua -- code Even putting module("other", package.seeall) in other.lua doesn't help, also require and dofile fails too. I searched forum and found that dofile() ISN'T SUPPORTED in LUA? I really need modular division between files because it helps organizing source, and also I used to it from C and C++. BtW. I read lua online book. Link to comment
Mr.Hankey Posted April 11, 2010 Share Posted April 11, 2010 The dofile function is disabled in MTA. All scripts in one resource are in the same luaVM so you can just do normal function calls across the different script files. Link to comment
JoeBullet Posted April 11, 2010 Author Share Posted April 11, 2010 Yeah, I tried that too, but doesn't work. It's says something like "global function is nil". I defined both files in meta.xml Link to comment
Gamesnert Posted April 11, 2010 Share Posted April 11, 2010 This should work just fine: <meta> <script src="a.lua"/> <script src="b.lua"/> </meta> -- a.lua function boo() print("boo") end -- b.lua boo() Note: It won't work if the scripts are the other way around in the meta.xml, because "boo" would be called before it would be defined. Link to comment
JoeBullet Posted April 11, 2010 Author Share Posted April 11, 2010 I just tried that, fails too. Exact error(same as before): * attempt to call global 'function_name'(nil value) * Link to comment
Gamesnert Posted April 11, 2010 Share Posted April 11, 2010 Exact error(same as before): * attempt to call global 'function_name'(nil value) * For me it works fine. [18:00:43] start: Requested by Console[18:00:43] Starting test boo [18:00:43] start: Resource 'test' started Can you post the exact meta.xml + script files you're using? Link to comment
JoeBullet Posted April 11, 2010 Author Share Posted April 11, 2010 meta.xml <meta> <info author="JoeBullet" type="gamemode" name="jMTARP" description="RP gamemode" /> <script src="language.lua" /> <script src="main.lua" /> </meta> main.lua function Callback_Join() outputChatBox(getTextByLang("Hrvatski", "PLAYER_JOIN")); outputChatBox(getTextByLang("English", "PLAYER_JOIN")); end addEventHandler("onPlayerJoin", getRootElement(), Callback_Join); language.lua function getTextByLang(language, alias) if type(language) ~= "string" then outputServerLog("getTextByLang's requires string"); language = tostring(language); end if type(alias) ~= "string" then outputServerLog("getTextByLang's requires string"); alias = tostring(alias); end local languages_node = xmlLoadFile("languages.xml"); -- load file, gets "languages" node if(languages_node) then for _, langNode in ipairs(xmlNodeGetChildren(languages_node)) do -- get "language" node local langName = xmlNodeGetAttribute(langNode, "name"); if(langName == language) then for _, text in ipairs(xmlNodeGetChildren(langNode)) do local xmlAlias = xmlNodeGetAttribute(text, "alias"); if(xmlAlias == alias) then return xmlNodeGetAttribute(text, "str"); end end end end xmlUnloadFile(languages_node); else outputServerLog('xmlLoadFile("languages.xml") failed'); end outputServerLog("FATAL ERROR: Alias " .. alias .. " not found for language " .. language); return "Please be patient, error is reported and will be solved as soon as possible. Thanks!"; end EDIT: Seems like problem self-repaired, I just started server and everything worked. Thanks for help (Please lock topic) Link to comment
Jumba' Posted April 11, 2010 Share Posted April 11, 2010 wouldn't it be faster to load the languages into a table when the resource starts? I imagine it'd be faster than opening/closing an xml file. Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now