Jump to content

Including LUA file in LUA file


JoeBullet

Recommended Posts

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

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
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

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 :wink:

(Please lock topic)

Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...