Quantcast
Channel: Active questions tagged require - Stack Overflow
Viewing all articles
Browse latest Browse all 166

Communicating global variables in Lua

$
0
0

I'm coding an inventory system in Computer Craft and I'd like to know if it would be possible to make a file edit variable that it doesn't exactly know about...

Here's my file structure:

root\    inv.lua    chestAPI\        inv_list.lua        inv_scan.lua

So my inv_scan.lua file scans all my inventories and stores the data in a 2D array.My inv_list.lua file is supposed to take that data and print it to a monitor.

Scanning all the inventories takes some time and I'd like to only do it once when you boot up the computer OR when you click refresh on the monitor to keep it optimized.

My listing script needs the scan script's variables to be able to print anything. The inv.lua script controls everything. It currently calls the scanning script when it gets executed and the it waits for a user input (because I'm going to add scripts to add or fetch items from the storage.

So I want the inv.lua to call the scan, get all the variables, shoot them to the listing script if the user wants to display the items, and then the listing will read them and print them.

I could do everything in one file but it would be horribly organized.

I tried using require(), but then the listing would always call the scan and it wouldn't be as optimized as I want it to be.

That's pretty much the only thing I tried that gave me a result. I learned Lua while making this project so I'm not that familiar with everything.


EDIT

While writing the minimal example, I fixed my problem. Here's how I did it.

Minimal example:

scan.lua

--some heavy scanningfunction scan()    data1 = 1 + 1    data2 = 4/2    return data1, data2end

print.lua

--printing the datafunction print(data1, data2)    print(data1)    print(data2)end

main.lua

local scanner = require("scan.lua")local printer = require("print.lua")--get the datadata1, data2 = scanner.scan()--wait for user input and then print itwhile true do    input = read()    if input == "print" then        printer.print(data1, data2)    endend

so with this, I can do the scan only when needed and I can print as much as I want.


Viewing all articles
Browse latest Browse all 166

Trending Articles