Have you had programming experience prior to Lua? I notice that you used terms some programming linguistic terms, which I don't see many new programmers do.
Whether you've had prior experience or not though, I think that overall your code is excellent! In fact, the only real suggestions that I do have are just semantics.
Here's a few things that I noticed:
- The first thing I noticed is that you're not localizing functions. Not a huge biggy, but unless you want those functions clogging up your global environment then you should probably do some localization.
- The second thing that immediately jumped out to me was coroutines. I don't mind using coroutines when it's appropriate, but I think it's a bit misplaced here.
–Alternative:
--Currently, I see that you are using sleep(60), which of course blocks input unless you use coroutines. But what if we use timers instead...
--Example of timer/input usage
local function sleepInput(time)
local timerID = os.startTimer(time)
while true do
local event = {os.pullEvent()}
if event[1] == "timer" and event[2] == timerID then
return true, "Sleep"
elseif event[1] == "key" and event[2] == keys.enter then
return true, "Key"
end
end
end
Why is this at all preferable? Well to be honest, it is only my opinion that it simplifies things a good deal in comparison to using coroutines (not that coroutines are particularly complicated, but it keeps relevant code in a singular location/function as opposed to splitting the code amongst several functions).
- Dangling variables:
I see that you are familiar with tables from the Save() function, but you don't initially store your variables in a table for convenient saving. Why not take all those variables and stick them into a variable table rather than having to manually do it during the save function? This would also simplify restoration of the variables due to the fact that you wouldn't have to type out each variable again.
Honestly, I don't really have anything else. Your Inventory function is fine in my eyes, but I suppose you could quicken it a bit by having it return immediately upon discovering that there is in fact enough space as opposed to going through the entire inventory twice. No need to actually be aware of exactly how much space is left, just that you have enough to do the job.
Nice work :)/>
Edit: One thing I do to keep my programs a little neater is to store functions and variables inside of tables like so:
local vars = {
importantVars = {5,4,3,2,1};
saveFile = "hello.txt";
save = function(self)
local f = fs.open(self.saveFile, "w")
f.write(textutils.serialize(self.importantVars))
f.close()
end;
}
--Now just to call it
vars.importantVars[1] = 3 --Set a variable to a different value
vars:save() --Save it
I don't know if you want to use this, but I find that it helps enormously when I to write longer programs.