[SOLVED] Compare multiple tags of two entity

Hello,

Is there any direct function to compare multiple tags of two entity?

Hi @Ketan_ATA,

For a single entity the has() method can take an array as well (AND) or a comma separated list (OR), to check if the tags exist.

But for multiple entities you will have to get the full list of tags, using the list() method and do the comparison yourself.

https://developer.playcanvas.com/en/api/pc.Tags.html#list

1 Like

Ohh i see… I was wondering if there is any quick function or something…

Any way i sort out my problem using following code…

GameManager.prototype.compareTags = function(entity1, entity2) {
    
    var tagData1 = entity1.tags.list();
    var tagData2 = entity2.tags.list();
    
    var isMatched = false;
    
    for(i = 0; i < tagData1.length; ++i)
    {
        if(tagData2.includes(tagData1[i]))
        {
            isMatched = true;
        }
        else
        {
            isMatched = false;
            break;
        }
    }
    
    if(isMatched === true)
    {
        for(i = 0; i < tagData2.length; ++i)
        {
            if(tagData1.includes(tagData2[i]))
            {
                isMatched = true;
            }
            else
            {
                isMatched = false;
                break;
            }
        }
    }
    
    return isMatched;
};

Thanks anyway :slight_smile:

2 Likes