Get value of Inventory.Amount

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

Get value of Inventory.Amount

Post by Apeirogon »

For debugging, I think its right word, reason I need to know how much health, armor and ammo exist on map at this moment, and what type, in case of ammo.

I try accessing its informations through event handler, like

Code: Select all

	override void WorldThingSpawned(WorldEvent e)
	{
		if(e.thing is "BasicArmorPickup"){armor_amount += e.thing.Armor.SaveAmount); }
		if(e.thing is "health"){health_amount += e.thing.Inventory.Amount); }
	}
But gzdoom print error "error while parsing, unknown identifier 'armor'/'inventory'/'save amount'/'amount'/'etc'..." with number of line where I use e.thing.Armor.SaveAmount/etc.
What I do wrong?
User avatar
m8f
 
 
Posts: 1449
Joined: Fri Dec 29, 2017 4:15 am
Preferred Pronouns: He/Him
Operating System Version (Optional): Manjaro Linux
Location: Siberia (UTC+7)

Re: Get value of Inventory.Amount

Post by m8f »

There are errors because Armor and Inventory are not properties of Thing (and you are trying to use them like properties, with "."). They are names of derived classes. You may want to read about inheritance concept.
Then, Amount and SaveAmount are properties of classes of Inventory and BasicArmorPickup, respectively. To access those properties you need to cast Thing to derived classes, like this:

Ошибки из-за того что Armor и Inventory - не свойства класса Thing, а имена унаследованных классов (а код пытается обратиться к ним как к свойствам, через точку "."). Может быть полезно почитать про наследование.
Далее, Amount и SaveAmount - свойства классов Inventory и BasicArmorPickup, соответственно. Чтобы получить доступ к этим свойствам, нужно сначала преобразовать Thing к унаследованному классу, вот так:

Code: Select all

    if (e.thing is "BasicArmorPickup")
    {
      BasicArmorPickup i = BasicArmorPickup(e.thing);
      Console.Printf("Found armor, amount: %d", i.SaveAmount);
    }
It must be noted that casting is not always successful, and resulting value may be Null if thing is not really an object of derived class. In this case, success is guaranteed because of previous check (e.thing is "BasicArmorPickup").
Нужно отметить, что преобразование типов не всегда может быть успешным, и результат может быть Null, если thing на самом деле не является объектом унаследованного класса. В данном случае, успех гарантирован, потому что перед преобразованием уже есть проверка (e.thing is "BasicArmorPickup").

Here is the working example of the code. I only replaced armor_amount and health_amount variables with Console.Printf for code to be more simple.
Вот пример работающего кода. Я только заменил armor_amount и health_amount на Console.Printf, чтобы код был проще.
debug.pk3
You do not have the required permissions to view the files attached to this post.
User avatar
Apeirogon
Posts: 1606
Joined: Mon Jun 12, 2017 12:57 am

Re: Get value of Inventory.Amount

Post by Apeirogon »

Again problem with casting...

I thought gzdoom knows about properties of its built-in inherited class, when I try e.thing.SaveAmount/e.thing.Amount.
Well, now I begin "cast casting" in every line where I try access to actor property.

Return to “Scripting”