The Struct Accessor - Replacing DS Maps - GameMaker Studio 2.3.1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so the struct is a new way of storing data which has been introduced in game maker studio 2.3 most people are probably aware of these by now they've been out for a little while there has been some talk among game maker users that struck completely obsolete ds maps now and while they didn't quite have all the features the ds maps had at first they had a few new features such as the ability to inherit from each other and the ability to contain methods and uh run code in their scope but with a 2.3.1 update that came out not too long ago a few more features have been added to struxx and the old ds map system has pretty much completely been obsoleted in every uh in every way structs are more lightweight than ds maps and you can use the operator on them if you want like i said earlier they can inherit from each other and they can contain methods which run code in their scope structs are automatically garbage collected which means you don't have to explicitly destroy them when you're done with them which means as long as the garbage collector is turned on you don't really have the potential for a memory leak which is always nice to have 2.3.1 introduced a few things such as the json stringify and json parse functions so now the ability to write structs and arrays to json and read them back out is now a built-in part of game maker and i have to say this is something i wasn't really expecting but there is now a new accessor that is part of game maker and these are fees with structs let me show you what i mean so i am going to create a file of code script one is fine i'm not really big on the names here i should make the text bigger though uh so let's create ourselves a struct i'm just going to create an anonymous one i'm not going to use a constructor let's give it some let's give it some variables that'll do for now uh as we know from before we can access the things inside the struct with the dot operator so if i were to show a message uh structs.name or age by the way in case anybody has been wondering someone asked this not too long ago the reason that name is highlighted in red here is because um there are a few variables which are used in structs that pertain to sequences i think name is one i think position is one no rotation is another one uh there are a few more it doesn't really mean anything if you use sequences and you use some of the built-in uh functions regarding them which return structs uh they'll have some variables it gets a special color in the ide for that reason game maker doesn't really know whether this this struct here is uh one that actually has a variable called name or not and it's just highlight highlighting it anyway uh anyway if i can run the game we can get a pop-up with with ted and elite which is the best age we know this already so what we can do now is we can treat this we can use this with square brackets and if this was a ds map we would be able to access the things inside it with the accessor which goes like question mark and a string containing the um the key that we want to look up in the ds map name in double quotes age and double quotes whatever um this will not work now because this is a struct and not a ds map and uh game maker is going to complain about that but what we can do is use the um the dollar sign and we can use that as an accessor to access the stuff inside his truck directly so you can see name ted uh age lead now given that the dollar sign is a symbol that is generally used by american programmers with american keyboards i do not know and i will put it on the screen right now if that would be localized to something like the british pound symbol or some other currency symbol for other parts of the world hey i kind of hope it is if you live in a region where the keyboards that they manufacture don't have that symbol on them it can be kind of a pain in the neck to you know use them you'll have to memorize the alt code or put it on a hotkey or copy and paste it from the internet or something so as you can oops as you can imagine with a with an accessor this goes both ways name and we can assign something to that member of the struct using the accessor as well as reading from it so we can say name is going to equal what's a funny name that should be noticeably different from what we had before i am not actually going to see that because i'm not actually displaying it uh we can we can set albus percival wilfred brian dumbledore i almost got through that without um messing it up uh we can set that to this struct's name and we can print it out again and you can see it changes so something that you're allowed to do in ds maps although it's not necessarily a good idea is to use uh keys which are not strings for example you could say in a ds map uh struct with a key of like 50 or something which isn't a string it isn't in a double quotes and this would work as long as the ds map obviously has a key with that name and the same is true for structs i'm going to comment this out because i don't really need it anymore so let's come up with a value for this the struct key the struct variable which is not a string i'll just use like the number 50 again or something like that and it's going to let us do this again just as a um a ds map would and you can see the data is there you could use as far as i know any data type as the key inside the struct accessor you could use a string obviously which is probably what you should be using you can use a number as i am here right now you can even use other structs which you really really probably should not do but if you really want to don't let me stop you this brings up kind of an interesting point in that one of the ways you can create a hexadecimal number is to prepend a dollar sign to the number so if i were to say let's get rid of that if i were to say var number equals 50 that's that's a number we know what numbers are i can also say number equals dollar sign 50 and this would no longer be the decimal five zero it would be the hexadecimal number five zero which is in base ten eighty which um as we can see this has the potential to cause some confusion when you try to use the struct accessor because what is um what is the value that is actually going into the struct is it a 50 with the struct accessor or is it in an 80 in decimal five zero in hex um i see it's uh i see it's accepting it either way so it seems that the struct accessor takes priority over the uh hexadecimal notation and honestly if nothing else that's probably another good reason that you shouldn't just use numbers as the keys inside structs uh using the struct accessor anyway there are a few uh more functions pertaining to structs which uh some of them have been around for a while and by a while i mean since 2.3.0 came out some of them are i believe new editions in 2.3.1 you have variable variable variable struct exists you have variable struct get uh variable struct get names variable struct to get names count uh what is our variable struct removed and variable struct set these are similar to some variable related functions that existed in in previous versions of game maker going back a long time previously i was not a fan of these you have things like variable global exist variable instance exists i don't like the practice of not knowing whether or not a variable exists ahead of time someone just screamed outside at the very least you can always always always define your variables either in an object's creation code or in like the game's creation code if it's a case for a global variable and if you ever don't know whether or not a variable has been defined or not you've probably already done something wrong and unless you want to start tearing your hair out in the next couple days you should probably just define your variables in the create event i don't have the same distaste for these functions when it comes to structs as i did with when it comes to instances especially now given that structs really are just a an abstraction and to an extent a replacement for ds maps not officially when i say that like ds maps are still a part of gmail you can still use them i just don't think there's really very much reason to use them in several of my projects i have just gone through and removed all ds maps completely and i think it makes my code quite a bit nicer in a very technical sense that's all instances were in in previous versions of game maker they were just let's call them less accessible abstractions of ds maps and you could treat them as such i just don't think that's a very good way to look at them since the way that they tend to be used in practice looks a lot more like a an object with a bunch of variables already defined anyway back to the point these will do what pretty much they say on the label you can use variable struct exists to check whether or not a struct actually has a variable inside it so let's pass it a struct and pass it a um a name for a variable which can be something like i don't know bananas and this is going to return false because uh the struct does not have a variable called bananas and if we pass it something instead more like name what is the line that duplicates the current line of code you're on i think it's ctrl d anyway name returns is one or also known as true because uh it does contain a variable with that name control d duplicates the current line i do that a lot by accident just a reminder this has to be a string this can't be a a simply a variable unless that variable contains a string if you try to do this the game will crash because the variable doesn't exist and you need to check to see if a uh a name or some other value exists inside the struct uh variable struct get again this does exactly what it sounds like this just returns the um it's basically the longer form of this up here using the struct accessor what's that that's the stark accessor variable struct to get names returns an array of all the variables that the instructor contains if you want to for some reason iterate over everything in a struct you can use this it's not something that you really do often unless you're literally just using a struct as a jumbled bag of data as with ds maps the keys are stored in a for all intents and purposes random order they're not alphabetized they're not stored in order that they were added or anything like that don't count on the variable names being in any particular order the analog for this with ds maps would have been something like ds map find first or ds map find last which would return the first or last keys in ds map and let you iterate over them with ds map find next and ds map find previous um this is nicer you can just loop over them with a for loop in the array and you don't have to do anything stupid to iterate over the uh over the ds map a variable struct to get names count is just the number of variables that are inside the struct the number of things that are inside the struct it's basically the length of this array should you need it variable structure move if you're using this like a like an object in a game you don't really ever need to remove variables if you're using them like a ds map this is the equivalent of ds map what was it delete a ds map delete which would remove a specific key from a ds map and variable structure set that's exactly what's going on here okay ds maps are dead long live destruct so as i say in pretty much every video this isn't something you have to do it's not like you now need to drop everything and um and and get rid of every dsmap in your game and replace it with a struct i believe there is a small performance increase if you access something in a struct with dot operator like this rather than like this because game maker can hash this value ahead of time and it doesn't have to do it during run time like it would have to if you just if you just gave it any old string to use as the key in a ds map but and i can't stress this enough this is like the micro optimization to end all micro optimizations don't worry about the amount of time it takes to access variables inside a struct gml is a slow interpreted language and if your game is performing badly it is 100 going to be because of the other things that are happening in your game and it's not because of the amount of time it takes to access a a variable inside a struct so and i'm trying to think if there's anything else worth mentioning and there's one one more minor thing that comes to mind so if you try to access something in a structured instance that did not exist under normal circumstances let's say show message struct and let's try to display ted's height on the screen or something like that height is not a variable that exists in the struct and if you try to access that with the dot operator game maker will crash as it often does when you try to use a variable that doesn't exist whereas if you were to use the struct operator or destruct accessor i'm sorry instead of crashing and causing an error this will instead return undefined that's a minor difference it is a difference that you can take advantage of if you want to because it is a fast way to see if a a key exists inside a struct this is how ds maps worked if you had a ds map and you tried to pull out a um a key that did not exist it would give you undefined instead of crashing at least i think i had quite a bit nicer than trying to check everything with ds map exists constantly you could just obtain the value from the ds map and check if it's undefined or not some people may think the opposite some people may think it's dumb and pointless and not something that's ever worth doing uh but regardless trucks work the same way if you try to access something that is not inside a struct uh using the accessor you will get undefined instead of a of a hard crash so these things are really cool again if you want to use structs now to replace every ds map in your game oh you can do that i have done that at least in one project did i say all of my projects earlier in the middle of the video when i mentioned it i don't remember if i said all or one because i have only so far done this in one project and it's um it's made my code quite a bit easier every time you don't have to type ds map destroy and worry about a memory leak is a is a good time in my book you don't have to worry about whether or not something exists an instance exists or a ds map exists they're just kind of there and then the garbage collector will take care of it when it runs next really nice anyway that's it i'm going to stop talking now i am probably not going to make this code available for download in the video description via github uh it's only nine lines long and it's 100 just demonstration code and i don't think there's really any point in cluttering up the uh the github repository organization thing that i made just for this the best way to learn how this stuff works is just to mess around with it yourself at any rate so my name is michael i like wizards and dragons and making games i have a patreon so if you want to contribute towards these things being made there's a link to that in all the usual places otherwise i try to post about two of these game dev videos a week i hope you found that useful and i will see you all later special thanks to edward halt indie punch jason and zenith for supporting these videos if you want your name in the credits or to hear yourself shout out at the end head over to the patreon page in the video description to join the fun you
Info
Channel: DragoniteSpam
Views: 1,271
Rating: undefined out of 5
Keywords: DragoniteSpam, game maker tutorial, gml 2.3, game maker structs, game maker lightweight objects, game maker 2.3 structs, game maker 2.3 map, game maker struct map
Id: OGVdIxIPGOo
Channel Id: undefined
Length: 16min 56sec (1016 seconds)
Published: Sat Nov 14 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.