Forum

Tutorial: Unlimited Parms! (well, sort of)

Discuss programming in the QuakeC language.

Moderator: InsideQC Admins

Tutorial: Unlimited Parms! (well, sort of)

Postby behind_you » Mon Jun 20, 2011 8:11 am

Ok I am about to explain how to write your floats before loading a new level. Then after you spawn in the new level, you will read the floats that you just wrote. So in the end, this allows for infinite parms!! Cool, huh?

In this tutorial I show how to save 3 floats, your angles(a vector), and your classname(a string). I know saving classname is pointless, but i just wanted to demonstrate how to save the 3 data types.

Okay, the only file you need to change is client.qc. Open it and go to the function named trigger_changelevel. In that function, you will find the line that says;

Code: Select all
self.touch = changelevel_touch;


This line makes it so that whenever you touch the trigger called trigger_changelevel, you will exit this level and load the next one. We don't want to do this right away. We need to write your floats first, right? So erase that line, and replace it with this one:
Code: Select all
self.touch = writefirst;   //function that we will make soon


Now, above the function you just changed, paste this chunk of code:
Code: Select all
function() writefirst =
{
   local float   writeparms;
   local string   writethis;
      
   writeparms = fopen ("THIS IS THE FILE NAME", FILE_WRITE);   //open what file to use in write mode
   if (writeparms < 0)                     //if you didn't find the file that you needed
      {
      objerror("Error: file not found\n");   //error message shows up
      localcmd("disconnect\n");
      return;
      }

   writethis = ftos(other.EXAMPLEFLOAT1);      //prepare what to write in the file
   fputs (writeparms, writethis);            //line 1 of the file (EXAMPLEFLOAT1) (write your stats to file by using ftos (float to string)
   fputs (writeparms, "\n");               //make sure you do this after every line, it's the ENTER button

   writethis = ftos(other.EXAMPLEFLOAT2);      //prepare the next thing to write in the file(EXAMPLEFLOAT2) since you finished writing the last float
   fputs (writeparms, writethis);            //line 2 - EXAMPLEFLOAT2
   fputs (writeparms, "\n");               //dont forget this

   writethis = ftos(other.EXAMPLEFLOAT3);      //prepare the next thing to write
   fputs (writeparms, writethis);            //line 3 - EXAMPLEFLOAT3
   fputs (writeparms, "\n");               //and again


   writethis = vtos(other.angles);      //prepare the next thing to write
   fputs (writeparms, writethis);            //line 4 - angles

   fputs (writeparms, "\n");               //and again

   writethis = other.classname;      //prepare the next thing to write
   fputs (writeparms, writethis);            //line 4 - classname

   fputs (writeparms, "\n");               //and again

   fclose (writeparms);                  //close the file or else you will have errors

   changelevel_touch();                  //now change the level
};

Look at the code. Where it says 'THIS IS THE FILE NAME' replace that with the name of the file you want to use to save your floats and stuff. Create a folder called 'data' inside your game folder, then create a file with the name that you replaced 'THIS IS THE FILE NAME' with. If you want to learn how the code works, read the comments I placed for you :D. You can make it save as many stats as you want

Ok, we just set up things so that everytime you touch the level changing trigger, you save your statistics. Now we will make it so that whenever you spawn at the next level, you get the floats you saved.

Go to the function called DecodeLevelParms. This is called whenever you spawn at the next level. This is exactly where you want to update your stats with the ones you just wrote.

Before:
Code: Select all
self.items = parm1;
self.health = parm2;


Add:
Code: Select all
   local float readparms;
   local string readtext;

        if (!deathmatch && world.model != "maps/start.bsp")//if you are not playing multiplayer, and are not starting a new game
        {
   readparms = fopen ("THIS IS THE FILE NAME", FILE_READ);   //open what file to use (THIS TIME IN READ MODE)

   if (readparms < 0)//if file not found
      {
      objerror("Error: file not found.\n");//error
      localcmd("disconnect");
      return;
      }
                                    //now to keep your stats, read the stuff you just wrote
   readtext = fgets(readparms);            //read the text
   self.EXAMPLEFLOAT1 = stof(readtext);      //turn the text into the float u want (by using stof: string to float)

   readtext = fgets(readparms);            //read second line
   self.EXAMPLEFLOAT2 = stof(readtext);      //turn the text into the float u want (by using stof: string to float)

   readtext = fgets(readparms);            //third line
   self.EXAMPLEFLOAT3 = stof(readtext);      //turn the text into the float u want (by using stof: string to float)

   readtext = fgets(readparms);            //third line
   self.angles = stov(readtext);      //turn the text into the vector u want (by using stov: string to vector)

   readtext = fgets(readparms);            //third line
   self.classname = readtext);      //dont need to change text, since you are getting in string format to begin with

   fclose (readparms);                     //close the file or else you will have errors
       }



Now replace all the EXAMPLEFLOATS with the floats you want to save. You can use this to save as many stats as you want. just remember that you must read the stats in the same order you wrote them.

And that's it!!! Follow this pattern with any and all floats and vectors and strings you want to save and you'll do it!
Last edited by behind_you on Tue Jun 21, 2011 7:32 am, edited 1 time in total.
User avatar
behind_you
 
Posts: 237
Joined: Sat Feb 05, 2011 6:57 am
Location: Tripoli, Libya

Postby Spike » Mon Jun 20, 2011 11:44 am

how do you prevent new games from getting old parms?
Spike
 
Posts: 2892
Joined: Fri Nov 05, 2004 3:12 am
Location: UK

Postby behind_you » Tue Jun 21, 2011 8:06 am

Spike wrote:how do you prevent new games from getting old parms?


just fixed that! thanks!
User avatar
behind_you
 
Posts: 237
Joined: Sat Feb 05, 2011 6:57 am
Location: Tripoli, Libya


Return to QuakeC Programming

Who is online

Users browsing this forum: No registered users and 1 guest