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
Line 7: Line 7:
*'''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>
== ==
== ==
Line 20: Line 22:
==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 39:


===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>

Revision as of 07:06, 10 November 2019

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