• Advertisement
Sign in to follow this  

Creating functions in GameMaker:Studio? And pseudo-enums?

This topic is 1856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I am throwing together a prototype for my pet game, and I figured I'd try GameMaker:Studio to make it faster, but it seems like GML is very... limited and confusing.

Anyway, I only need to pass a couple hurdles and I should be able to create a functioning prototype that I can use to test the concepts of the game and get others' input and stuff.

So, the first thing I wanted to do was create some functions that will take care of various calculations and stuff. And I'm already stuck lol. Well, none of the documentation on GML seems to be very explicit on how things are done. I logged into the GameMaker Community and it won't let me make a new post and I searched there and here and couldn't figure out exactly how they worked.

It seems like you make a Script, but I don't know how to define the arguments for it. It seems like the arguments are defined by variables you call argument0, argument1, etc. throughout the Script you make. So I guess the arguments for your "function" are based on how many argumentX variables you write into the Script...

Then the example in the documentation looks like this:

//clamp_x(xmin, xmax)
//maintain x within the given bounds

if x > argument0 { x = argument0; }
else { if x < argument1 { x = argument1 } }

... It looks like the "notes" remind you of what arguments the function can take based on how many argumentX variables are in the code. But then the comments also state that it's supposed to keep x within the bounds... but I feel like argument0 should be what they're calling "xmin" and argument1 is what they're calling "xmax" ... but wouldn't they be the opposite, based on what the function apparently does...?

I'm assuming the documentation just uses a flawed example? That argument0 would be the ceiling and argument1 would be the floor of where you're trying to keep x inside.

So if I want to write a function, do I just put any number of argumentN variables in the Script, and then make comments at the top to remind me how many I had and what kind of parameters this function should be taking?

This seems needlessly complicated X_X; I wish the documentation actually explained how it works. It pretty much just says "Scripts can be used like functions." and then gives that example from above with no further explanation...

Also, since I can't really create an enum with GML or make states with one, let's say I wanted the user to be able to choose one of 20 pets or something, and then this choice will persist for the rest of the game, even between Rooms. I've created a global variable for this, but is it possible to use strings and check for the strings to use it kinda like an enum? Like, let's say they picked Pikachu (I'm not making a Pokémon game; it's just an example) then I could set global.petSpecies to "PIKACHU" and from there on out, on each room, I will need to display their pet. So I will create a function that will, through a tedious set of if statements, check all of the monster names, and then an if global.petSpecies == PIKACHU statement can tell the pet object that displays to change its sprite to the Pikachu set... does that seem like it will make sense? I honestly can't think of a better way to do it using GML :\

I'm using the free version of GameMaker:Studio so I can't create my own extensions using C++, nor can I use extensions others have made... I can only use the tools that GM:S comes with.

EDIT: And what all kinds of arguments can a Script function take? Like, could I pass a variable (rather than a value) or a pointer (are there even pointers in GML...?) to a variable? I'm assuming no, but who knows. The documentation does not explain how this works at all. Edited by nesseggman

Share this post

Link to post
Share on other sites




 If you continue to have issues scripting in Game Maker, I would suggest trying Python with Pygame to quickly create your proof of concept. ( also recommend using the PyScripter IDE )

Share this post

Link to post
Share on other sites

Thanks. I realized my Game Maker Community account was created a while ago but I never activated it through the email, so that's why I wasn't able to post new topics.


The biggest problem I have is the documentation is very vague sometimes, and sometimes leaves a lot unsaid, even if I search all over the place, trying to piece together how it works through all the help topics. The GMC is really hard to search because I'm typically bad at forum searches and usually when I find something with the same keywords, it's usually nothing related to what I'm wanting to know (in this case, I wasn't searching "script arguments" but rather stuff like "script as function" "creating function" etc. hoping to find some overview of all things scripts could do when working like a function)


The more I learn about it, the more GML seems to be useful, just... there's a lot of ambiguity and run-around in writing it sometimes.


I'm going to see if I can create some kind of... object that hides and stays 'instanced' throughout the game so I can make a sort of... class out of it, so I can make them for inventory, etc. It seems possible, but figuring out how to do it is going to be an adventure.


Well, thanks again!

Share this post

Link to post
Share on other sites

OK, I use GMStudio so I can help you.

For that clamping function, it looks wrong to me too.  It takes two arguments, which are argument0 and argument1, which correspond to the minimum and maximum that it will clamp the variable 'x' to.  But it seems you know a bit about reading code because yes, it is like the arguments are backwards.  But, as far as the arguments go, you are right, in that they are argument0 and argument1.  When GM "parses" that function, it looks for uses of those arguments, and that is how it determines how many the function needs, and so doesn't allow too many(or too few) when calling the function.  Also, a pointer I can give you is to look up the 'var' statement which creates a temporary variable that loses scope when the function is done.  If the script gets very complicated, it is more readable to set a few of those equal to the arguments, like this:

var xmin, xmax;
xmin = argument0;
xmax = argument1;

But, as of a very recent update to GMStudio, you can initialize the values on the same line as the declaration, where in previous versions you haven't been able to, though you always have been able to with "normal" variables.  Also, I haven't tested yet, but this behavior may actually be the only way to do it now, though I'm not sure since that update came out.

As far as the "enums" go, I know exactly what you mean because I need them too sometimes.  The way I handle that is with the constants.  Just like enums, each constant is equal to some specified integer, though you can actually make the value a string too if you wanted.  Then when writing your code, you would write the constant's name and the editor would replace all instances of it with whatever integer(or string) you made it.  This is also valid for switch statements, comparisons, etc... with no issues.  The constants you have specified also appear in the "intellisense" of the code editor too.  Also, the constants dialog have a save and load button so you could do so to a text file.  Once you see the formatting, it may become easier for you to input your constants via a text file if you are using lots of them.

About saving the state of the game, in general it is somewhat easy to use global variables.  But, if you have lots of things to save like that, then you can (and it is common to do so) use a few controller objects, that not only store values, but each game loop can do other things, like checking the remaining enemies, levelling characters, or anything else you need.  Also, you can check "persistant" in the dialog for the object, which makes that object not get destroyed when you change rooms, and so it stays around no matter what room you go into.

Well, have fun, and good luck.  If you need more help, you can PM me either here on the GMC, where I'm also known as kburkhart84.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement