Log in to reply

C# Help with Weapons - SHVDN3

  • I'm capturing a weapon selection from LemonUi Menu and rather than use switch, or select case, or hard coded if statements, I'd like to pass the selected weapon as a string variable. However I can't get this to work.

    The Weapon Give function comes in 2 flavours.

    This should be the simple one because my selection is returned as a string.
    public Weapon Give(string name, int ammoCount, bool equipNow, bool isAmmoLoaded);

    This is how I capture the selection and this works perfectly if i use if statements.
    String MyChoice = WeaponsSubmenu.SelectedItem;
    However, this does NOT work:
    PP.Weapons.Give(MyChoice, 500, true, true); } //no compile error, no runtime error, just returns nothing
    Given it is expecting a string (first argument for function is a string) and I'm passing a string, what gives? Why doesn't it work.

    The other possibility is to use the weapon hash.
    public Weapon Give(WeaponHash weaponHash, int ammoCount, bool equipNow, bool isAmmoLoaded);

    For example, SniperRifle = 100416529 and this works:
    PP.Weapons.Give(100416529, 500, true, true);

    But the issue now is I don't know how to get the hash from the string MyChoice - because I'm not a programmer!

    Would love answers to both questions. Why the string doesn't work and how I can get the hash value for the weapon in MyChoice.

    My working code, which I don't want hardcoded, looks like this:
    if (MyChoice == "SMG") { PP.Weapons.Give(WeaponHash.SMGMk2, 5000, true, true);

  • Solved
    WeaponHash wepHash = (WeaponHash)Enum.Parse(typeof(WeaponHash), MyChoice);
    PP.Weapons.Give(wepHash, 1000, true, true);

    Still don't understand why I can't pass a string to a string argument, but it works and now I have one line of code to replace over 20.

Log in to reply

Looks like your connection to GTA5-Mods.com Forums was lost, please wait while we try to reconnect.