[Lua] Is this possible

Started by
1 comment, last by MrFlibble 19 years, 2 months ago
I've just started looking into using Lua for a small project that I'm doing but I've run into a problem, either that or I'm not understanding how to use Lua correctly. I want to load a lua script into memory - without running it. i.e.

a = 10;
function AddA( )
    a = a + 10;
    print( a );
end

I then want to be able to run code from another Lua file/string which calls this function. Unfortunatley when I try this - I get the error: [string "AddA();"]:1: attempt to call global `AddA' (a nil value) Ideas? or am I being silly?
MrF.--Code..reboot..code..reboot..sigh!
Advertisement
Assuming that you named the file "AddA.lua", add the line:

require "AddA"


to the top of your calling code. Additionally, code in placed in the *global* space will be run when the require statement executed, So 'a' will equal 10. You should then be able to call AddA() worry-free.
"This I Command" - Serpentor, Ruler of C.O.B.R.A

I didn't actually want the second file to have to 'require' the first one.

After looking into this a bit more I found that if I use the lua_pcall function
it does what I need. It sets up the variables & functions from the first file
enabling me to use the second file to call global functions from the first.

cheers
MrF.--Code..reboot..code..reboot..sigh!

This topic is closed to new replies.

Advertisement