Iavra Weak Reference

Iavra

Veteran
Veteran
Joined
Apr 9, 2015
Messages
1,797
Reaction score
863
First Language
German
Primarily Uses
Description

Allows access to objects without holding a direct reference. If an object is only referenced by weak references, it may be garbage collected at any time.

Prerequisites

None

How to Use

A weak reference to <object> is created and accessed like this:

weakref = IAVRA::WeakRef.new(<object>)weakref.objectThe object method will return nil if <object> is no longer accessible. Objects keep a list of backreferences to WeakRef objects referencing them.When a WeakRef is garbage collected, it will remove itself from the backreferences of the object it was referencing.

Note: WeakRefs should NOT be stored in savefiles. The finalizers might not be serializable and the list of references is not stored in a persistant way.

Also, finalizers are not threadsafe, so if you are insane enough to attempt any kind of multithreading, you would need to extend the methods with some kind of lock or mutex.

Terms of Use

Free to use for both commercial and non-commercial games. Please give credit.

Credits

Iavra

FAQ

Q: Why should i use this?

A: I came up with the idea of using weak referencing because i wanted to change the way my Animate Everything works. Currently, every object handles its own animations, but needs an update method to do so.

So i wanted to move all the logic inside my module, but directly referencing the animated object means, that they wouldn't get garbage collected as long as an animation is running on them.

This is why i want to keep a weak reference instead.

Changelog

- 1.00: First version.

- 1.01: Added a hash method to ensure equality in hashs.

Download

Script: http://pastebin.com/jAxqnNdh
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,867
Messages
1,017,061
Members
137,575
Latest member
akekaphol101
Top