2

When require is called in testt.lua which is one of two files the return is movee and movee.lua.

movee are for the most part a class to be required, but should be able to accept to be called direct with parameter.

movee.lua

local lib = {} --this is class array

function lib.moveAround( ... )
    for i,direction in ipairs(arg) do
        print(direction)
    end
end

function lib.hello()
    print("Hello water jump")
end

lib.moveAround(...)

return lib

testt.la

local move = require("movee")

Expected result is not to call lib.moveAround or print of file name when require is called.

3 Answers 3

2

Your expectations are incorrect. Lua, and most scripting languages for that matter, does not recognize much of a distinction between including a module and executing the Lua file which provides that module. Every function statement is a statement whose execution creates a function object. Until those statements are executed, those functions don't exist. Same goes for your local lib = {}. And so on.

Now, if you want to make a distinction between when a user tries to require your script as a module and when a user tries to execute your script on the command line (or via just loadfile or similar), then I would suggest doing the following.

Check the number of arguments the script was given. If no arguments were given, then your script was probably required, so don't do the stuff you don't want to do when the user requires your script:

local nargs = select("#", ...)
if(nargs > 0) then
  lib.moveAround(...)
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for responce. Did try you'r solution but did not work. The reason is that movee and move.lua count as element in the "..." and will act the same for the unwanted and wated result. But did find a solution as shown under 'edit' in main post.
1

Solved by replacing

lib.moveAround(...)

with

local argument = {...}
if argument[1] ~= "movee" and argument[2] ~= "movee" then
    lib.moveAround(...)
end

Comments

0
require("movee")

will execute the code within movee.lua

lib.moveAround(...)

is part of that code. Hence if you require "movee" you call lib.moveAround

If the expected result is not to call it, remove that line from your code or don't require that file.

1 Comment

Thank you for your responce. That is not a option for me yeat as i'm to new to know a other path. But did find a end to my need : )

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.