I wasn't sure what you mean by "accompanied by one or more caregivers", so my answer above assumed you were refering to "companions".
If, instead, you want to get a reference to a staff member who was used on a particular activity of the patient, then you can use the getrequiredresource() command. To use this command, you need to specify a particular activity. For instance, the following command
getrequiredresource(patient,torownum(patient,40),STAFF,1)
will return a reference to the staff member who was allocated to the first requirement of activity ID 40 of "patient". Timing is everything here, because if no staff member has been allocated yet to that activity, then the command will return a reference to the group member associated with the staff requirement! So in other words, this command will always return a valid reference to an object: either a group object or a specific resource member of the group who was allocated. Therefore, you could not simply say
if(objectexists(getrequiredresource(patient,torownum(patient,40),STAFF,1)))
{
// do this
}
but instead would need to maybe say something like this
if(getobjecttype(getrequiredresource(patient,torownum(patient,40),STAFF,1)) == OBJECT_Staff)
{
// do this if a staff member was allocated to activity 40
}
Because I'm not sure of your actual use case, I'm not able to give much more than this. If I totally missed the mark, please give me more background on what you're trying to do, and I'll do my best to help!