Page 1 of 4 123 ... LastLast
Results 1 to 10 of 40

Thread: File handling

  1. #1
    Sergeant EvoloZz's Avatar
    Join Date
    Sep 2012
    Location
    Helsinki, Finland
    Posts
    360
    Thanks
    314
    Thanked 167 Times in 120 Posts

    File handling

    I have a problem with a script for writing something on a file
    I gotta ask, how do i tell the game that on the 1. line of that file is something and on the 2. something else, so i could save 'em into variables
    If i have few things that needs to be wrote on that file, and on some event i need the game to read data from the file, how do i define that?

    Code:
    file = openFile(filename + ".txt", "read")
    if(file != -1)
    here i dont know what do to...
    closeFile(file);
    Hopefully someone could explain me...

  2. #2
    Private First Class
    Join Date
    Dec 2012
    Posts
    127
    Thanks
    132
    Thanked 114 Times in 63 Posts
    I'm not sure whether I understand, but that way writing events

    Code:
    f = openFile(filename + ".txt", "write");
    fprintln(f,self.name);
    fprintln(f,self.score);
    fprintln(f,self.etc);
    closefile(f);
    and that reading them

    Code:
    f = openFile(filename + ".txt", "read");
    freadln(f);
    self.name = int(fgetarg(f,0));
    self.score = int(fgetarg(f,1));
    self.etc =  int(fgetarg(f,2));
    closefile(f);

  3. The Following 4 Users Say Thank You to Jeplaa For This Useful Post:

    EvoloZz (9th February 2013),kung foo man (9th February 2013),pollo (10th February 2013),XLxp|HUNT3R|HD (22nd February 2013)

  4. #3
    Sergeant EvoloZz's Avatar
    Join Date
    Sep 2012
    Location
    Helsinki, Finland
    Posts
    360
    Thanks
    314
    Thanked 167 Times in 120 Posts
    if you could explain little bit better what "fgetarg" means it would be awesome, maybe getArgument or something? :P

  5. #4
    Private First Class
    Join Date
    Dec 2012
    Posts
    127
    Thanks
    132
    Thanked 114 Times in 63 Posts
    FGetArg( <filenum>, <arg> )
    Module: File

    Summary:
    Get a specific argument number from the current line


    Required Args:
    1: <filenum> The file number returned by openfile
    2: <arg> the argument number

  6. The Following 3 Users Say Thank You to Jeplaa For This Useful Post:

    EvoloZz (9th February 2013),kung foo man (9th February 2013),XLxp|HUNT3R|HD (22nd February 2013)

  7. #5
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    2,011
    Thanks
    2,102
    Thanked 1,084 Times in 753 Posts
    To get rid of the file-functions, i would suggest to use the mysql-functions: http://killtube.org/downloads/cod2/exampleserver/

    1) its faster
    2) its more powerful
    3) you can make a website about your cod2-server-stats

    At start you need to get used to it, because the mysql-api is not that self-explaining and also more complex then the file-functions, but you have fully working examples (mostly copy-paste). I can also assign you a cod2-server on the debug-server.
    timescale 0.01

  8. The Following User Says Thank You to kung foo man For This Useful Post:

    Jeplaa (9th February 2013)

  9. #6
    Sergeant EvoloZz's Avatar
    Join Date
    Sep 2012
    Location
    Helsinki, Finland
    Posts
    360
    Thanks
    314
    Thanked 167 Times in 120 Posts
    I should try that but it looks so hard :/
    But now i got weird error in a hud, saying that "type undefined is not a float", i called that hud right after loading the file so everything should work, but doesn't.

  10. The Following User Says Thank You to EvoloZz For This Useful Post:

    kung foo man (9th February 2013)

  11. #7
    Brigadier General
    Join Date
    Oct 2012
    Posts
    994
    Thanks
    20
    Thanked 588 Times in 388 Posts
    Quote Originally Posted by EvoloZz View Post
    I should try that but it looks so hard :/
    But now i got weird error in a hud, saying that "type undefined is not a float", i called that hud right after loading the file so everything should work, but doesn't.
    The golden rule of posting error messages on a modding forum: always post the full script and the exact error message you get. Anything else is too cryptic to even begin to know what is going on. We would just be guessing what it is and what is causing it.

    The file functions have nothing whatsoever to do with hud elements. So, if you are getting errors relating to hud elements, it suggests that a failed script is causing a hitch in the server, and things are being returned as undefined. Knowing where you are using the file functions, and what hud element script is being affected will help us to diagnose the problem. As such, please post both in full; show where they are being called from; what map and gametype are being run; and last but not least, post the full error message you are getting.
    Last edited by Tally; 9th February 2013 at 16:23.

  12. The Following 2 Users Say Thank You to Tally For This Useful Post:

    kung foo man (9th February 2013),serthy (9th February 2013)

  13. #8
    Sergeant EvoloZz's Avatar
    Join Date
    Sep 2012
    Location
    Helsinki, Finland
    Posts
    360
    Thanks
    314
    Thanked 167 Times in 120 Posts
    Code:
    type undefined is not a float: (file 'maps/mp/gametypes/_tdm.gsc', line 585)
      self.ratiohud setValue(self.ratio);
    And which script do you want to see? hud or file-handle?
    Last edited by EvoloZz; 9th February 2013 at 16:26.

  14. #9
    Brigadier General
    Join Date
    Oct 2012
    Posts
    994
    Thanks
    20
    Thanked 588 Times in 388 Posts
    Quote Originally Posted by EvoloZz View Post
    Code:
    type undefined is not a float: (file 'maps/mp/gametypes/_tdm.gsc', line 585)
      self.ratiohud setValue(self.ratio);
    And which script do you want to see? hud or file-handle?
    Both. Especially include where self.ratio is first initially defined.

  15. #10
    Sergeant EvoloZz's Avatar
    Join Date
    Sep 2012
    Location
    Helsinki, Finland
    Posts
    360
    Thanks
    314
    Thanked 167 Times in 120 Posts
    Well here's the file thing
    Code:
    load()
    {		
    	guid = self getGuid();
    	if(guid == 0)
    	    return;
    		
    	file = openFile(guid + ".txt", "read");
    	if(file != -1)
    	{
               freadln(file);
               self.ratio=int(fgetarg(file,0));
    	   closeFile(file);
    	}
    	
    	    else
    		{
                            self.ratio = 0;
    			self write();
    		}
    }
    
    write()
    {
    		
    	guid = self getGuid();
    	if(guid == 0)
    	    return;
    		
    	file = openFile(guid + ".txt", "write");
    	closeFile(file);
    	
    	file = openFile(guid + ".txt", "append");
    	fPrintln(file,self.ratio);
    	closeFile(file);
    }
    And the hud's like this
    Code:
    ratio()
    {
    	self endon("joined_spectators");
    	self endon("disconnect");
            self.ratio = self.score / self.deaths;
    	
    	if(!isDefined(self.ratiohud))
    	        self.ratiohud = newClientHudElem(self);
    		self.ratiohud.vertAlign = "fullscreen";
    		self.ratiohud.horzAlign = "fullscreen";
    		self.ratiohud.alignX = "left";
    		self.ratiohud.alignY = "middle";
    		self.ratiohud.x = 25;
    		self.ratiohud.y = 474;
    		self.ratiohud.sort = 1; 
    		self.ratiohud.alpha = 1;
    		self.ratiohud.fontScale = 0.8;
    		self.ratiohud.archived = true;
    		self.ratiohud.label = (game["ratio"]);
    		self.ratiohud setValue(self.ratio);
    }
    Hud is called at playerspawn, and load file is called at playerconnect

  16. The Following User Says Thank You to EvoloZz For This Useful Post:

    kung foo man (9th February 2013)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •