Log in to reply
 

Code breaks from SHVDN3.1 to SHVDN3.4. Fix?



  • My game is frozen in time but I recently had to update SHVDN from 3.1 to 3.4 to try out a new mod using LemonUI.

    Mod worked fine but I realized my own LemonUI menu for weapons was now broken. So I reverted back to 3.1

    I looked at the documentation, post 3.1 and because I'm not a programmer it's hard for me to understand but at least i can see they made changes that coincide with my code:
    additional enum values for WeaponComponentHash and WeaponAttachmentPoint, and additional properties for missing enum values of WeaponAttachmentPoint

    My code that works in 3.1 but not if I upgrade to 3.4 is this:

     public void ChangeWeaponsPed(object sender, EventArgs e)
            {
                String MyChoice = WeaponsSubmenu2.SelectedItem;
                {  WeaponHash wepHash = (WeaponHash)Enum.Parse(typeof(WeaponHash), MyChoice);
                    CurrentPed.Weapons.Give(wepHash, 5000, true, true);
                   // weapon components: Function.Call(Hash.GIVE_WEAPON_COMPONENT_TO_PED, CurrentPed, WeaponHash.HeavyPistol, 0xC304849A); //suppressor  
                }
            }
    

    Although I don't mind staying at 3.1 forever, would be nice to know why my code broke with subsequent updates.



  • @JohnFromGWN said in Code breaks from SHVDN3.1 to SHVDN3.4. Fix?:

    String MyChoice = WeaponsSubmenu2.SelectedItem;
    

    I am confused! how does the input come? is an integer but in string?



  • @Niziul It comes from a LemonUI menu, as strings.

    The weapon selection options are chosen from here (this is what is displayed in the menu):
    private static readonly NativeListItem<String> WeaponsSubmenu2 = new NativeListItem<String>("Ped Weapons", "", "Unarmed", "Bat", "SMGMk2", "RPG", "Machete", "Nightstick", "Molotov", "Grenade", "GrenadeLauncher", "FlareGun", "SniperRifle");

    This is where the trigger is
    submenuPed.Add(WeaponsSubmenu2); WeaponsSubmenu2.Activated += ChangeWeaponsPed;



  • This post is deleted!


  • @Niziul I was just copying your code and it disappeared. lol. how come?



  • @JohnFromGWN

    is that I realized that the problem is not in ChangeWeaponsPed(object sender, EventArgs e). That is, I basically copied and pasted your own code, but with some changes that wouldn't go anywhere.

    Your script is basically right. The only thing that is out of order is the creation of two function scopes:

    theoretically wrong, but I believe that the code editor itself would show this error:

     public void ChangeWeaponsPed(object sender, EventArgs e)
            {
                String MyChoice = WeaponsSubmenu2.SelectedItem;
                {  WeaponHash wepHash = (WeaponHash)Enum.Parse(typeof(WeaponHash), MyChoice);
                    CurrentPed.Weapons.Give(wepHash, 5000, true, true);
                   // weapon components: Function.Call(Hash.GIVE_WEAPON_COMPONENT_TO_PED, CurrentPed, WeaponHash.HeavyPistol, 0xC304849A); //suppressor  
                }
            }
    

    I think it was supposed to be like this:

            public void ChangeWeaponsPed(object sender, EventArgs e)
            {
                String MyChoice = WeaponsSubmenu2.SelectedItem;
                WeaponHash wepHash = (WeaponHash)Enum.Parse(typeof(WeaponHash), MyChoice);
                CurrentPed.Weapons.Give(wepHash, 5000, true, true);
                // weapon components: Function.Call(Hash.GIVE_WEAPON_COMPONENT_TO_PED, CurrentPed, WeaponHash.HeavyPistol, 0xC304849A); //suppressor  
            }
    

    The problem lies elsewhere. Could you tell me if there is another function that is altering the weapon components?



  • @Niziul Just to clarify, the code works as expected with SHVDN3.1 but in SHVDN3.4 there are lags and inconsistencies. I was happy with the script because the code is minimal.

    I forgot to mention the most puzzling part. I have a second, almost identical function for the Player - but for some reason that one doesn't have any issue in SHVDN3.4 - it works with both. Only the Ped code breaks. WTF?

    And the editor will not show errors. It will compile and the script will run in both 3.1 and 3.4. However 3.4 does not run consistently and the documentation clearly says they made changes to the weapons - but nothing i understand.

    This is the Player, identical code, PP= Player ped. Why should this one work and not the one for the ped for V3.4?

    public void ChangeWeapons(object sender, EventArgs e)
            {
                String MyChoice = WeaponsSubmenu.SelectedItem;
                {
                    WeaponHash wepHash = (WeaponHash)Enum.Parse(typeof(WeaponHash), MyChoice);
                    PP.Weapons.Give(wepHash, 5000, true, true);
                    
                }
            }
    

    The video below is with 3.1 and shows the script runs without lag, without inconsistencies, without any issues.



  • @JohnFromGWN
    In case you are referring to the player like this!?
    Game.Player.Character.Weapons.Give(WeaponHash weaponHash, int ammoCount, bool equipNow, bool isAmmoLoaded);



  • @Niziul Yes, exactly.

    public static Ped PP = Game.Player.Character;



  • @JohnFromGWN

    That's really weird, I created a script similar to yours and it worked on mine. Could it be something with the menu system?

    using GTA;
    
    
    namespace Helideck_Signaling
    {
        internal class Class1 : Script
        {
            public Class1()
            {
                var player =
                    Game.Player.Character;
    
                var randomPed = 
                    World
                        .CreateRandomPed(player
                                            .FrontPosition);
                randomPed
                    .AddBlip()
                        .Scale = 0.50f;
    
                Aborted += (o, e) =>
                {
                    randomPed
                        .Task
                            .WanderAround();
    
                    randomPed
                        .MarkAsNoLongerNeeded();
                };
    
                KeyUp   += (o, e) =>
                {
                    if (e.Shift && e.KeyCode is System.Windows.Forms.Keys.L)
                    {
                        randomPed
                            .Weapons
                                .Give((WeaponHash)System.Enum.Parse(typeof(WeaponHash), "CombatPistol"), 10, true, true);
                    }
                };
            }
        }
    }
    
    


  • @Niziul I don't know what to say. My script in 3.4 works perfectly for Player, inconsistently for ped. What I mean by inconsistently, it's almost as if it had to be initiated.

    For example I'll press RPG and nothing happens, I keep hitting it nothing, I keep cycling through the weapons and then suddenly it works, as if it was waiting for something requested and then loading.



  • @Niziul Please don't lose anymore time with this. I'm going to stick with 3.1 for now. Thanks so much for your help.



  • They messed with the parameters in 3.4. Try replacing the Give function with the old version to see if it changes the behavior:

    //old version of Give function from v3.0 https://github.com/crosire/scripthookvdotnet/blob/86c836dedccd4d4bb621e8a97762243befda409d/source/scripting_v3/GTA/Weapons/WeaponCollection.cs#L138-L156
    //modified because it usually depends on properties only available from within the WeaponCollection class. UNTESTED
    private Weapon Give(Ped owner, WeaponHash weaponHash, int ammoCount, bool equipNow, bool isAmmoLoaded)
    {
    	Weapon weapon = owner.Weapons[weaponHash];
    
    	if (weapon.IsPresent)
    	{
    		Select(weapon);
    	}
    	else
    	{
    		Function.Call(Hash.GIVE_WEAPON_TO_PED, owner.Handle, weapon.Hash, ammoCount, equipNow, isAmmoLoaded);
    	}
    
    	return weapon;
    }
    

    Untested. Don't be surprised if there are errors. Change your code to

    Give(CurrentPed, wepHash, 5000, true, true);
    

    Changing the function name might be a good idea, as Give isn't exactly descriptive.


Log in to reply
 

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