Gamemaker Extensions Using C# 1: Introduction

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up everybody I'm here today with the another game maker studio tutorial today I'm going to be explaining how to write an extension for game maker inside of c-sharp so for those of you who don't know J maker allows you try two extensions inside of other languages namely C++ that can be used with in game maker so part of the problem with this is that game maker can only call unmanaged coasts however c-sharp compiles down to what's called managed code and I believe this has something to do with a c-sharp memory management and it's garbage collector so we need to make sure that anything that we write for game occurs to do inside of c-sharp will compile down to unmanaged memory parts are a managed code or at least look like it as far as game maker is concerned and thankfully there's a way that we can do that so I'm going to go ahead and hop into visual studio there's a few things to go mate as you can see right here I need you need visual studio or a c-sharp compiler of some kind and you need new get because we need to download a library for this to work now if none of those things make sense to you maybe this is a bit advanced for you this is going to be the simplest possible way I can explain this however and I'm going to do my best to keep it beginner friendly so inside a visual studio I clicked on file new project we're going to create a new class library for the.net framework I'm going to go ahead and call this GM tutorial then click okay [Music] all righty so inside of here you can see it automatically creates some stuff for your crates with public class and as in some of the most common using statements so first things first we actually need to add in another library I'm going to go ahead and right click on references going to manage NuGet packages this will open up a GUI package manager so you can see we have installed updates and browse go ahead and click on browse inside of the search bar you need to type in DLL export this has all things that we need in order to make our c-sharp code look like C++ code as far as game maker is concerned so we're going to go ahead and download this waiting for this to happen it's going to open up a little wizard as you can see here and inside of here you get to actually choose which namespace this library is included in so because this live library is very similar to many of things found inside of the interrupt services library I'm going to add it into that so it's going to be system runtime not enter services however you don't have to do this you don't even have to change at all this is just for myself because this application is going to be so small not particularly important but after that you can go ahead and click on configure and it will go ahead and add that in for you so now come in here we're going to add in another using statement is going to be using system dot runtime interrupt services so we can use library that we just installed now we need to do one last thing game maker is actually a 32-bit program or at least the executive bullseye tikrit are so that means that when we compile our library or build our library we need to make sure that it is for x86 archetypes so go ahead and click on configure it go ahead and go right here go ahead and click on configuration manager on this one for the platform we're going to go ahead and click on the drop-down menu can click on new and the new platform is going to be x86 I just go ahead and click on OK there and go ahead and close that so now we compile our library for x86 archetypes so with that done we can actually get started so we're going to go ahead and make this class a public class because any function that's going to be exported needs to be a static function now this just makes it so you will not forget it's been for slide the compiler this way all right so let's go ahead and write our function doing stuff that they make here can be a little tricky sometimes because game maker can only pass in pass in doubles and strings to external libraries and it can only return or you can only I can only receive doubles and strings as well so everything they have to do has to be done with those two value types which is fine for this we are going to write a function is going to be public static double going to return a double is going to be called a square and it's going to take and a double and that's going to be value and it's just going to squared value so it's giving value x value extremely simple right you may think that we're finished but we actually have to do one last thing in order to port this function here to look like a C or C++ function we need to go ahead and add an attribute to it attributes are special like tag type things that you can apply to functions properties classes etcetera that will force it to compile differently can force the compiler to compile a program differently in this step in this case we need to make sure that the compiler compiles us down to look like C plus self code now which was this library that we got earlier is going to be relatively simple so the attribute that we need is DLL export okay this tribute is going to take true values it's going to take in a string and it's going to take in a calling convention already so the string is going to see how external code will actually see this function so we're going to go ahead and call this X square it does not need to share a name with this it's just going to be for simplicity sake this can be anything that you want it to be and the calling convention is going to be this one here cdecl now the reason for that is whenever a whenever you compile your code all of the methods are actually obfuscated a little bit so for the case of this this could be an example it's not I actually don't know exactly how it obfuscated this is like I said just an example but internally it may make the function look something like this come however we don't want it to look like that we want it to look like square right oh I may have gotten a little bit too much there at a and the reason that it does this is because when you overload a function or yeah when the overload function it needs to be able to distinguish between the two different versions I don't know type let's say for example we also have a overload of the section that would be something like this and this would just return the area of a rectangle of course you wouldn't actually call it this um but for our case we will so the compiler needs a way to distinguish between this version of square and this version of square so it adds in a few extra symbols at the end of the function name in order to be able to do so however we don't want it to look like that we need to make sure that it looks like this to external code otherwise we'll never be able to figure out exactly how to call it and this calling convention makes it so it does that exactly um so now that we have it done like this we can go ahead and actually and build our project so I'm going to go ahead and click ctrl shift B and that will go ahead and build it unless I'm not clicking to try the skin one more time safekeeping already so that is all we need to do from C sharp let's go ahead and jump back into game maker and now we can actually add our extension into here so in order to do so we need to create a new extension I'm just going to go ahead and call this shark it can be anything you want it to be and we're going to go ahead and add a file this is going to be the library that we just built so go to wherever your projects are for me I just have a pen to my quick access here I'm going to go ahead and click on GM tutorial once again click on GM story go into your binary go into the x86 one just to make sure click on debug and you can go ahead and click on the dot DL else that was formed earlier when we built it so go ahead and click open on that now we actually need to create the function definitions from inside of game maker as well so we can call us whatever you want I'm going to call it square with a little s to follow the game maker naming convention however the external code is going to be square with biggis now whenever you put in here make sure that it is not the name of the function but what you put in here when you call the DL export those are going to be this is what's actually going to be seen from game maker now for the help this is the helpline here is going to be essentially like if you have done this inside of a script tag called square with the three triple slashes now sorry not three two expressions with the triple backslash there but you don't actually need to add the back slashes it does it will automatically click this good for you alrighty now for the return type we're going to be returning a double and our calling type is going to be this one go ahead and click on add here the add minus R value add and subtract arguments so if you accidentally add a too many you can press the minus sign to get rid of one and it's not going to be string where you can go ahead and go over and click on the drop down menu some time this min this button isn't there so if it's not a C so you can see here it's not there and there's nothing you can do if that happens just go ahead and click off of the box and now you'll see it will show up so go ahead and click on the double and that's pretty much all you need so now let's create an object that will actually call it I'm just going to do a simple version of this this is going to say show debug message so we can actually view the result and now we're going to call slick spell correctly and it's going to try a 10 to square of the number 3 and we need to actually put that object inside of the room so it so if that actually instantiated now I'm going to go ahead and click run and this should work as you can see 3 times 3 is equal to 9 so everything worked as intended so that's pretty much it for this tutorial this is an extremely simple version of it just to help you get started part of the reason I decided to do it this way is because I'm going to be doing a couple of more advanced tutorials however getting the extension to work from game maker using sea shark is a little bit complicated because it's not meant to work this way but I'm glad that says so I hope this was able to help you and I will see you next time
Info
Channel: Mystborn
Views: 4,911
Rating: undefined out of 5
Keywords: programming, c#, gamemaker
Id: QIMxPkKoQhM
Channel Id: undefined
Length: 13min 0sec (780 seconds)
Published: Wed May 03 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.