1

I want to compare some enums on ifs statements here is what I mean but this isnt working. Basically I want to see if the injury is the same as the enum so if for example the injury is bleeding you need to bandage etc. If you need any other information please let me know.

static string injuries = GetInjuriesName(GetInjuries(closestPlayer));
EPedInjuries result;
if (Enum.TryParse(injuries, out result) && result == EPedInjuries.Overdose)
{
.....
}
else if (Enum.TryParse(injuries, out result) && result == EPedInjuries.GunShotWound)
{
....
}
....


public enum EPedInjuries
{
    OpenFracture,
    GunShotWound,
    Fever,
    BrokenLeg,
    BrokenArm,
    BrokenRib,
    Overdose,
    .....
}

    public static EPedInjuries GetInjuries(Ped ped)
    {
        Ped = ped;
        int num = API.Common.Random.Next(0, 101);
        if (num >= 0 && num <= 37)
        {
            return EPedInjuries.Overdose;
        }
        if (num > 37 && num <= 55)
        {
            return EPedInjuries.GunShotWound;
        }
        if (num > 55 && num <= 72)
        {
            return EPedInjuries.CardiacArrest;
        }
        ....
}


public static string GetInjuriesName(EPedInjuries injuries)
{
    string result = string.Empty;
    switch (injuries)
    {
        case EPedInjuries.Overdose:
            result = "~r~Overdose";
            break;
        case EPedInjuries.GunShotWound:
            result = "~r~Gunshot Wound";
            break;
        case EPedInjuries.CardiacArrest:
            result = "~r~Cardiac Arrest";
            break;
        ....
    }
    return result;
}
thedp
  • 8,350
  • 16
  • 53
  • 95
John35352
  • 33
  • 6

3 Answers3

1

The GetInjuries method already returns the enum type you want to compare. As @Johnny Mopp points out, you are getting the enum, converting it to some string, only to try to convert it back to an enum

Just do

EPedInjuries result = GetInjuries(closestPlayer);
if (result == EPedInjuries.Overdose)
{
.....
}
else if (result == EPedInjuries.GunShotWound)
{
....
}
Borka
  • 803
  • 5
  • 16
0

Your GetInjuriesName returns string which is not correct enum value and Enum.TryParse can't parse it and returns false:

var injuries = GetInjuriesName(EPedInjuries.Overdose);
Console.WriteLine(injuries); //prints "~r~Overdose"
Console.WriteLine(Enum.TryParse(injuries, out EPedInjuries result)); // prints "False"

Just introduce two fields/variables one for name and one for injury enum value:

EPedInjuries injury = GetInjuries(closestPlayer)
string injuryName = GetInjuriesName(injury);

And use the first one to handle the logic:

if(injury == EPedInjuries.Overdose)
{
    ....
} 
.....
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

I believe you will receive strings from somewhere else so you want to convert that's why you have that GetInjuriesName method. For me, you should just switch on the strings you receive.

For the sake of fixing this code, you should remove the "~r~" from the strings so that it matches any enum type.

Also, enums can't be null so you will always get the first value which is the Default.

public static string GetInjuriesName(EPedInjuries injuries)
{
    string result = string.Empty;
    switch (injuries)
    {
        case EPedInjuries.Overdose:
            result = "Overdose";
            break;
        case EPedInjuries.GunShotWound:
            result = "Gunshot Wound";
            break;
        case EPedInjuries.CardiacArrest:
            result = "Cardiac Arrest";
            break;
    }
    return result;
}

Then the TryParse will work and produce a value contained inside the enum.

panoukos41
  • 41
  • 9