Object::getClosestObjectOfType: Difference between revisions

From RAGE Multiplayer Wiki
m (Example: forgot to define player)
(→‎Example: should work now, apparently, forEach messes with return...)
 
Line 29: Line 29:
nextToAtm = function(player) {
nextToAtm = function(player) {
     // We want to iterate over every type of atm
     // We want to iterate over every type of atm
     atms.forEach(atmHash => {
     for (let i = 0; i < atms.length; i++) {
        const atmHash = atms[i];
         // any atm withing range our check object, or nothing
         // any atm withing range our check object, or nothing
         var check = mp.game.object.getClosestObjectOfType(player.position.x, player.position.y, player.position.z, atmRange, atmHash, false, true, true);
         var check = mp.game.object.getClosestObjectOfType(player.position.x, player.position.y, player.position.z, atmRange, atmHash, false, true, true);
         // object -> true, nothing -> false
         // object -> true, nothing -> false
         if (check) {
         if (check) {
            object = check;
             return true;
             return true;
         }
         }

Latest revision as of 15:25, 1 July 2021

Has 8 params in the latest patches.

isMission - if true doesn't return mission objects

Syntax

mp.game.object.getClosestObjectOfType(x, y, z, radius, modelHash, isMission, p6, p7);

Required Arguments

  • x: float
  • y: float
  • z: float
  • radius: float
  • modelHash: Model hash or name
  • isMission: Boolean
  • p6: Boolean
  • p7: Boolean

Return value

  • Object handle

Example

I use it for interaction with objects of type. E.g. atms, vending machines, bins, etc...

const player = mp.players.local;
// atms
// There are four different types of atms, so I will create this array with hashes of them
atms = [
mp.game.joaat("prop_atm_01"), 
mp.game.joaat("prop_atm_02"),
mp.game.joaat("prop_atm_03"),
mp.game.joaat("prop_fleeca_atm")
];
// I need a range. I make this variable to ease any configuration
var atmRange = 1.5;
nextToAtm = function(player) {
    // We want to iterate over every type of atm
    for (let i = 0; i < atms.length; i++) {
        const atmHash = atms[i];
        // any atm withing range our check object, or nothing
        var check = mp.game.object.getClosestObjectOfType(player.position.x, player.position.y, player.position.z, atmRange, atmHash, false, true, true);
        // object -> true, nothing -> false
        if (check) {
            return true;
        }
    });
    // If none of the types of atms is in reach, we'll return false
    return false;
}

See also

  • [[Object::disableGlow|mp.game.object.disableGlow