I'm a coding novice and need some help. [Solved]

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
Beef Flavor
Posts: 10
Joined: Thu May 20, 2021 9:36 pm
Location: Texas, USA

I'm a coding novice and need some help. [Solved]

Post by Beef Flavor »

Hello. I'm having some trouble wrapping my head around accessing a class's user variables.

What I'm attempting to do is: I have a custom player class that uses inventory items to spawn friendly monsters. When the inventory item is used, it invokes the owner to spawn a custom projectile which functions much like the 'ArtiDarkServant's summoning doll projectile. I'd like to have a cap on the number of summons active at once, so I gave the custom class a 'NumChildren' user variable.

Trouble I'm having is accessing the user variable from the projectile. I've referenced the wiki article on member variables, but I'm not knowledgeable enough in coding to understand what I'm maent to do to access the variables.

This is what I have so far.

Code: Select all

// Custom Mage Class -----------------------------------------------------------

class MagePlayerCustom : MagePlayer
{
	int NumChildren;
	Default
	{
		Player.StartItem "MWeapDagg";
	
		Player.WeaponSlot 1, "MWeapDagg";
	}
}

// Summoning crystal Projectile-------------------------------------------------

Class SummoningCrystal : SummoningDoll
{
	Default
	{
		Scale .15;
	}
	States
	{
	Spawn:
		CRYS A 4;
		Loop;
	Hound:
		TNT1 A 0
		{
			Spawn("MinotaurSmoke", Pos, ALLOW_REPLACE);
			A_StartSound("hound/act", CHAN_VOICE);
		}
		CRYS AA 4;
		CRYS A 4 
			{
				if(!A_SpawnItemEX("HoundFamiliar", 0, 0, 0, 0, 0, 0, 0, SXF_SETMASTER))
				{
					A_SpawnItemEx("ArtiSummoningCrystal");
				}
				else
				{
					master.NumChildren += 1; //<-- the problem area.  I've tried master.x, invoker.player.x, owner.blah.blah.blah, with the same result, GZDoom throws an error complaining that the identifier 'NumChildren' doesn't exsist.
				}
			}
		Stop;
	Dragon:
		TNT1 A 0
		{
			Spawn("MinotaurSmoke", Pos, ALLOW_REPLACE);
			A_StartSound("dragon/act", CHAN_VOICE);
		}
		CRYS AA 4;
		CRYS A 4 
		{
				if(!A_SpawnItemEX("DragonFamiliar", 0, 0, 40, 0, 0, 0, 0, SXF_SETMASTER))
				{
					A_SpawnItemEx("ArtiSummoningCrystal");
				}
				else
				{
					master.NumChildren += 1;// <-- Same as above.
				}
			}
		Stop;
	Death:
		TNT1 A 0 A_Jump(256, "Hound", "Dragon");
		Stop;
	}
}
Any help with this would be greatly appreciated.
Last edited by Beef Flavor on Fri Jul 19, 2024 6:58 am, edited 1 time in total.
Jarewill
 
 
Posts: 1822
Joined: Sun Jul 21, 2019 8:54 am

Re: I'm a coding novice and need some help.

Post by Jarewill »

The pointer of a projectile's shooter is target, however you will also have to cast the pointer as your custom class:

Code: Select all

let targ = MagePlayerCustom(target);
targ.NumChildren += 1;
User avatar
Beef Flavor
Posts: 10
Joined: Thu May 20, 2021 9:36 pm
Location: Texas, USA

Re: I'm a coding novice and need some help.

Post by Beef Flavor »

Ok, when I try to use this, it crashes GZDoom.

Code: Select all

VM execution aborted: Tried to read from address 0
Here's the offending code:

Code: Select all

override bool Use(bool pickup)
	{
		let target = MagePlayerCustom(target);
		int nc = target.NumChildren;
		if(nc >= MaxFamiliars)
		{
			console.printf("You have too many familiars.");
			return false;
		}
		Owner.SpawnPlayerMissile("SummoningCrystal");
		return true;
	}
Blue Shadow
Posts: 5032
Joined: Sun Nov 14, 2010 12:59 am

Re: I'm a coding novice and need some help.

Post by Blue Shadow »

You'll want to access Owner, not target:

Code: Select all

let owner = MagePlayerCustom(self.Owner);

if (owner != nullptr)
{
	// Do your stuff...
}
User avatar
Beef Flavor
Posts: 10
Joined: Thu May 20, 2021 9:36 pm
Location: Texas, USA

Re: I'm a coding novice and need some help.

Post by Beef Flavor »

Got it, thank you.

Return to “Scripting”