#Can I inject a static into a struct?

1 messages · Page 1 of 1 (latest)

quaint hedge
#

From my C# background, where I very often used extension methods to enrich existing classes, I wonder, if something like this is possible in GML too.

I know that I can just write a normal function, that does the job, but I am looking for a more fancy solution to this.

What I want to do:
I want to inject a method into the scribble object.
draw is nice and good for 99,99% of my usecases, however, sometimes I do not want the text to be rendered to screen directly, I want it on a surface.

Something as simple as that (using Canvas library from @sweet hound )

canvas.Start();
scrib.draw(x,y);
canvas.Finish();

just redirect the draw to a surface, nothing SciFi here.

But I don't want a function, I want to be able to invoke

scrib.drawToCanvas(canvas,x,y);

So, basically, I want to enhance the scribble object with a new method, drawToCanvas and it shall do the job.

Can this be done? create a new static method in another class?

sweet hound
#

Perhaps, I may know of a rather hacky solution @quaint hedge

#

Well, it's technically valid but I find it to be hacky

quaint hedge
#

My ear just grew to pizza-size... go on 😄

#

i listen.

sweet hound
#

The one thing I'll note is as a sort of downside, the constructor itself does need to be initialized at least once, so it doesn't really work as well

#

(Statics don't exist until the function is called)

quaint hedge
sweet hound
#
// Obtain statics from a constructor function
var _statics = static_get(constructor_function);

// Define a "static" method. In this case, we'll write it as how static methods are usually handled
_statics.drawToCanvas = method(undefined, function(canvas, x, y) {
  canvas.Start();
  draw(x,y);
  canvas.Finish();
});
#

That is more or less a "direct" hack

quaint hedge
#

so, just replace "constructor_function" with the name of the scribble class and that's it -- sounds good

sweet hound
#

Yeah, but the class must at least be created/called once

#

So it's actually not as elegant of a solution

#

The alternative, and that's a lot better

quaint hedge
#

np - i create one at startup, i can do that

sweet hound
#

I'd just create a separate constructor class that inherits the scribble element class

#

And then create a static method that way, and create a small function to wrap around it

#

That's the most other kind of alternative I could think of, without really doing anything super hacky

quaint hedge
#

the scribble function is not a constructor - it deals with weakref and maybe returns a new __scribble_class_element instance.
so i think, the hacky way is the better one for me

sweet hound
#

scribble() itself isn't, but it does spin up a constructor if one doesn't exist for the relevant text

#

So you could make a copy that instead spins up a child of that scribble element

#

It doesn't require any weird order of execution, and it's essentially universal

quaint hedge
#

yeah ok, i derive from __scribble_class_element and kind-of clone the scribble method, say scribblex and it has almost the same code but instantiates my derived class

#

then i could create a clean second layer of existence... ok thanks, you gave me something to think about

sweet hound
#

Yep, and it also at least ensures that you can maintain some form of compatibility, without something else breaking

#

I figured I'd show both ways nonetheless since they both can be used for different scenarios

quaint hedge
#

mhhm the first one has the drawback of "needs an instance first" and the second the drawback of a code-copy of a function from juju, which I have to, at least "look at" when a new scribble release comes, to keep it in-line with his version. both not perfect, but the latter really has the advantage of an isolated second layer with less... punch-into-the-face-hackiness

sweet hound
#

Mhm yep!

quaint hedge
#

fine, i'll sleep over that and decide tomorrow - thanks mate!