Use Python to Enhance The Godot Game Engine

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] Godot is awesome but it is a game engine so if you try and push it to do things like web scraping displaying notifications Bluetooth or machine learning you're bound to hit some walls luckily we can achieve these things by leveraging the power of python and I'm not talking about the Godot python library but rather running scripts in the background while your game runs in the foreground to get started we'll want to create a folder within our games directory to hold our python files this folder will also contain our python virtual environment if you don't know what this is it essentially packs Python and any libraries you're using into a file so you can use your scripts from anywhere to create one we'll need the terminal if you don't use the terminal often don't be scared I promise this is very easy we'll first navigate to the directory we just created using the CD command then we'll type in the command python-mvenv followed by whatever you'd like to name your environment in my case I just named it venv and that's all you have to do to create a virtual environment you should see you're sitting in the directory here I've created a simple python function that sends a notification to the Mac OS notification center you can certainly use a script that runs by itself but here I'd like to send arguments from within my godogame to do this I'll want to import the sys library then I'll use this as RGB function to capture the arguments past the command line in an array and then pass those to our notify function within our Godot script we'll want to create variables containing the paths to our interpreter which lies within our virtual environment and the script we want to run here I'm using the project settings globalize path function to get the global Paths of both this works great in the editor but will not work on export to do that we'll need to get the directory containing our exported executable using this line here then we'll just add the relative paths from our interpreter in script to the global executable directory I'll also add a simple if statement that changes The Interpreter and script paths to what we had previously if we're running in the editor to run the script we'll use godot's os.execute function which executes a file at the given path with the arguments passed as an array of strings here we'll pass the interpreter path as the file to be executed followed by an array containing the script path as the first argument followed by any arguments we'd want to pass to that script my recording software blocks notifications but after we run the script we can see the notification in the Mac OS Notification Center in order to get it to work on an exported version First export the game normally I'll copy our python files directory navigate to the directory of our games executable and then paste it there on a Mac I just right click on the gamer app and then select show package contents the executable directory is just called Mac OS and I'll paste our python files right in there now when we run the game it'll locate the directory we just pasted into and have access to our interpreter and script here I'll do just that and you can see that our notification pops up in the notification center now while this works great for simple scripts what if we want a more complex program that is running continuously in the background and communicating with Godot here's an example where I'm web scraping the YouTube live chat and communicating that information to my Godot game to achieve this I use Python's built-in socket library to send and receive UDP packets between Godot and python each packet contains a blight encoded Json that contains the information I'd like to send back in Goodell I create a UDP server class to communicate with the python script I use the os.execute function much like I did before except this time I set the third parameter blocking to false so it doesn't alt our script and I also capture the returned process ID in a variable and append it to an array then when I quit the game I use the OS dot kill function to kill the python process I can check if I'm about to quit the game using the notification virtual function and checking if what equals main Loop dot notification quit request in the process function I continually pull the server to see if a packet is available if one is I get the packet convert it to a string and then handle it as a Json we can respond to our python script using the put packet function we just need to ensure and handle this in our python script finally we can save a reference to our python script by capturing peer in a global variable hey everybody thanks so much for watching I hope you found this video useful and if you did please drop a like below and potentially subscribe to my channel also maybe check out one of these two videos you know whatever floats your boat so thanks so much for watching and have a great day foreign
Info
Channel: ACB_Gamez
Views: 28,391
Rating: undefined out of 5
Keywords: Godot, Python, game development, game engines, programming, game design, 2D games, 3D games, game logic, physics simulations, game mechanics, game programming, game scripting, scripting in Godot, Python in game development, godot 4.0
Id: z2MHuWEDUNw
Channel Id: undefined
Length: 4min 17sec (257 seconds)
Published: Thu Feb 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.