Looping through str[] returns incorrect strings

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!)
Zero One
Posts: 6
Joined: Sat Sep 14, 2024 7:58 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Looping through str[] returns incorrect strings

Post by Zero One »

I wrote a script that loops through all of the ammo types in the player's inventory and throws them out on death so other people can pick them up.

My initial script looked like this:

Code: Select all

#define ammoTypeCount 7

// The ammo types to search for
str ammoTypes[ammoTypeCount] = { "PistolBullets", "NewShell", "NewClip", "HighExplosiveGrenadeRounds", "SpecialGrenadeRounds", "RocketAmmo", "Cell" };
// The ammo pickups to spawn
str ammoDrops[ammoTypeCount] = { "SmallBulletCartridge", "2Shells", "SmallClip", "HEGrenade", "SpecialGrenade", "RocketAmmo", "NewCell" };
// Amount to remove from inventory
int dropCounts[ammoTypeCount] = {10, 2, 10, 1, 1, 1, 20};

int i;
for (i = 0; i < ammoTypeCount; i++)
{
	// For each ammo type in the array, if we have enough of that ammo in the inventory, "drop" it
	while (CheckInventory(ammoTypes[i]) >= (dropCounts[i] * multiplier))
	{
		int uTid = UniqueTID();
		if (Spawn(ammoDrops[i], x, y, GetActorZ(0), uTid))
		{
			TakeInventory(ammoTypes[i], dropCounts[i] * multiplier);
		}
	}
}
When I tried running the code, instead of returning strings from the ammoTypes array, it was pulling other entries from the string table like `BD_COOPLIVES`. I worked around this by just not looping, but does anybody know why this happened and if I can fix that?
Last edited by Zero One on Sat Sep 14, 2024 12:38 pm, edited 1 time in total.
Blue Shadow
Posts: 5019
Joined: Sun Nov 14, 2010 12:59 am

Re: Looping through str[] returns incorrect strings

Post by Blue Shadow »

Is this an ACS library? If so, make sure you have #library "libname" at the very top of the flie (replace libname with the name of the library).
Zero One
Posts: 6
Joined: Sat Sep 14, 2024 7:58 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Re: Looping through str[] returns incorrect strings

Post by Zero One »

Yeah, that's all sorted, I'm just showing the snippet that isn't working correctly. I have a drop ammo script written in a different way, I'd just like to find out what was wrong with this method.
Blue Shadow
Posts: 5019
Joined: Sun Nov 14, 2010 12:59 am

Re: Looping through str[] returns incorrect strings

Post by Blue Shadow »

Unless I'm missing something, I can't see anything wrong with that piece of code.

If you could upload something which exhibits the weird behavior, it would make it easier to find out what's wrong.
Zero One
Posts: 6
Joined: Sat Sep 14, 2024 7:58 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Re: Looping through str[] returns incorrect strings

Post by Zero One »

I've attached the .pk3 to this post. It requires Project Brutality v2.03 and PB Overhaul.

I'm encountering a new issue where execution of the script just appears to stop after defining one of the arrays. This is the updated code:

Code: Select all

#library "DeathDrop"
#include "zcommon.acs"

#define ammoTypeCount 7
#define delayTimeTics 1

