createHashMapObject

From Bohemia Interactive Community
Revision as of 13:01, 11 May 2023 by Dedmen (talk | contribs) (Inheritance)
Jump to navigation Jump to search
Hover & click on the images for description
Only available in Development branch(es) until its release with Arma 3 patch v2.14.

Description

Description:
Create a HashMap with an Object-Oriented Programming behaviour.

Inheritance: HashMap objects support a simple form of Inheritance.
It behaves similar to the merge command with overwriteExisting enabled. Sub-class methods and properties with same name, overwrite their Base-class values.
Constructor/Destructor/Clone methods will be merged together and executed in sequence.
Flags entry will use overwrite behaviour.
Groups:
HashMap

Syntax

Syntax:
createHashMapObject [classDefinition(, constructorArguments)]
Parameters:
classDefinition: Array of Arrays in format [name, value] OR HashMap.
name can be anything like any other HashMap, but it must be a String for it to be used like an OOP object method (see call - Syntax 3).
Some special values, starting with #, are reserved and expect a specific value type (they are all optional):
  • "#create": Code - this is the hashmap object's constructor
  • "#clone": Code - this is code happening when cloning is done on this hashmap object
  • "#delete": Code - this is the hashmap object's destructor
  • "#str": Code - code that is used to evaluate what is displayed when - must return String
  • "#flags": Array of Strings - case-insensitive flags regarding this hashmap object
    • "noCopy": forbids copying, +_hashMapObject will throw an error
    • "sealed": prevents from adding and removing any keys - key values can still be edited
    • "unscheduled": all methods (including #clone and #create) will be executed in unscheduled environment
  • "#base": Array or HashMap - declaration of base class for inheritance
  • "#type": Any - can be used to give a object a "type name", on inheritance types will be merged into an Array
Note that all entries beginning with # are reserved, the engine might use these internally without their use being documented.
constructorArguments(Optional, default: Nothing): Any - Passed as _this argument to "#create" method.
Return Value:
HashMap

Examples

Example 1:
private _declaration = [ ["#flags", ["sealed"]], ["#create", { hint "Hello!" }], ["#clone", { hint "We were copied!" }], ["#delete", { hint "Goodbye" }], ["#str", { "My HashMap Object" }], ["Method", { hint "Method has been called" }] ]; private _hashMapObject = createHashMapObject [_declaration]; // hints "Hello!" _hashMapObject call ["Method"]; // hints "Method has been called" hint str _hashMapObject; // hints "My HashMap Object" private _shallowCopy = _hashMapObject; // no hint private _deepCopy = +_hashMapObject; // hints "We were copied!" // at the end of the scope, _hashMapObject is deleted and hints "Goodbye"
Example 2:
Inheritance constructor/destructor/copy orderings:
private _animalDeclaration = [ ["#type", "IAnimal"], ["#create", { systemChat "Animal Init" }], ["#clone", { systemChat "Animal Copied" }], ["#delete", { systemChat "Animal Bye" }] ]; private _pigDeclaration = [ ["#base", _animalDeclaration], ["#type", "Pig"], ["#create", { systemChat "Pig Init" }], ["#clone", { systemChat "Pig Copied" }], ["#delete", { systemChat "Pig Bye" }] ]; private _smolPigDeclaration = [ ["#base", _pigDeclaration], ["#type", "SmolPig"], ["#create", { systemChat "SmolPig Init" }], ["#clone", { systemChat "Smol Copied" }], ["#delete", { systemChat "Smol Bye" }] ]; _smolPigInstance = createHashMapObject [_smolPigDeclaration]; // Prints "Animal Init", "Pig Init", "SmolPig Init" _copy = +_smolPigInstance; // Prints "Animal Copied", "Pig Copied", "SmolPig Copied" _copy = nil; // Prints "SmolPig Bye", "Pig Bye", "Animal Bye"
Example 3:
Inheritance type checking:
private _animalDeclaration = [ ["#type", "IAnimal"], ["FurType", { "None" }] // Default implementation ]; private _pigDeclaration = [ ["#base", _animalDeclaration], ["#type", "Pig"], ["FurType", { "Bristles" }] // Override the base class method ]; private _catDeclaration = [ ["#base", _animalDeclaration], ["#type", "Cat"], ["FurType", { "Hair" }] // Override the base class method ]; _instance = createHashMapObject [selectRandom [_pigDeclaration, _catDeclaration]]; if ("IAnimal" in (_instance get "#type")) then { // Check if a object instance is or inherits from a specific type _instance call ["FurType"]; // Now that we know it implements IAnimal, we know that "FurType" method will be present };

Additional Information

See also:
call createHashMap

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note