Referencing custom struct types: Difference between revisions

m
→‎The user interface: Repositioned LoadReference to be mentioned last.
mNo edit summary
m (→‎The user interface: Repositioned LoadReference to be mentioned last.)
Line 156: Line 156:
     import attribute String Name;
     import attribute String Name;
     writeprotected WeaponReference Reference; // NOT an attribute!
     writeprotected WeaponReference Reference; // NOT an attribute!
    ///Loads a reference to a weapon into this instance.
    import void LoadReference(WeaponReference, bool destroyCurrentInstance=true);
     ///Creates a new copy of this weapon, and returns a reference to it.
     ///Creates a new copy of this weapon, and returns a reference to it.
     import WeaponReference CreateCopy();
     import WeaponReference CreateCopy();
     ///Destroys this weapon and frees its memory (You MUST call this to release your weapon's memory!).
     ///Destroys this weapon and frees its memory (You MUST call this to release your weapon's memory!).
     import void Destroy();
     import void Destroy();
    ///Loads a reference to a weapon into this instance.
    import void LoadReference(WeaponReference, bool destroyCurrentInstance=true);
   };
   };


The properties we've added are all imported as attributes. Again, this is essential to the way this particular workaround works. The actual data is stored in the internal cache, but we need to give the user a way to access that data. The Reference member is an instance of our WeaponReference pseudo-type, and is '''not''' an attribute. We actually want the Reference member to be associated with the individual struct instances, not with the internal cache itself. Finally, we have the methods the user will call in working with references of this type.
The properties we've added are all imported as attributes. Again, this is essential to the way this particular workaround works. The actual data is stored in the internal cache, but we need to give the user a way to access that data. The Reference member is an instance of our WeaponReference pseudo-type, and is '''not''' an attribute. We actually want the Reference member to be associated with the individual struct instances, not with the internal cache itself. Finally, we have the methods the user will call in working with references of this type.
====LoadReference====
This method is used to load a WeaponReference into this struct instance. The optional destroyCurrentInstance parameter controls whether the existing instance data will be freed from memory. Unless you're loading several separate WeaponReferences into a single temporary, you'll most likely want to leave this as the default of true. Failure to free items from the internal cache could cause it to grow unnecessarily large.
  Weapon wpn;
  wpn.LoadReference(sword.Reference); // loads a reference to the sword instance into wpn
  wpn.Damage += 5; // increases sword.Damage by 5


====CreateCopy====
====CreateCopy====
Line 193: Line 186:
     // also, DON'T call Destroy if temp is a loaded global reference
     // also, DON'T call Destroy if temp is a loaded global reference
   }
   }
====LoadReference====
This method is used to load a WeaponReference into this struct instance. The optional destroyCurrentInstance parameter controls whether the existing instance data will be freed from memory. Unless you're loading several separate WeaponReferences into a single temporary, you'll most likely want to leave this as the default of true. Failure to free items from the internal cache could cause it to grow unnecessarily large.
  Weapon wpn;
  wpn.LoadReference(sword.Reference); // loads a reference to the sword instance into wpn
  wpn.Damage += 5; // increases sword.Damage by 5


===The implementation===
===The implementation===
190

edits