It's worth to mention that this design pattern can be implemented to the existing code.
The current example (made by me) isn't showing this way, so i'm going to demonstrate one neat trick with use of metatables that allows us to easilly edit the existing class to use the flyweight pattern. Suppose that we have a class called Npc, let's just assume that it looks like more or less like this:
The problem is, that if we change this class by adding the static keyword before the attributes like this:
The error will occure when we try to get the static field via the object like this:
What we can do, is to add the _get metamethod which will be invoked each time, when we try to access the object field which doesn't exists.
Now, when we try to access the field via the object, everything will be working as it should.
There is only one problem, having a static fields as types: int, string, null, bool, float will cause a problem with directly trying to modify them.
First of all, we can't modify the static field via the object, even if we provide the _set metamethod, it will still raise an error.
There is a way to add the desired functionallity by using the class.newmember or class.rawnewmember.
IMO however it's not required to supply the _set metamethod. Anyways, that's all i wanted to show, ofc _get metamethod can be implemented in different way allowing us to access the data from for example a table.
The current example (made by me) isn't showing this way, so i'm going to demonstrate one neat trick with use of metatables that allows us to easilly edit the existing class to use the flyweight pattern. Suppose that we have a class called Npc, let's just assume that it looks like more or less like this:
Squirrel Script
- class Npc
- {
- _maxHealth = 40
- _maxMana = 10
- // more fields and some methods & constructor.
- }
The problem is, that if we change this class by adding the static keyword before the attributes like this:
Squirrel Script
- class Npc
- {
- static _maxHealth = 40
- static _maxMana = 10
- // more fields and some methods & constructor.
- }
The error will occure when we try to get the static field via the object like this:
Squirrel Script
- local npc = Npc(...) // ... means some arguments
- print(npc._maxMana)
What we can do, is to add the _get metamethod which will be invoked each time, when we try to access the object field which doesn't exists.
Squirrel Script
- class Npc
- {
- static _maxHealth = 40
- static _maxMana = 10
-
-
- function _get(idx)
- {
- try
- return getclass()[idx]
- catch (msg)
- throw null
- }
- }
Now, when we try to access the field via the object, everything will be working as it should.
There is only one problem, having a static fields as types: int, string, null, bool, float will cause a problem with directly trying to modify them.
First of all, we can't modify the static field via the object, even if we provide the _set metamethod, it will still raise an error.
There is a way to add the desired functionallity by using the class.newmember or class.rawnewmember.
Squirrel Script
- class Npc
- {
- static _maxHealth = 40
- static _maxMana = 10
-
-
- function _get(idx)
- {
- try
- return getclass()[idx]
- catch (msg)
- throw null
- }
-
- function _set(idx, val)
- {
- try
- getclass().newmember(idx, val, "", true)
- catch (msg)
- throw null
- }
- }
IMO however it's not required to supply the _set metamethod. Anyways, that's all i wanted to show, ofc _get metamethod can be implemented in different way allowing us to access the data from for example a table.