Page 1 of 1

Get value of Inventory.Amount

Posted: Sat Mar 24, 2018 11:52 am
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?

Re: Get value of Inventory.Amount

Posted: Sat Mar 24, 2018 8:40 pm
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
(608 Bytes) Downloaded 37 times

Re: Get value of Inventory.Amount

Posted: Sun Mar 25, 2018 8:35 am
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.