How to make a new class?
Posted: Wed Jul 04, 2018 1:19 am
In my scenes, I am starting to use more and more global variables. I wonder if it is possible to create a new class so they wouldn't clutter up the namespace.
_init := (instName)=>{
onSpawn = (e)=>{};
g := Scene.cloneEntityTo(entity, pos);
eID := g.entityID;
s = "scene.my." + instName + " := " + eID;
eval(s);
s := "g.onSpawn = (e)=>{scene.my." + instName + " = entityID}";
eval(s);
g._init = "NA";
g.text = instName;
g.pos = pos + [0.25000000, -0.25000000];
g = "";
onSpawn = (e)=>{
scene.my.myClass = entityID
};
eID
};
_ra_len := (ra)=>{
r := string.length(ra);
r
};
_zyxw := 123;
onSpawn = (e)=>{
scene.my.myClass = entityID
};
collideSet = 0;
text = "myClass";
onClick := (e)=>{
myClass := scene.entityByID(scene.my.myClass);
myClass._init("myInstance");
myInstance := scene.entityByID(scene.my.myInstance);
myInstance._zyxw = "Hello";
myInstance.color = [1, 0.50000000, 0, 1];
text = "" + myInstance._ra_len([1, 2, 3, 4, 5]);
text = text + "\n" + myInstance._zyxw;
myClass = "";
myInstance = ""
};
text = "Tester Box:\nClick me to create an instance of he class that is on the right. Code is in the onClick event.";
collideSet = 0;
makething = {
_ = alloc;
_.thing = 3;
_.getthing = null;
_.setthing = null;
_ -> {
getthing = {thing};
setthing = (n)=>{thing = n};
};
_
}
a = makething; //constructor
b = makething; //again a constructor call
a.setthing(5);
b.getthing; //should still return the default 3
a.getthing; //should return the 5
> m = makemap
unnamed
> m.add(2, "thing")
[2, thing]
> m.get(2)
thing
> m.add(3, "thing2")
[3, thing2]
> m.get(2)
thing
> m.get(3)
thing2
> m.remove(2)
true
> m.remove(2)
false
> m.get(2)
undefined