[ZScript] File Reading

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!
Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

[ZScript] File Reading

Postby Nero » Sat Dec 16, 2017 4:32 pm

So I'm trying to read a specific lump in a pk3 and it's not working. I am on the Dec 15th build so that's not the issue, unless it's a bug, but I'm going to assume human error for now. The file does exist, so I'm assuming I'm doing something wrong in the code. Anyone have an example of reading files in a pk3? Especially using Wads.CheckNumForFullName()? Or can you see what I'm doing wrong from the code below? Thanks :D

Note/Edit: I just realized I was still trying to use Wads.FindLump() in my demo code. However when changing to CheckNumForFullName(), the game stops responding and seems to consume lots of memory, after force quitting via Task Manager. I haven't tested further yet but my first thought is it's the loop.

Edit2: Updated the code here in the post. Found a nice example from Graf here on trying to load lumps. So I've integrated it and changed my test file into more of a definition file and created a quick and dirty parser loop. Again the game behaves as stated in my first edit. This seems to be loop related. The looping function zWindow_Init() is called from OnRegister(), is that a problem?

Code: Select allExpand view
class zWin_Events : EventHandler
{
   zWinDefs zDefs;
   CVar cvZDebug;
   bool bFileNamesLoaded,      // true when "fileNames.zwin" has been read
      bPicsLoaded;         // true when file names have been loaded to NULL_ZWIN
   
   // Primary Initialization
   override void OnRegister()
   {
      zDefs = zWinDefs.Get();
      cvZDebug = CVar.FindCVar(zDefs.zcv_Debug);
      bFileNamesLoaded = bPicsLoaded = false;
      
      consoleDebugMessage("Z-Windows Primary Handler Registered!");
      zWindow_Init();
   }
   
   // Z-Window Definition Parser
   void zWindow_Init()
   {
      consoleDebugMessage("ZWIN-PH : Z-Window Definition Parser executing.");
      Array<string> fileNames;
      int lump = zDefs.NO_FILE;
      // Check for lump by full path
      lump = Wads.CheckNumForFullName(zDefs.zfln_zWindowPath);
      
      // Try normal lookup
      if (lump == zDefs.NO_FILE)
      {
         consoleDebugMessage("ZWIN-PH : Full path search for window definitions failed\ntrying normal lookup.");
         lump = Wads.CheckNumForName(zDefs.zfln_zWindow, Wads.ns_global);
      }
      
      //File Not Found
      if (lump == zDefs.NO_FILE)
         consoleDebugMessage("ZWIN-PH : NO ZWINDOW DEFINITIONS FOUND!", zDefs.FORCE);
      // File was found, comence reading
      else
      {
         consoleDebugMessage("ZWIN-PH : Storing Definition file names.");
         
         string line = zDefs.NOTHING;
         bool isMaster = false;
         int reads = 0;
         while (line != zDefs.FILE_END && (isMaster || reads == 0))
         {
            line = Wads.ReadLump(lump);
            if (reads == 0 && line == zDefs.MASTER_HEADER)
            {
               consoleDebugMessage("ZWIN-PH : File is a valid Master.");
               isMaster = true;
            }
            else if (isMaster && line != zDefs.FILE_END)
            {
               fileNames.Push(line);
               if (cvZDebug.GetBool() == true)
                  console.printf("ZWIN-PH : File %s stored in slot %i.", fileNames[reads], reads);
               
               reads++;
            }
         }
      }   
   }
   
   // Prints a message to the console if the debug cvar is on
   // - full debug info is output if cvar is changed at titlescreen, not sure about titlemap
   // - cvar is not saved to ini and is a user cvar
   void consoleDebugMessage(string message, bool force = false)
   {
      if (cvZDebug.GetBool() || force)
         Console.Printf(message);
   }
      
   override void WorldTick()
   {
      
   }
   
   override void RenderOverlay(RenderEvent e)
   {
      
   }
}


Spoiler: "Old Code"
Last edited by Nero on Sat Dec 16, 2017 6:33 pm, edited 2 times in total.
User avatar
Nero
Royal Boredom....Why can't I do this in Windows?
 
Joined: 06 Sep 2006
Location: Middle of Nowheresville Il.

Re: [ZScript] File Reading

Postby AFADoomer » Sat Dec 16, 2017 5:28 pm

Did you try including the file extension in the CheckNumForFullName call? So, "zswindefs/zswindow.txt" (or whatever)?
User avatar
AFADoomer
 
Joined: 15 Jul 2003

Re: [ZScript] File Reading

Postby Nero » Sat Dec 16, 2017 6:30 pm

Dang it, I had it working, then I put the loop back in and it stalls out the engine again. Yeah I've tried it with the extension and the same behaviour is observed. I edited the OP with the most recent code and some notes. Is a loop called from OnRegister() a problem?
User avatar
Nero
Royal Boredom....Why can't I do this in Windows?
 
Joined: 06 Sep 2006
Location: Middle of Nowheresville Il.

Re: [ZScript] File Reading

Postby gwHero » Sat Dec 16, 2017 7:06 pm

Nero wrote:Dang it, I had it working, then I put the loop back in and it stalls out the engine again. Yeah I've tried it with the extension and the same behaviour is observed. I edited the OP with the most recent code and some notes. Is a loop called from OnRegister() a problem?


My guess would be that there is something wrong with the loop and not the Wads functions.
In my pk3 I have this code:

Code: Select allExpand view
string lumpcontentx = Wads.ReadLump(Wads.CheckNumForFullName("zscript/gStats.txt"));
string lumpcontent = Wads.ReadLump(Wads.FindLump("MAPINFX"));


and this is working fine.
User avatar
gwHero
Be your own Siddhartha
 
Joined: 08 May 2017
Location: The Netherlands

Re: [ZScript] File Reading

Postby AFADoomer » Sat Dec 16, 2017 7:52 pm

I just changed my own file reader to run from OnRegister, and the file reading portion works fine... So I'm pretty sure it's not that.

One possible issue that I see: What are you trying to do here?
Code: Select allExpand view
         while (line != zDefs.FILE_END && (isMaster || reads == 0))
         {
            line = Wads.ReadLump(lump);

etc...


Wads.ReadLump reads in the entire contents of the lump, not just the current line. You only need to read the data in one time, then you'll have to split that string on "\n" to get each line in an array, then loop through those.

console.printf(line); added after that ReadLump call should print out the entire contents of your file...

The problem is that you have it in the while loop, so it's going to read the whole file and keep pushing the whole file's contents to your array each loop, which is probably what's causing the hang.
User avatar
AFADoomer
 
Joined: 15 Jul 2003

Re: [ZScript] File Reading

Postby Nero » Sat Dec 16, 2017 8:29 pm

Yep that would indeed be it. I was thinking ReadLump read one line at a time, thus the loop. So indeed, user error by overthinking it :wink:
User avatar
Nero
Royal Boredom....Why can't I do this in Windows?
 
Joined: 06 Sep 2006
Location: Middle of Nowheresville Il.


Return to Scripting

Who is online

Users browsing this forum: No registered users and 2 guests