Reference Source

Collector

This plugin gives you an easy and beneficial way to reuse instances inside of the Vylocity game engine, as well as slow down the garbage collector.

Installation

ES Module

import { Collector } from './collector.mjs';

IIFE (Immediately Invoked Function Expression)

<script src="collector.js"></script>;
// ...
window.CollectorBundle.Collector;

CommonJS (CJS) Module

const { Collector } = require('./collector.cjs.js');

API

Collector.toggleDebug()

Collector.setMaxLimit(pLimit)

Collector.collect(pCollected, pCollection)

Collector.isInCollection(pType, pNum, pCollection, [pArg[, pArg[, ... pArg]]])

Diob|Object.clean() event

Diob|Object.onDumped(...pParam) event

Diob|Object.onCollected() event

Examples

Get an instance

const toyBin = []
Diob
   Toy
World
   onNew()
      const toyForParty = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 1, toyBin) // DiobToyInstance 

Get an array of instances

const toyBin = []
Diob
   Toy
World
   onNew()
      const toysForParty = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 3, toyBin) // [DiobToyInstance, DiobToyInstance, DiobToyInstance]

Recycle an instance

const toyBin = []
Diob
   Toy
World
   onNew()
      const toyForParty = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 1, toyBin) // DiobToyInstance
      // a few moments later
      JS.CollectorBundle.Collector.collect(toyForParty, toyBin)
      // We have gotten a toy from the toy bin, did something with it for a while, and returned it to the toy bin. Recycling rocks!

How to use instance.onDumped

Any time an instance is removed from a collection array it will call instance.onDumped() if it is a defined function.

const toyBin = []
Diob
   Toy
      onNew()
         World.log('toyBin was empty so i was created!')
      function onDumped()
         World.log('I was removed from toyBin so you can use me again!')
World
   onNew()
      // At this point in time toyBin has no toys in it so Collector creates the instance for you
      const toyForParty = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 1, toyBin) // DiobToyInstance
      // We have just put the toy into the toybin
      JS.CollectorBundle.Collector.collect(toyForParty, toyBin)
      // A few moments later we decide we want to get that toy back out
      // Instead of creating a new instance of the toy, aRecyle cleaned and removed the toy you previously put into toyBin
      const toy = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 1, toyBin) // DiobToyInstance

How to use instance.onDumped with parameters

If your collection array starts off empty you must handle the condition of Collector calling new Diob and in return the event function instance.onNew being called!

const toyBin = []
Diob
   Toy
      onNew(pName)
         this.name = pName // 'toyBall'
      function onDumped(pName)
         this.name = pName // 'toyBall'
World
   onNew()
      const toyBall = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 1, toyBin, 'toyBall') // DiobToyInstance  

Using instance.onDumped like a pro

Since instance.onNew and instance.onDumped are virutally the same, they can run the EXACT same code under each of them. So it is best to create a handler function so you don't have to duplicate code. In this case setup is the handler function.

const toyBin = []
Diob
   Toy
      onNew(pName)
         this.setup(pName)
      function setup(pName)
         this.name = pName // 'toyBall'
      function onDumped(pName)
         this.setup(pName)
World
   onNew()
      const toyBall = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 1, toyBin, 'toyBall') // DiobToyInstance  

Using instance.onCollected

Any time a diob is collected, ERecyle will call instance.onCollected if it is a defined function.

const toyBin = []
Diob
   Toy
      function onCollected()
         World.log('I have been collected')
World
   onNew()
      const toyBall = JS.CollectorBundle.Collector.isInCollection('Diob/Toy', 1, toyBall) // DiobToyInstance
      JS.CollectorBundle.Collector.collect(toyBall, toyBin)

Global Dependency

Collector relies on the VYLO variable being globally accessible.