Godot's GDScript for Programmers: Static Types & Memory Safety

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi I got distracted so I started making a visual story game in the gdau game engine and game engines aren't usually my thing but with it getting more attention recently and being open source I thought I should learn about how to use it and today I'm not going to go much into a lot of the details of how the engine works and things I've learned there I'm going to focus on the GD script programming language and especially comparing it a bit with python so think of this as like GD script for programmers from someone who still new in the engine and the language and and in gdau there are multiple language options available but the easiest to reach for is GD script and it's integrated well with your scene tree environment and with the editor generally including things like debugger and a certain amount of hot code reloading and profiling and more so GD script while it's a bespoke language for gdau it's a language you might still want to reach for today I'm going to focus on type safety and memory safety and rather than working the engine where there's a lot of nice things like documentation available and autocomplete I'm going to work in vs code I think it's possible I could set this up better for more features from VSS code as well but for now I just have syntax highlighting and rather than Sirus game code I'm going to focus on very simple examples it turns out you can run GD script scripts from the command line as long as you extend main Loop or scene Tree in your script SL class because every GD script script file defines a class class whether you have these words here or not let's run it we see gdau headless quit after we get started and run this script here which runs the underscore nit function and we could ignore this other class for a minute we'll get to it soon so in addition to some verbosity we see the message hi printed out here and here's an equivalent in Python although I have some other stuff ready for exploration later but mostly I'm I'm calling main main defines a string thing and prints it let's try that out it says High although it may be more fun if I time that as well you can tell there's a little bit of overhead in getting Gau started compared to python we can also type check our python file which finds no errors say I try reassigning this to an INT does my piie care and the answer is yes it does and there are different type check this for python I'm using some version of mypie here so this was a bad thing as far as mypi is concerned let's do something similar here in GD script thing equals 5 let's run it and we see an error here says can't assign a value of type int as string notice it also says here parse error I find it gives that message whenever the error happens at static analysis time if the error is only Dynamic you won't see that message so what's happening here is that if I say colon equals in GD script it assigns the static type of thing as being whatever is inferred from this side if I get rid of that equals no errors so let's put that colon back or we could even be explicit string and we get an error message now worth noting that there's another difference here already between GD and python even though my PI says this is an error by default python itself doesn't care unlike gdau that gave us a definitive error message and it even goes further than that say we just declare a variable in Python and don't assign it to anything what happens H it's not actually defined say we just declare it in GD script and run the program didn't have an error message what's going on here well let's inspect a little bit more and we see our length was Zero when declared as a string that variable exists and gets a default value for Strings which is an empty string we can even compare it like this true or we can say not that and apparently empty strings are considered to be falsy in GD script if I don't declare a type we get null by default so some kinds of value in GD script are not nullable and the top type in GD script is called variant other types that aren't objects or nullable include int float array dictionary and more and yes you do have to declare your variables in GD script this is no good so people say that GD script and python are a lot alike some of the syntax is similar it includes indentation and GD script is primarily a dynamic language like python there's a lot of things that are very different in the semantics by the way just to prove this thing is a class let's instantiate the class defined by the script as a whole I haven't tried this yet quite let's see if it works oh I probably should use correct syntax new in Gau and apparently that doesn't work so I might need to ask gdau to reassess all the different types are okay that's done and now it did work although we had stack Overflow from the infinite recursion here so let's not do that anymore instead let's create something of type something okay so now we have a new kind of thing we have an instance of something that prints out the string form of ref counted whenever you define your own class the default super type is ref counted and ref counted is a subtype of object and ref counted things are the only things that get reference counted so presumably we have a reference defined here it gets printed goes out of scope and gets cleaned up what happens if we make a circular reference does gdau handle that and as reference counting let's try thing. internal equals thing let's try this now and we see warning object DB instances leaked to exit and let's try running that with verbos to see what happens verbos okay it says it was something of type something that wasn't freed which gives us at least some information and when you're running in the gdau engine editor you actually have access to things like monitors on memory and objects allocated over time so anyway if I do want to have circular references we should do weak ref for that and now if I try running this there's no leak weak references also exist in Python but they're not required for reference counting to clean up Cycles because python handle Cycles automatically so for example example let's create a new something and let's assign that thing's internal to itself again the question is how can we verify this actually gets cleaned up well that doesn't tell us too much yet and there could be different ways of going about this to get information out of python to say whether something's been cleaned up or not but I'm going to go about it by actually making a weak reference to the thing itself so internally it's not a weak reference it just has circular referencing here but I'll make a weak reference to the thing to see if it disappears and gets collected let's do this first of all let's Force some collection and see if we still have access through our weak reference and it looks like we do but what happens if we delete thing meaning we no longer have any local references to it and then we collect and try to get our weak reference access again if we do that we see none here out of our second attempt to access a weak reference after we no longer have a strong reference to it so here we can see that python has indeed collected something despite internal cycles and again that's something GD script does not do so you need weak references for your cyclical links if you want to make sure it gets released but it also turns out in GD script that you can make things that aren't reference counted and if you want to clean up those things manually you call free on them let's try freeing first a reference kind of thing and see what happens can't free a reference okay by reference they mean ref counted thing in earlier versions of the G engine they just called it reference so that's no good let's make something that extends object instead where again ref counted is a subtype of object and ref counted is a default super type when we make a new class but if we explicitly extend object then we have to free manually ooh instance is leaked exit let's free it okay so now it got freed and I printed a freed object cuz cuz once it's been freed that's the type that gido sees it as let's try freeing twice let's see if we have manual memory management do we have memory safety risks here let's see here and we see that we have an error for double free detected that there's a cord DP here does bother me and I don't know if they guarantee detecting double freeze if they do you should be Memory safe although crashing is no fun by the way I have seen the gdau editor crash and I'm not sure what kinds of bugs cause that perhaps just internal things I have not found definitive statements of whether they consider GD script to be Memory safe or not let's try doing things with an object after it's been freed and we see here that we can't do that and this isn't even a crash there's no core dumping here this is just checked and reported as an error of something bad that I did so I think it's possible a GD script is designed to be Memory safe and just earlier today I happened to see a news report claiming that python is memory safe as well but that's not necessarily true okay so here I'm using C types and treating something as a pointer to a string and getting its value out and I'm using a trick here to get a value for an address that's possibly in the memory space of the program this is built into python it's designed for being able to interact with C libraries for example you can also just use it to subvert memory safety let's try that oh I forgot to save save run again and we see here that I'm getting arbitrary random memory from inside my program so no python is not memory safe by default because they Expos features you can just use by default that subvert memory safety I don't know if there's obscure errors of GD script where I can do the same Oh and before I go one other thing on type safety and GD script let's do some static type inference of an array of inss and print that out let's see what happens got three four five okay so the question is is this typed as an array of ins or is it typed as just an array and they do call it array instead of list in gdo let's try saying thing Z equals High what happens it's fine to do that internally it's variant type but we can if we want to enforce the type of what's inside of the array we could either cast it here and then get our error or we can be explicit on the other side either one gives a tip to gdau that only ins can be stored inside this array cannot assign a value of type string as int and again this is being statically checked and just to prove we could assign otherwise let's say two here okay 245 let's go back to the broken thing and just to prove this is being done at run time and not just at compile time let's do something else VAR other equals thing and let's change something inside of other so now other is not statically typed let's see what happens if we try this we get a runtime error of can't assign string into typed array of type int no more parse error it's not statically checked but it is still checked by the way when you have these static errors ones that show as parse errors they actually get highlighted in the gdau editor while you're typing we just don't see that here in vs code at least not with the configuration I have and sadly dictionaries which do exist in GD script just like in Python they do not have type arguments to control what's stored inside of a dictionary and something you do have in Python so for example I can say VAR more dictionary equals something print more run this way to say anything like dictionary of string to int for example that doesn't exist again what you can do in Python although again typically erased at runtime anyway so hopefully that's something they might add in the future but meanwhile as we see here what is claimed to be a type in GD script is checked dynamically as well as statically so it might even be a sound type system I haven't verified that either but if it is presumably there's Poss possibilities in the future for things like precompiling things to native code for semi- highspeed execution I don't know if that's on the radar either but it seems like something that could possibly happen in the future anyway this has been an intro to one part of comparing GD script and python understanding some differences about the language and some of how GD script works for programmers I hope it's been fun and if you like the video be sure to subscribe bye all
Info
Channel: Context Free
Views: 2,965
Rating: undefined out of 5
Keywords: godot, gamedev, gdscript, godot game engine, python, mypy, memory safety, static typing, programming languages
Id: 7jv73TjViAo
Channel Id: undefined
Length: 13min 54sec (834 seconds)
Published: Mon Mar 04 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.