This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MentalHamburger's profile picture

Load GUI from folder.

Started by MentalHamburger, 28 October 2015 - 10:55 AM
MentalHamburger #1
Posted 28 October 2015 - 11:55 AM
Hey i'm having trouble loading GUI from a folder, in the code bellow i am looking for folders in the GUI folder then loading all the GUI in each of those folders. It doesn't seem to be working as it should, it should use the file name as the variable for the GUI but it doesn't seem to be doing that. Any help is appreciated, thanks!

code :


function loadGUI()
folder = fs.list(".Nova/GUI")

a = 1
s = 1
repeat
files = fs.list(".Nova/GUI/"..folder)
repeat
sleep(1)
files[a] = paintutils.loadImage(".Nova/GUI/"..folder.."/"..files[a])
a = a+1
until a == #files
until s == #folder
end
Bomb Bloke #2
Posted 28 October 2015 - 01:10 PM
You're loading your images into the "files" table, but you're discarding that table and replacing it with a new one when you get the next fs.list(".Nova/GUI/"..folder) - so only the last checked folder will be accounted for in the "final" files table..

The conditional checks on your repeat loops have got problems - if #files or #folder is 1, then the loops will never end (edit: scratch that, you never increment s, so that's still broken but in a different way!). If they're 0, you'll crash. If they're anything else, you'll skip the last indexes.

Some for loops would be a better bet, along with an extra table to hold the directory listings:

local function loadGUI()
	local folder, results = fs.list(".Nova/GUI"), {}
	
	for s = 1, #folder do
		local files = fs.list(".Nova/GUI/"..folder[s])
		
		for a = 1, #files do
			results[#results + 1] = paintutils.loadImage(".Nova/GUI/"..folder[s].."/"..files[a])
		end
	end
	
	return results
end

local images = loadGUI()
Edited on 28 October 2015 - 12:12 PM
Creator #3
Posted 28 October 2015 - 01:17 PM
for i,v in pairs(fs.list(dir) do
– i is the index and v is the value
end

Also works
MentalHamburger #4
Posted 28 October 2015 - 02:04 PM
Thanks for the response, ill try that out now :)/>/>

okay so i tried out the code, it runs fine but i still cant seem to draw the images. the images that are being "loaded" are all part of a boot animation that will be shown on startup (obviously), here is the code for displaying the images:



function boot(time)
  clear()
  local stage = 0
  repeat
	paintutils.drawImage(BootUpStage..stage,1,1)
sleep(time)
stage = stage+1
  until aqwerw == 23
end
Edited on 28 October 2015 - 02:57 PM
Creator #5
Posted 28 October 2015 - 04:20 PM
relpace the repeat loop with:


for stage=1,23 do
	 paintutils.drawImage("path"..tostring(stage),1,1)
end

Sleeps are annoying, so don't. just don't.
MentalHamburger #6
Posted 28 October 2015 - 04:29 PM
I need the sleep because the animation wouldn't be an animation :P/> , that isn't the problem anyways i made a test program and it wont draw any images "loaded" by the code, any ideas?
Creator #7
Posted 28 October 2015 - 04:45 PM
It actually would since the image needs time to be drawn. However, you can do as you like.
Exerro #8
Posted 28 October 2015 - 11:25 PM
Nah, you need sleeps in there, just really short ones. If you don't sleep, each image will have a couple of milliseconds on the screen before being drawn over. I'd recommend an interval time of between 0.05 and 0.2 seconds, depending on the detail of your animation.
Bomb Bloke #9
Posted 29 October 2015 - 12:16 AM
that isn't the problem anyways i made a test program and it wont draw any images "loaded" by the code, any ideas?

Explaining what DOES happen will generally get you much better feedback than making people guess.

I'd assume it has something to do with you passing random strings to paintutils.drawImage(), as opposed to the images that were loaded. For example, carrying on from my last code snippet, you might do:

.
.
.

local images = loadGUI()

local function boot(time)
  for i = 1, #images do  -- Or whatever range covers your boot sequence.
    term.clear()
    paintutils.drawImage(images[i], 1, 1)
    sleep(time)
  end
end

boot(0.5)