HTML Sugarcube checking number of files(pictures) in a folder

chiekat

New Member
May 23, 2017
14
14
I'm trying to streamline the process when modding Sugarcube games.

Say a folder has 10 image files in it with file naming from 1 to 10.
As the moment I manually set a variable to record how many pics are in the designated folder.
I'd like to avoid having to modify the code everytime a file is added.
Is there a way for Twine code to count the number of image files in a folder instead?

Further down the line, if I place a set of pictures in the folder and naming them like following
Scene1-1, Scene1-2, Scene1-3,Scene1-4, Scene1-5,
Scene2-1, Scene2-2
Is it possible to run a repeatable code to check how many files are under Scene1-x, and how many are under Scene2-x?

Thanks
 

guest1492

Member
Apr 28, 2018
320
268
No, you can't. Twine (and SugarCube) is based off of javascript and JS does not allow access to files stored on a client's computer, including checking whether or not a file exists.

I suppose it's possible if you are hosting the game on a server though and require players to connect to the internet to retrieve pictures.
 

chiekat

New Member
May 23, 2017
14
14
Thank you, my experience with Twine only started last weekend. Sorry for not more clear with question.
I'm only trying to work with game html file and source images in the save base directory.

My logic is by running a loop with loading each image with increment name, checking if the image exist.
If the image is successfully loaded, I'll add to a counter. And if the image fail to load, the loop will stop.
Thus in the end, the counter with record the number of images loaded successful.

I've read though HiEv's imageExists() function, but failed to use it in a repeating process.
I've got 0 knowledge with Javascript, so I only edit the Twine code to do a look of checking.
Once the first image is loaded, even if the subsequent image failed to load, it will continue to return local image url as if it was successfully.
 

guest1492

Member
Apr 28, 2018
320
268
For you to use HiEv's imageExists() function, you have to actually load an image. That means if you want to count how many images you have per scene, you have to actually load all the images (ie have them all displayed on the page) before you can actually count them. Is that what you want? If so, why do you even need the counter? You would have all the images loaded already anyway.
 

hiya02

Member
Oct 14, 2019
169
95
Looping all the (possibly thousands) files in numerous folders is very inefficient and slow.
Maybe create a tool that counts the numbers of files in each scene/folder etc and updates the values to the source file/properties. After adding or removimg files you'd need to run the tool.