GML Dependency Injection - GMWolf

GMWolf

aka fel666
GM Version: GameMaker:Studio 1.x and 2
Target Platform: ALL
Download: N/A
Links: YouTube Video
Summary:
Its often needed to reuse code, but with slight alterations.
Dependency injection is used to keep you code generic, allowing it to be reused in multiple different situations.

Tutorial:
Check out my Channel on Youtube for more tutorials!
 
As always, an interesting video. Nicely produced, and showing examples of how it can be used.

The examples are good, as in some previous video's you'd speak of the concepts, but then never actually show something in practice (I could be wrong, and remembering incorrectly...)

Which, as a not great programmer myself, is important to see. "Show, not tell" I guess :)
 

GMWolf

aka fel666
As always, an interesting video. Nicely produced, and showing examples of how it can be used.

The examples are good, as in some previous video's you'd speak of the concepts, but then never actually show something in practice (I could be wrong, and remembering incorrectly...)

Which, as a not great programmer myself, is important to see. "Show, not tell" I guess :)
Thanks for the feedback!
Good to know examples are important to you. I will try to include then in this style whenever I can :)
 

FrostyCat

Member
Thanks for the feedback!
Good to know examples are important to you. I will try to include then in this style whenever I can
I don't think it's your job to appease people who have not done their homework before watching your material. Suckers suck, so let them suck.

This guy is telling you that you aren't showing the concept in practice, but at 3:17 there is a clear reference to your live example. At 1:50:00 of the live stream, you are clearly seen injecting movement behaviours.

Cripes, this same guy told you last time that arrays and enums are intermediate stuff. Give me a damned break, arrays are elementary material, something that shows up in the first week of an introductory CS course. And enums are but a small section on the Manual page for types, someone with common sense shouldn't take any more than 10 minutes to know what it does.
 
I don't think it's your job to appease people who have not done their homework before watching your material. Suckers suck, so let them suck.

This guy is telling you that you aren't showing the concept in practice, but at 3:17 there is a clear reference to your live example. At 1:50:00 of the live stream, you are clearly seen injecting movement behaviours.

Cripes, this same guy told you last time that arrays and enums are intermediate stuff. Give me a damned break, arrays are elementary material, something that shows up in the first week of an introductory CS course. And enums are but a small section on the Manual page for types, someone with common sense shouldn't take any more than 10 minutes to know what it does.
Wow! Your compassion and understanding are...well, non existent.

You seem to have no grasp of what it takes for people to learn new things. When I said that to Fel666 its from the point of view of someone who hasn't done an introductory CS course - since such a thing didn't exist when I went to school (imagine that - oh wait, you can't, because you lack empathy and have your head up your ass)

"Suckers suck, so let them suck" - what more needs to be said about what a disgraceful individual you are.
 

Homunculus

Member
A great tutorial! script_execute is often overlooked but it's actually one of the greatest features we have right now in terms of flexibility, and you explain that really well.
I used this kind of solution in one of my assets to check all entries in a ds_grid against a condition that has a lot of variations. The loop through the grid is the main function, while the condition is just a script passed as argument. This way I can have code like this (simplified):

Code:
rows = ds_grid_get_rows(my_grid,scr_str_is_short);
rows = ds_grid_get_rows(my_grid,scr_number_is_negative);
Where ds_grid_get_rows is just a script that loops through a ds_grid and returns all the rows that match a certain condition, as defined by the script passed as argument. If I need to test all values against another arbitrary condition, I just add another condition script and pass that as argument.
 
T

The Sentient

Guest
Dependency injection is the name of the programming design pattern. Although it is to be interpreted in a loose way here, as far as I know.
From the title I took it as executable dependency injection. For example VC, DX, libraries etc..

Just had something else in mind, that is all. Nothing negative towards the OP. :)
 

DukeSoft

Member
Going by the title I was expecting something else.
I'm wondering what you had in mind then :p Its a pretty well-known design pattern in OOP.

Nice one Fel, I've been using script_execute quite a lot as well - but I use it a lot combined with asset_get_index(); - I used string names in an array to define the function being called. I dont recall exactly why, but I feel like I was unable to put in a reference to a script? I don't remember, will look tonight on why I did this..

I still hope that one day we we have methods in objects.... Man, would that change GM's flexibility. Imagine that :D obj_rocket.explode(); instead of with (obj_rocket) {event_user(0);}
 

GMWolf

aka fel666
I'm wondering what you had in mind then :p Its a pretty well-known design pattern in OOP.

Nice one Fel, I've been using script_execute quite a lot as well - but I use it a lot combined with asset_get_index(); - I used string names in an array to define the function being called. I dont recall exactly why, but I feel like I was unable to put in a reference to a script? I don't remember, will look tonight on why I did this..

I still hope that one day we we have methods in objects.... Man, would that change GM's flexibility. Imagine that :D obj_rocket.explode(); instead of with (obj_rocket) {event_user(0);}
Well you can do this:
Code:
///obj rocket create
explode = rocket_explode; //notice no function call operator ()
Code:
with(obj_rocket) {script_execute(explode);}
But Yeah, I can't wait for just rocket.explode() being possible.

What's great is that this is all duck typed. So you cvan so very powerful polymorphism.
An explodable object just needs to define the explode variable. No need to extend, etc.

I may make a short video on that.


Asset get index can be great if you are defining these things using a json string, for example.

@Catan there is no real consensus on patterns in GM. So it's up to us to define then:) I feel like this is the closest and most practical analog to injection in GML.
But Yes, it's much looser than in OOP. This is really just using function references. True dependency injection would take a full object as a dependency, but that required too much groundwork for a tutorial.
 

DukeSoft

Member
@Catan there is no real consensus on patterns in GM. So it's up to us to define then:) I feel like this is the closest and most practical analog to injection in GML.
But Yes, it's much looser than in OOP. This is really just using function references. True dependency injection would take a full object as a dependency, but that required too much groundwork for a tutorial.
To be fair, you _could_ spawn instances of objects through some kind of magic controller and have those use code references.. Bad thing is that there is still a huge folder filled with scripts. Being able to define scripts inside of objects shouldn't be that hard to implement for YYG (allthough I could be completely wrong here) and it would be REALLY awesome - using parenting it would make using GM so much more likable. Throw in some callbacks and damn...
 

GMWolf

aka fel666
To be fair, you _could_ spawn instances of objects through some kind of magic controller and have those use code references.. Bad thing is that there is still a huge folder filled with scripts. Being able to define scripts inside of objects shouldn't be that hard to implement for YYG (allthough I could be completely wrong here) and it would be REALLY awesome - using parenting it would make using GM so much more likable. Throw in some callbacks and damn...
They have plans to add inline scripts.
So this would be a thing:
Code:
///rocket create event
explode = script {
    instance_create(x,y, oExplosion);
    instance_destroy();
}
Ofc the syntax will probably be somewhat different.
 

DukeSoft

Member
That would be anonymous functions? Thats pretty neat;
Code:
spawnPlayer = function(xx, yy, blurp) {
    with (instance_create(xx, yy, obj_player)) {
        blarp = blurp
    }
}
That would basically mean there are methods in objects :D
 
Top