Page 1 of 1

Help streamlining loadVars code

PostPosted: Mon Oct 10, 2011 6:12 pm
by VertigoKeyz
I am looking to have the computer come up with a random number and load a file based on that number. However I will have between 500 and 1000 files to choose from. Here is what my code looks like...
Code: Select all
if (randcatloader == 1) {
    loadVars("qfile_001.jqf", "Category");
    loadVars("qfile_001.jqf", "Question1");
    loadVars("qfile_001.jqf", "Question2");
    loadVars("qfile_001.jqf", "Question3");
    loadVars("qfile_001.jqf", "Question4");
    loadVars("qfile_001.jqf", "Question5");
} else if (randcatloader == 2) {
    loadVars("qfile_002.jqf", "Category");
    loadVars("qfile_002.jqf", "Question1");
    loadVars("qfile_002.jqf", "Question2");
    loadVars("qfile_002.jqf", "Question3");
    loadVars("qfile_002.jqf", "Question4");
    loadVars("qfile_002.jqf", "Question5");
.
.
.
} else if (randcatloader == 999) {
    loadVars("qfile_999.jqf", "Category");
    loadVars("qfile_999.jqf", "Question1");
    loadVars("qfile_999.jqf", "Question2");
    loadVars("qfile_999.jqf", "Question3");
    loadVars("qfile_999.jqf", "Question4");
    loadVars("qfile_999.jqf", "Question5");
}

Done like this, it comes out to be about 6,000 lines long and I need it to run 12 times per game. Can anyone let me know of a more streamlined way of doing this using arrays or concatenation or anything?

Re: Help streamlining loadVars code

PostPosted: Mon Oct 10, 2011 7:15 pm
by lcl
Here's how I'd do it.
Code: Select all
char FileName[200]; //create string for file path
sprintf(FileName, "qfile_%03i.jqf", randcatloader); //set file path to be qfile_(number).jqf

loadVars(FileName, "Category");
loadVars(FileName, "Question1");
loadVars(FileName, "Question2");
loadVars(FileName, "Question3");
loadVars(FileName, "Question4");
loadVars(FileName, "Question5");


This code will work if your randcatloader variable is int.
That is all you need to do it. :D

Re: Help streamlining loadVars code

PostPosted: Mon Oct 10, 2011 7:47 pm
by VertigoKeyz
lcl wrote:Here's how I'd do it.
Code: Select all
char FileName[200]; //create string for file path
sprintf(FileName, "qfile_%03i.jqf", randcatloader); //set file path to be qfile_(number).jqf

loadVars(FileName, "Category");
loadVars(FileName, "Question1");
loadVars(FileName, "Question2");
loadVars(FileName, "Question3");
loadVars(FileName, "Question4");
loadVars(FileName, "Question5");


This code will work if your randcatloader variable is int.
That is all you need to do it. :D


That looks great but how did you come up with the "%03i" part?

Re: Help streamlining loadVars code

PostPosted: Mon Oct 10, 2011 8:58 pm
by skydereign
That's how you format strings with the printf functions. This one happens to copy the format into the string. But, it allows you to input other variables in the string. sprintf detects that you want to put an integer in their by the %03i. You can input other variables like chars, strings, doubles, by using %c, %s, %lf. But, for integers it is %i or %d. The 03 means that the variable will take up at least 3 spaces and the 0 means any spaces not taken by the value of the integer will be taken up by 0. So, having %03i and the variable equal to 1, will add "001". Essentially it does exactly what you want.

Re: Help streamlining loadVars code

PostPosted: Mon Oct 10, 2011 9:14 pm
by lcl
VertigoKeyz wrote:That looks great but how did you come up with the "%03i" part?

Oh, sorry for not telling that, I was in a hurry.
But yeah, skydereign explained that to you quite perfectly :)

Re: Help streamlining loadVars code

PostPosted: Tue Oct 11, 2011 4:24 pm
by VertigoKeyz
Thanks! That makes my code much better!