Script "YouGodDamnDied" DEATH
{
	//At least we can drop weapons
	DropInventory(0, "Revolver");
	DropInventory(0, "Super_Grenade_Launcher");
	DropInventory(0, "FreezerRifle");
	DropInventory(0, "Grenade_Launcher");
	DropInventory(0, "Rocket_Launcher");
	DropInventory(0, "Plasma_Gun");
	DropInventory(0, "M2PlasmaRifle");
	DropInventory(0, "Rail_Gun");
	DropInventory(0, "BHGen");
	DropInventory(0, "PlasmaBeam");
	DropInventory(0, "BIG_FUCKING_GUN_MKIV");
	DropInventory(0, "Hell_rifle");
	DropInventory(0, "NewLMG");
	
    PrintBold(s:"Weapons dropped");
	int angle = GetActorAngle(0);
	int x = GetActorX(0);
	int y = GetActorY(0);
	int z = GetActorZ(0) + 28;
    PrintBold(s:"Location calculated");
	
	int multiplier = 1;
	
	if ((GetCVar("DMFlags2") & 64) != 0)
	{
		multiplier = 2;
	}
    PrintBold(s:"Multiplier set");
	
	str ammoTypes[7] = { "PistolBullets", "NewShell", "NewClip", "HighExplosiveGrenadeRounds", "SpecialGrenadeRounds", "RocketAmmo", "Cell" };
    PrintBold(s:"Defined ammo types");
	str ammoDrops[7] = { "SmallBulletCartridge", "2Shells", "SmallClip", "HEGrenade", "SpecialGrenade", "RocketAmmo", "NewCell" };
    PrintBold(s:"Defined ammo drops");
	int dropCounts[7] = {10, 2, 10, 1, 1, 1, 20};
    PrintBold(s:"Defined drop counts");
	
	PrintBold(s:"Looping through ammo types");
	int i;
	for (i = 0; i < ammoTypeCount; i++)
	{
		PrintBold(d:i);
		PrintBold(s:ammoTypes[i]);
		while (CheckInventory(ammoTypes[i]) >= (dropCounts[i] * multiplier))
		{
			PrintBold(s:StrParam(s:"Dropping ammo type", s:ammoTypes[i]));
			int uTid = UniqueTID();
			if (Spawn(ammoDrops[i], x, y, GetActorZ(0), uTid))
			{
				TakeInventory(ammoTypes[i], dropCounts[i] * multiplier);
			}
		}
	}
}
For some reason, it stops after the PrintBold "Defined ammo types". I'm not sure why it's failing in a different place now.
You do not have the required permissions to view the files attached to this post.
Blue Shadow
Posts: 5019
Joined: Sun Nov 14, 2010 12:59 am

Re: Looping through str[] returns incorrect strings

Post by Blue Shadow »

Since I don't have those mods, I just ran the file alone, and this is what got logged in the console after running the script:

Code: Select all

Weapons dropped
Location calculated
Multiplier set
Defined ammo types
Defined ammo drops
Defined drop counts
Looping through ammo types
0
PistolBullets
1
NewShell
2
NewClip
3
HighExplosiveGrenadeRounds
4
SpecialGrenadeRounds
5
RocketAmmo
6
Cell
Apart from the stuff which happens inside the while-loop statement (again, I don't have the mods for the inventory check to succeed), it seems correct. This could only mean something those mods are doing incorrectly with their ACS libraries which affects your code. But I can't really say for sure.

(Side note: ACS library names are limited to 8 characters in length. As far as the engine is concerned, the name of your library is DeathDro not DeathDrop. Keep this in mind.)
Zero One
Posts: 6
Joined: Sat Sep 14, 2024 7:58 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Re: Looping through str[] returns incorrect strings

Post by Zero One »

That's bizarre. I tried launching just my own .pk3 on plain DOOM II and it still breaks in the exact same place. I have no idea why it's working for you and not for me.
Blue Shadow
Posts: 5019
Joined: Sun Nov 14, 2010 12:59 am

Re: Looping through str[] returns incorrect strings

Post by Blue Shadow »

Do you have any mods in your autoload that might cause an interference?
Zero One
Posts: 6
Joined: Sat Sep 14, 2024 7:58 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Re: Looping through str[] returns incorrect strings

Post by Zero One »

Nope, all the autoloads in the .ini are empty. As far as I can tell, it's just plain DOOM II.
Blue Shadow
Posts: 5019
Joined: Sun Nov 14, 2010 12:59 am

Re: Looping through str[] returns incorrect strings

Post by Blue Shadow »

Then I'm out of ideas, I'm afraid.
Zero One
Posts: 6
Joined: Sat Sep 14, 2024 7:58 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Windows 10

Re: Looping through str[] returns incorrect strings

Post by Zero One »

s'alright, just figured it out. The version of Zandronum I was running was out of date. Running 3.2-alpha and it seems to correctly reach the end of the script. Haven't tested it with PB yet, but I'm hopeful it'll work. Thanks for helping!

Return to “Scripting”