Log in to reply
 

NulllReferenceException after CreatePed



  • Hello,

    I saw that CreatePed sometimes (Normally upon start game) returns a null ped. After that I get NullReferenceException when trying to set Ped properties.

    Here's a piece of my code:

        Ped getPed(Vector3 position, float heading = 0, bool putMask = true)
        {
            Ped random = World.CreatePed("mp_f_freemode_01", position, heading);
            /*  while (random==null) { 
                Script.Wait(1000);
                GTA.UI.Notification.Show("Debug: waiting", false);
            } */
    
            random.BlockPermanentEvents = true;
            random.IsPersistent = true;
            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, random, ClothingComponent.Undershirt, -1, 0, 0);
            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, random, ClothingComponent.Torso, 15, 0, 0);
            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, random, ClothingComponent.Top, 15, 0, 0);
            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, random, ClothingComponent.Legs, 14, 0, 0);
            Function.Call(Hash.SET_PED_COMPONENT_VARIATION, random, ClothingComponent.Shoes, 9, 0, 0);
    
            if(putMask) 
            {
    
                Function.Call(Hash.SET_PED_COMPONENT_VARIATION, random, ClothingComponent.Mask, masks[rnd.Next(0, masks.Length)], 0, 0);
    
            }
    
    
            return random;
        }
    

    Also here is my OnTick:

        void OnTick(object sender, EventArgs e)
        {
            if (Function.Call<bool>(Hash.GET_IS_LOADING_SCREEN_ACTIVE)) return;
    
            Player player = Game.Player;
            Ped playerPed = player.Character;
    
            if (justStarted && Vector3.Distance(playerPed.Position, new Vector3(114.6678f, -1290.395f, 28.26097f))<50f)
            {
                random = getPed(new Vector3(120.515f, -1297.159f, 29.26254f));
                justStarted = false;
    
            }
    
        }
    

    Additionally I'm using ScriptHoogDotNetV3

    As workaround I tried "while (random==null) Script.Wait(1000);" after World.CreatePed but sometimes execution holds inside the loop forever.

    Do you know how could I avoid this NullPointer?



  • It is returning null because it wasn't able to load the model in time. You could try requesting the model yourself, or you could keep trying to create the ped in a loop until it is actually created.


Log in to reply
 

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