Peds::new: Difference between revisions

From RAGE Multiplayer Wiki
(Updated the page to represent the change in syntax, as well as provide the function for legacy syntax support.)
No edit summary
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
{{Note|Updated docs on creating peds: [[Ped::Ped]].}}
==Syntax==
==Syntax==
<syntaxhighlight lang="javascript">mp.peds.new(model, position, heading, dimension);</syntaxhighlight>
<syntaxhighlight lang="javascript">mp.peds.new(model, position, heading, dimension);</syntaxhighlight>
Line 7: Line 9:
*'''dimension:''' int
*'''dimension:''' int
===Return value===
===Return value===
*'''Ped handle or object'''
*'''Ped object'''
 
This function relies on the 'entityStreamIn' event to handle the Ped's streamInEvent. If you do not have this event in your script, you can find it below.


==Example==
==Example==
{{ClientSide}}
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
let Ped = mp.peds.new(mp.game.joaat('MP_F_Freemode_01'), new mp.Vector3( 100.0, -100.0, 25.0), 270.0, mp.players.local.dimension);
let ped = mp.peds.new(
    mp.game.joaat('MP_F_Freemode_01'),  
    new mp.Vector3(100.0, -100.0, 25.0),
    270.0,
    mp.players.local.dimension
);
</syntaxhighlight>
</syntaxhighlight>
== ==


==Legacy Syntax Variation==
==Legacy Syntax Variation==


If you would like to use the legacy syntax for mp.peds.new(), refer to the following function:
The previous syntax for this function allowed you to specify a "streamedIn" callback. This has been removed in favor of the [[EntityStreamIn]] event, but you can add this polyfill to your client script to enable the previous syntax:
{{ClientSide}}
 
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
mp.peds.newLegacy = (hash, position, heading, streamIn, dimension) =>
mp.peds.newLegacy = (hash, position, heading, streamIn, dimension) => {
{
     let ped = mp.peds.new(hash, position, heading, dimension);
     let ped = mp.peds.new(hash, position, heading, dimension);
     ped.streamInHandler = streamIn;
     ped.streamInHandler = streamIn;
     return ped;
     return ped;
};
};
</syntaxhighlight>


For the above function to work, you must add the following 'entityStreamIn' event to your script:
mp.events.add("entityStreamIn", entity => {
{{ClientSide}}
   if (entity.streamInHandler) {
<syntaxhighlight lang="javascript">
mp.events.add("entityStreamIn", (entity) =>  
{
   if(entity.streamInHandler)
  {
       entity.streamInHandler(entity);
       entity.streamInHandler(entity);
   }
   }
Line 44: Line 40:


===Example===
===Example===
{{ClientSide}}
<syntaxhighlight lang="javascript">
<syntaxhighlight lang="javascript">
let Ped = mp.peds.newLegacy(mp.game.joaat(model), location, 0, (streamPed) => {
let ped = mp.peds.newLegacy(mp.game.joaat('mp_m_freemode_01'), new mp.Vector3(1000, 100, 10), 0, ped => {
        // Ped Streamed
    // Called when the ped is streamed in
     streamPed.setAlpha(255);
     ped.setAlpha(255);
     streamPed.freezePosition(false);
     ped.freezePosition(false);
     streamPed.setInvincible(false);
     ped.setInvincible(false);
     streamPed.setProofs(false, false, false, false, false, false, false, false);  
     ped.setProofs(false, false, false, false, false, false, false, false);  
}, 0);
}, 0);
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 12:12, 14 November 2021

Updated docs on creating peds: Ped::Ped.

Syntax

mp.peds.new(model, position, heading, dimension);

Required Arguments

  • modelHash: Model hash
  • position: Vector3 position
  • heading: float
  • dimension: int

Return value

  • Ped object

Example

let ped = mp.peds.new(
    mp.game.joaat('MP_F_Freemode_01'), 
    new mp.Vector3(100.0, -100.0, 25.0),
    270.0,
    mp.players.local.dimension
);

Legacy Syntax Variation

The previous syntax for this function allowed you to specify a "streamedIn" callback. This has been removed in favor of the EntityStreamIn event, but you can add this polyfill to your client script to enable the previous syntax:

mp.peds.newLegacy = (hash, position, heading, streamIn, dimension) => {
    let ped = mp.peds.new(hash, position, heading, dimension);
    ped.streamInHandler = streamIn;
    return ped;
};

mp.events.add("entityStreamIn", entity => {
   if (entity.streamInHandler) {
       entity.streamInHandler(entity);
   }
});

Example

let ped = mp.peds.newLegacy(mp.game.joaat('mp_m_freemode_01'), new mp.Vector3(1000, 100, 10), 0, ped => {
    // Called when the ped is streamed in
    ped.setAlpha(255);
    ped.freezePosition(false);
    ped.setInvincible(false);
    ped.setProofs(false, false, false, false, false, false, false, false); 
}, 0);


See also