Help streamlining loadVars code

Non-platform specific questions.

Help streamlining loadVars code

Postby VertigoKeyz » Mon Oct 10, 2011 6:12 pm

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?
VertigoKeyz
 
Posts: 12
Joined: Thu Jun 02, 2011 4:12 pm
Score: 0 Give a positive score

Re: Help streamlining loadVars code

Postby lcl » Mon Oct 10, 2011 7:15 pm

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
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help streamlining loadVars code

Postby VertigoKeyz » Mon Oct 10, 2011 7:47 pm

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?
VertigoKeyz
 
Posts: 12
Joined: Thu Jun 02, 2011 4:12 pm
Score: 0 Give a positive score

Re: Help streamlining loadVars code

Postby skydereign » Mon Oct 10, 2011 8:58 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Help streamlining loadVars code

Postby lcl » Mon Oct 10, 2011 9:14 pm

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 :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Help streamlining loadVars code

Postby VertigoKeyz » Tue Oct 11, 2011 4:24 pm

Thanks! That makes my code much better!
VertigoKeyz
 
Posts: 12
Joined: Thu Jun 02, 2011 4:12 pm
Score: 0 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest