Monster cant look at weapon variable

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
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Monster cant look at weapon variable

Post by Apeirogon »

I try make fully silenced weapon, like

Code: Select all

class vss : pistol
{
	bool silencer;

	default
	{ 
	Weapon.SelectionOrder 1;
	Weapon.SlotNumber 9;
	+WEAPON.NOALERT;
	}

	override void postbeginplay()
		{
		silencer = false;
		super.postbeginplay();
		}

states
{
altfire:
	pisg c 66 {
	if (invoker.silencer = false) {invoker.silencer = true;}
	if (invoker.silencer = true) {invoker.silencer = false;}
	}
goto ready;
}
}


class suspiciousImp : doomimp
{
	int suspicions;
	
override void postbeginplay()
	{
	suspicions = 0;
	super.postbeginplay();
	}

override int TakeSpecialDamage(Actor inflictor, Actor source, int damage, Name damagetype)
{
	let silencerchecker = source.FindInventory("vss");
		if (silencerchecker.silencer == true)//line 102
				{
				super.TakeSpecialDamage(inflictor, source, damage, damagetype);
				target = null;
				return resolvestate("where_hitman");
				}
	super.TakeSpecialDamage(inflictor, source, damage, damagetype);
}

But I got error
Script error, "monsters.pk3:zscript.dec" line 102:
Unknown identifier 'silencer'
So, I do something wrong or weapon/inventory variables cant be checked from other actors?
User avatar
krokots
Posts: 266
Joined: Tue Jan 19, 2010 5:07 pm

Re: Monster cant look at weapon variable

Post by krokots »

You need to cast the inventory to vss.

Code: Select all

override int TakeSpecialDamage(Actor inflictor, Actor source, int damage, Name damagetype)
{
   let silencerchecker = vss(source.FindInventory("vss"));
      if (silencerchecker && silencerchecker.silencer == true)//line 102
            {
            super.TakeSpecialDamage(inflictor, source, damage, damagetype);
            target = null;
            return resolvestate("where_hitman");
            }
   super.TakeSpecialDamage(inflictor, source, damage, damagetype);
}
FindInventory returns inventory, and that needs to be cast to "vss".
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: Monster cant look at weapon variable

Post by Apeirogon »

And quite predictable question, why I need write vss(source.findinventory("vss"))?

Find inventory return pointer to inventory item. Why I cant acces pointed thing directly?
User avatar
krokots
Posts: 266
Joined: Tue Jan 19, 2010 5:07 pm

Re: Monster cant look at weapon variable

Post by krokots »

You can, but this inventory item don't have your variable "silencer". So, first you have to tell the compiler that silencerchecker IS a "vss". So now he will know all the additional variables that are in "vss". Also, remember to add a check

Code: Select all

if(silencerchecker)
because the cast will return a "null" if the type is something other than "vss".

Edit. For more clarification, when you use FindInventory, you get a pointer to inventory item. Through that, you can only get variables that are defined ONLY on class inventory, and functions ONLY defined in class inventory (and classes that inventory inherits from). Now, you can cast this into other classes that inherits from inventory. So you can cast inventory to, say, weapon class, and you will be able to use all functions that are defined in weapon class.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: Monster cant look at weapon variable

Post by Apeirogon »

So, you want to say that pointer can point from actor to actor, only if pointed thing on other actor dont line with functions that dont define in gzdoom pack!?
And if it have, it need actorname before define "path" to desider actor, using findinventory, to correctly point actor!?
User avatar
Arctangent
Posts: 1235
Joined: Thu Nov 06, 2014 1:53 pm

Re: Monster cant look at weapon variable

Post by Arctangent »

I have no idea what you're trying to say, but the basic gist of it is that ZScript can only access the variables and functions of the class the pointer is cast as, i.e. when you define a variable as

Code: Select all

Actor hatethisdude = target
then the "hatethisdude" variable can only access the stuff defined in Actor. Which is most stuff for actors, like health and radius and A_Chase, but not stuff like Inventory.Amount or Inventory.PickupMessage - those are variables of the Inventory class, which would require something like this

Code: Select all

Inventory coolthing = player.ReadyWeapon
Note, however, that ReadyWeapon is a Weapon pointer. This is still a valid cast, however, as Weapon inherits from Inventory, though the "coolthing" variable will still only be able to access stuff from the Inventory class. So, the reason why you're having issues here is that your vss actor defines a new variable that then only variables cast to vss would have access to, but FindInventory returns a variable cast as Inventory.

Which, though vss is a valid class for an Inventory pointer, as it inherits from Pistol which inherits from DoomWeapon which inherits from Weapon which inherits from Weapon, means that you only have access to the stuff from the Inventory class.

This is why you need the vss(source.FindInventory("vss")) line - that vss(), or rather classname(), is effectively a way to convert one pointer to a new pointer that's cast to that particular class. This means that instead of having the normal Inventory return value from FindInventory(), you'll instead take that and convert it to a vss variable, which does have access to all of vss's variables and functions.
User avatar
gwHero
Posts: 360
Joined: Mon May 08, 2017 3:23 am
Graphics Processor: Intel with Vulkan/Metal Support
Location: The Netherlands

Re: Monster cant look at weapon variable

Post by gwHero »

what helped me years ago to understand classes and inheritance is to compare it with examples that might be a bit less abstract.
Example:

inventory item -> weapon -> vss -> property silencer
animal species -> mammal -> elephant -> property trunk

When you have an instance of the class animal, or even more specific mammal, you can't retrieve the property trunk directly; you must cast it first to the subclass elephant when refering to it, because not all animals have a trunk.

Return to “Scripting”