We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi I am currently working on a game project where I use a txt file that contains the sprite for an object, the object name, and some properties based on the object (ex: player.png, player, player's health). While the first two parts are not a problem to get working into the game, I cannot load the sprites due to only being able to run preload once which is spent on LoadStrings and so I cannot also load the images due to not being able to read the strings. Is there a way to force preload to wait until the LoadStrings function is done so that I can load the images that the txt file indicates?
Code Sample:
funtion preload(){
mainFile = loadStrings("assets/MainFile.txt"); //load the text file
//need it to wait here until loading strings finishes
for (var i = 3; i < mainFile.length; i++) {
var newCharacterStr = splitTokens(mainFile[i], ",");
var newImage = loadImage(newCharacterStr[0]);
}
}
MainFile.txt:
player.png,player,5
Answers
preload()? Is this about p5.js? If so, please click "Edit" and change the category accordingly!
Also you should post a sample of both your code and ".txt" file.
Your code sample says
funtion preload()
. I'm guessing that should befunction preload()
and that you should be getting warnings about that...? If you don't fix it, nothing else will work.Disclaimer: Given your "MainFile.txt" is actually a ".csv" file type, my sketch version is using loadTable() in place of loadStrings():
Obviously you can stay on loadStrings(). It works as well; as long as you modify the code to deal w/ the returning loaded object's String[] instead of p5.Table:
Also, I've changed the loaded FILENAME to "players.csv". :\">
Both loadStrings() & loadTable() (and all the other p5.js' IO API functions) got a success parameter where we pass a
function
callback; which in turn is invoked when the loading is completed.The interesting thing is that we can use it even inside preload()!
My solution is about chaining the loadImage() w/ the loadTable()'s success parameter callback. *-:)
Apparently the sketch continues to block the execution until all images are loaded! \m/
Once the execution reaches setup(),
console.log(players.length, entries);
displays10 10
. It means players.length got the same number of loaded entries; that is,table.getRowCount();
.I guess if it wasn't delaying, it'd display players.length w/ a lower count than entries, right? =P~
Or maybe not! It may happen those p5.Image objects haven't finished loading completely. :-SS
Here's the "players.csv" file. You can grab it at https://ThimbleProjects.org/gotoloop/96556/players.csv:
Also the "index.html" minimum template file:
And finally the "sketch.js" file. You can watch it online at the link below: :-bd
https://ThimbleProjects.org/gotoloop/96556/
I solved the problem on my own. While I appreciate the attempt to help, please don't make assumptions that other filetypes would work better for what I'm doing.
Since my only problem was the sprite not being able to preload due to how preload works I didn't feel it necessary to mention the other parts of the txt file I have that work perfectly fine and do not work well at all when introduced through a csv format.
If I stopped doing such, not more than 10% folks here I would be able to help out at all! [-(
And I was very clear that you'd be able to keep using loadStrings() instead of loadTable() w/ small modifications. :-@
It'd be nice if you explained how you had solved it by yourself; so others can learn too. O:-)