UE4 crashes on condition

UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = “WeaponManager”)
TArray<AAbWeapon*> Inventory;

bool UWeaponManager::HaveCapacity()
{
	return Inventory.Num() <= MaxCapacity;
}

bool UWeaponManager::IsInInventory(AAbWeapon* Weapon)
{
	for (int32 i = 0; i < Inventory.Num(); i++)
	{
		if (Inventory[i] == Weapon)
			break;
	}

	return condition;
}

int32 UWeaponManager::PutInInventory(AAbWeapon* Weapon)
{
	if (HaveCapacity())                 //HERE THE ERROR HAPPENS
	{
		if (!IsInInventory(Weapon))
		{
			return Inventory.Add(Weapon);
		}
	}

	return -1;
}

Hey Mauro-

What exactly is the error you’re getting from this code? Also, your HaveCapacity method returns true when Inventory.Num is equal to MaxCapacity. If that behaves as I expect (your inventory is full) then you would have a problem in the PutInInventory() call when you try to add to the inventory.

“eu troquei a condição na chamada da funcao HaveCapacity em PutInInventory para uma condição primitiva e passou, mas parou novamente dentro de IsInInventory na condição do for onde chamo Inventory.Num()”

i changed the condition on the call of HaveCapacity and PutInInventory for a primitive condition and pass, mas stopped again on IsInInventory on the “for” condition where i call Inventory.Num()

What is the error that Visual Studio gives? Also, you may have to set Inventory somewhere at the start as you may be trying to access something that doesn’t actually exist.

Try putting “Inventory = NULL;” somewhere in a constructor.

Is that error the one you got originally or the error you receive after making the changes to HaveCapacity and PutInInventory? Could you also explain what that error message is saying?

Hey,

It sounds like you’re trying to call PutInInventory on a null UWeaponManager pointer. How is it being called?

Steve

Hi Mauro,

We have not heard back from you in a few days, so we are marking this post as Resolved for tracking purposes. If you are still experiencing the issue you reported, please respond to this message with additional information and we will offer further assistance.

Thank you.