All files / object-instance get-parent-object-instance.function.ts

100% Statements 9/9
100% Branches 5/5
100% Functions 1/1
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32              1x 1x               1x     2x 1x   1x     2x   2x 1x      
/**
 * Exports get parent object instance function.
 *
 * @module object-instance/get-parent-object-instance.function
 */
import type { AgtkObjectInstance } from '@agogpixel/pgmmv-ts/api/agtk/object-instance/object-instance';
 
import { getObjectInstance } from './get-object-instance.function';
import { getParentObjectInstanceId } from './variables/get-parent-object-instance-id.function';
 
/**
 * Get parent object instance.
 *
 * @param childInstanceOrId Child object instance or ID.
 * @returns Reference to parent object instance or `undefined`.
 */
export function getParentObjectInstance(childInstanceOrId: number | AgtkObjectInstance) {
  let childInstance: AgtkObjectInstance;
 
  if (typeof childInstanceOrId === 'number') {
    childInstance = getObjectInstance(childInstanceOrId);
  } else {
    childInstance = childInstanceOrId;
  }
 
  const parentInstanceId = getParentObjectInstanceId(childInstance);
 
  if (parentInstanceId !== undefined && parentInstanceId !== -1) {
    return getObjectInstance(parentInstanceId);
  }
}