Log in to reply
 

[help] why is "myPeds" list always null



  • trying to make a simple script to spawn random peds and have them follow me, but myPeds list doesn't get updated?
    how can i fix this?
    myPeds list is initialized at the top of the class, then as i run SpawnPed() i add them to the list
    I then check the list in onTick, but it always return null

    [CODE]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using GTA;
    using GTA.Native;
    using GTA.Math;

    namespace MyFirstScript
    {
    public class Class1 : Script
    {
    public int listCount = 0;
    List<Ped> myPeds;

        public Class1()
        {
            
            Tick += onTick;
            KeyDown += onKeyDown;
        }
    
        private void onTick(object sender, EventArgs e)
        {
            if (myPeds != null)
            {
                listCount = myPeds.Count();
            }else
        {
                UI.ShowSubtitle("myPeds is null");
            }
    
        }
    
        private void onKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.L)
            {
                SpawnPed();
            }
        }
        public void SpawnPed()
        {
            
            var myPed = World.CreatePed(PedHash.Miranda, Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0, 5, 0)));
            myPeds.Add(myPed);
        }
    }
    

    }
    [/CODE]

    also....how do i correctly use the CODE tag? this doesn't look right lol



  • @DCass89

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using GTA;
    using GTA.Native;
    using GTA.Math;
    
    namespace MyFirstScript
    {
    	public class Class1 : Script
    	{
    		public int listCount = 0;
    		List<Ped> myPeds = new List<Ped>();
    
    		public Class1()
    		{
    			
    			Tick += onTick;
    			KeyDown += onKeyDown;
    		}
    
    		private void onTick(object sender, EventArgs e)
    		{
    			if (myPeds != null)
    			{
    				listCount = myPeds.Count();
    			}
    			else
    			{
    				UI.ShowSubtitle("myPeds is null");
    			}
    
    		}
    
    		private void onKeyDown(object sender, KeyEventArgs e)
    		{
    			if (e.KeyCode == Keys.L)
    			{
    				SpawnPed();
    			}
    		}
    		public void SpawnPed()
    		{
    			
    			var myPed = World.CreatePed(PedHash.Miranda, Game.Player.Character.GetOffsetInWorldCoords(new Vector3(0, 5, 0)));
    			myPeds.Add(myPed);
    		}
    	}
    }
    

    You never initialize the myPeds list, so it will always be null. You need to turn

    List<Ped> myPeds;
    

    into

    List<Ped> myPeds = new List<Ped>();
    

    like in the code I showed above.


Log in to reply
 

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