Jump to content
  • Spawning your ped (C#)


    Hayden Almeida

    Today i will try to teach how to spawn your first ped in RedM with C#.


    first you need to create a "ENUM" to make your list of your peds (you can see they here: https://www.mod-rdr.com/wiki/peds/ )

    public enum PedHash : uint
    {
    	AcHorseAndalusianDarkbay = 0xE57FC660,
    	AcHorseAndalusianRosegray = 0x2C80A080  
    }

    ATENTION!  If you are using other Class to create this enum, you put "public enum". If this enum was created in the same class, you use "private enum".

     

    Second lets create our function to spawn and load the ped:

    public static async Task<bool> LoadModel(int hash)
    {
    	if (Function.Call<bool>(Hash.IS_MODEL_VALID, hash))
        {
    		Function.Call((Hash)0xFA28FE3A6246FC30, hash);
    		while (!Function.Call<bool>(Hash.HAS_MODEL_LOADED, hash))
            {
            	Debug.WriteLine($"Esperando o modelo {hash} carregar!");
                await BaseScript.Delay(200);
    		}
    			Debug.WriteLine("Modelo carregado!");
    			return true;
            }
            else
            {
            	Debug.WriteLine($"Model {hash} nao e valido!");
                return false;
            }
    }
    
    public static async Task<bool> LoadPed(PedHash ped)
    {
    	if (!await LoadModel((int)ped))
        	return true;
        else
        	return false;
    }

    This function ^ will load the ped. Before creating the main function to spawn the ped, we need to create other function to "randomize" the outfit of the ped. Without this the ped will not spawn! So i only created in a "beautiful" name 😄

    public static void SetRandomOutfitVariation(int ped)
    {
    	Function.Call((Hash)0x283978A15512B2FE, ped, true);
    }

     

    Now lets create the main function to spawn de ped:

    public static async Task<int> CriarPed(PedHash ped, float posx, float posy, float posz, float heading)
    {
    	var task = LoadPed(ped); // this will call the function we already created before
        bool result = await task; // this line will only proceed if we have something in return from "LoadPed"
        int ped_ = API.CreatePed((uint)ped, posx, posy, posz, heading, true, true, true, true);
        Function.Call(Hash.SET_ENTITY_AS_MISSION_ENTITY, ped_, true, true); // setting this true for this ped not "despawn"
        SetRandomOutfitVariation(ped_); // every ped to spawn we need to random his outfit or variation...
        API.SetModelAsNoLongerNeeded((uint)ped); // clear the model in memory
        return ped_; 
    }

    Now with our functions created, we can now create our command to spawn any ped that are inside ENUM.

    The command will work this way: /createped [ped name]

    Lets go to our main CLIENT file, inside our Main public:

    public Main()
    {
    	API.RegisterCommand("createped", new Action<int, List<object>, string>((src, argumentos, raw) =>
    	{
        	CMD_CriarPed(argumentos); // this is the function we will excute
        }), false);
    }

    This is the function our command will execute:

    private async void CMD_CriarPed(List<object> argumentos)
    {
    	playerid = API.PlayerPedId();
        var argList = argumentos.Select(o => o.ToString()).ToList();
        if (argList.Any() && Enum.TryParse(argList[0], true, out PedHash pedi)) // this line will compare if the param we write, exists inside ENUM.
        { // argList[0] is to get the first argumento we wrote.
          	// TIP:
          	// if we want another parameter for our command we can call this:
          	// int number = Convert.ToInt32(argumentos[1]);
          	// Now our command can use: /createped [ped name] [number]
          	// and so on...
        	Vector3 pos = API.GetEntityCoords(playerid, false, false);
            Vector3 forwardpos = API.GetEntityForwardVector((uint)playerid);
            float hdg = API.GetEntityHeading(playerid);
            pos += (forwardpos * 5);
            var ped_criado = CriarPed(pedi, pos.X, pos.Y, pos.Z + 2.0F, hdg);
            int result = await ped_criado;
    		Debug.WriteLine($"Ped ID created={result}");
    	}
    }

    Hope this will help begginers to program in RedM! Cya 🧲

     

    • Like 1

    User Feedback

    Recommended Comments

    There are no comments to display.



    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Add a comment...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...