[ZScript] File Reading

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

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.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
User avatar
Sarah
Posts: 551
Joined: Wed Sep 06, 2006 12:36 pm
Preferred Pronouns: She/Her
Operating System Version (Optional): Debian 11 (bullseye), Windows 10
Location: Middle of Nowheresville Il.

[ZScript] File Reading

Post by Sarah »

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 all

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 Sarah on Sat Dec 16, 2017 5:33 pm, edited 2 times in total.
User avatar
AFADoomer
Posts: 1337
Joined: Tue Jul 15, 2003 4:18 pm

Re: [ZScript] File Reading

Post by AFADoomer »

Did you try including the file extension in the CheckNumForFullName call? So, "zswindefs/zswindow.txt" (or whatever)?
User avatar
Sarah
Posts: 551
Joined: Wed Sep 06, 2006 12:36 pm
Preferred Pronouns: She/Her
Operating System Version (Optional): Debian 11 (bullseye), Windows 10
Location: Middle of Nowheresville Il.

Re: [ZScript] File Reading

Post by Sarah »

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
gwHero
Posts: 360
Joined: Mon May 08, 2017 3:23 am
Graphics Processor: Intel with Vulkan/Metal Support
Location: The Netherlands

Re: [ZScript] File Reading

Post by gwHero »

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 all

string lumpcontentx = Wads.ReadLump(Wads.CheckNumForFullName("zscript/gStats.txt"));
string lumpcontent = Wads.ReadLump(Wads.FindLump("MAPINFX"));
and this is working fine.
User avatar
AFADoomer
Posts: 1337
Joined: Tue Jul 15, 2003 4:18 pm

Re: [ZScript] File Reading

Post by AFADoomer »

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 all

         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
Sarah
Posts: 551
Joined: Wed Sep 06, 2006 12:36 pm
Preferred Pronouns: She/Her
Operating System Version (Optional): Debian 11 (bullseye), Windows 10
Location: Middle of Nowheresville Il.

Re: [ZScript] File Reading

Post by Sarah »

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:

Return to “Scripting”