2

I'm working with a program that has standard Lua 5.1 embedded and am trying to write a module I can call functions from but have had no avail.

The current environment is quite picky, and if I make mistakes the scripts will break but will not get any errors, so here we go:

I have something like this (inside moduletests.lua):

local _ = {}

function _.prints()
    HUD.AddUpdateBoxText("Hello World!", 200) --Equivalent to print()
end

I would then attempt to require this and call it with:

mts = require 'moduletests' --seems to cause no issues
mts.prints() --breaks the scripts

Normally if the require function is incorrect, the scripts will break. The scripts work fine with the require, so I assume it is correct, but on any attempt to call the print function it will break.

On the other hand, I have another lua module installed and have been able to successfully require it and call a function and it is what I modeled my module after. This is the module's code.

Here is how I used it:

moses = require 'moses' --Works

local bok = moses.isInteger(6)
HUD.AddUpdateBoxText(tostring(bok), 700); --Works, outputs "true"

And this worked fine, perfectly as intended. Can someone tell me what is different or wrong with my module, or have any suggestions on how I could make a better functional version of this?

Thank you to everyone, I sincerely appreciate the help! Much appreciated!

1 Answer 1

2

In Lua modules, you have to return something. The reason your code isn't working is because you are trying to call a method from whatever is returned by the module, but since nothing is being returned, an error can only be expected. Try returning the table:

local ar = {}
function ar.prints()
    HUD.AddUpdateBoxText("Hello World!", 200) --Equivalent to print()
end
return ar
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! This answered my question perfectly, was very easy to understand and very concise! Thank you, I really do appreciate the help! Makes perfect sense. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.