Lua calls methods across files

Lua calls methods across files

Create a lua file to be called

Create a raspberryfile named with a suffix of .lua, and put the following code in it

Raspberrypi={
    
    };

Runstate=false;

Sayhello=print("hello raspberry ready");

function Raspberrypi.Run()

print("raspberrypi running!")

Runstate=true;

end

 

function Raspberrypi.Closse()

print("raspberrypi Close!")

Runstate=false;

end


function Raspberrypi.Getstate()

if Runstate ==true then

print("raspberrypi running");

else

print("raspberrypi closeing");

end

end
return Raspberrypi;


Create a file for calling

Create a mainfile named call the above method

require("raspberry");--加载,

print(Raspberrypi:Run());

print(Raspberrypi:Getstate());

print(Raspberrypi:Closse());

run

Then run main.lua
print out

hello raspberry ready
raspberrypi running!
raspberrypi running
raspberrypi Close!

Guess you like

Origin blog.csdn.net/GoodCooking/article/details/129409454