View Single Post
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 12-06-2022 , 13:57   Re: Enum struct, how do we access the struct property as parameter?
Reply With Quote #6

enum struct just "sugar coating" code.

If you want get items work as array, using index, one way is this.
PHP Code:
enum
{
    
LifeStealChance 0,
    
ArmorStealChance,
    
FlashProtectionChance,
    
DamageChance,
    
CameraShakeChance,
    
FreezeChance,
    
ChanceTOTAL
};

enum struct EPlayerChances
{
    
float Chance[ChanceTOTAL];
}
EPlayerChances FPlayerChances[MAXPLAYERS+1];

// You use it like this:
// FPlayerChances[attacker].Chance[CameraShakeChance]; 

Second way.
You can create function, inside enum struct as well
Code:
enum
{
	LifeStealChance = 0,
	ArmorStealChance,
	FlashProtectionChance,
	DamageChance,
	CameraShakeChance,
	FreezeChance,
	MaxChances
}

enum struct EPlayerChances
{
	float LifeStealChance;
	float ArmorStealChance;
	float FlashProtectionChance;
	float DamageChance;
	float CameraShakeChance;
	float FreezeChance;

	bool CanUseAbility(int index)
	{
		// out of bounds
		if(index < LifeStealChance || index >= MaxChances)
			return false;
		
		float a;
		float b = GetRandomFloat(0.0, 1.0);
		
		switch(index)
		{
			case LifeStealChance:
			{
				a = this.LifeStealChance;
			}
			case ArmorStealChance:
			{
				a = this.ArmorStealChance;
			}
			case FlashProtectionChance:
			{
				a = this.FlashProtectionChance;
			}
			case DamageChance:
			{
				a = this.DamageChance;
			}
			case CameraShakeChance:
			{
				a = this.CameraShakeChance;
			}
			case FreezeChance:
			{
				a = this.FreezeChance;
			}
		}
		
		if(a >= b)
			return true;
		
		return false;
	}
}
EPlayerChances PlayerChances[MAXPLAYERS+1];

enum struct EPlayerProperties
{
	float CameraShake;
}
EPlayerProperties PlayerProperties[MAXPLAYERS+1];

bool Game_ShakeVictimOnAttack(int victim, int attacker)
{
	float ShakeAmplitude = PlayerProperties[attacker].CameraShake;

	if(ShakeAmplitude > 0.0 && PlayerChances[attacker].CanUseAbility(CameraShakeChance))
	{
		// Gonna be too much figure these out
		//Game_IsAbilityCoolingDown()

		return true;
	}

	return false;
}

Anyone, please mention, if this code sample is wrong. I just throw this from my head.
__________________
Do not Private Message @me
Bacardi is offline