- 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
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:
