Workers and Pylons - Python AI in StarCraft II tutorial p.2
Key Takeaways
Builds a basic StarCraft II bot using Python and the SC2 API to collect resources and produce workers
Full Transcript
what is going on everybody welcome to part two of the artificial intelligences in starcraft 2 with python tutorial series in this video what we're doing is building on the last video continuing to build up our starting game methodology so the things here are pretty pretty simple and pretty rule-based you have to you know create workers distribute workers collect your resources and then basically kind of from that point you can start to strategize how you want to do things but first we have to get through quite a few just kind of basic things now I forgot to mention in the previous video most of the time people ask hey what are the prerequisites to this series my answer is honestly always the same but I should have given it anyways in it like none there are never prerequisites to really anything like you just follow along and then anytime you hit something that you don't understand research it go to Google look for a tutorial on it whatever so for example you know you you might not understand inheritance well you can go to Python program it type in and hair it search boom inheritance easy maybe you don't understand async what's what's that async OH async IO basics okay and then you can learn about async IO right so if you're having trouble with anything just research it or ask in the comments for someone to help you because if if you try to learn everything before you're gonna learn like new topics a lot of times you're just gonna find yourself always learning the basics in that's really boring so anyways there you go those are the prerequisites slash a life lesson so so let's get started what we've got here is we've distributed the workers the next thing is we want to produce more workers so let's go ahead and do that now in order to produce workers I did link to this in the in the text-based version of the tutorial but this is like a good a good little resource for someone like me who has no idea what they're doing so at least this is for the Protoss units and you can kind of see what it takes it's like the tree for building things so you start off with a nexus a nexus well with a nexus you produce or you can produce pylons and assimilators bike but you basically start with the ability to do all these things like so with a worker now the Nexus itself produces the little probes the workers then you from there once you have a nexus you can build a gateway or a forge and then from here you can build like if you have a gateway you can build a cybernetics core if you have it and then clearly it looks like if you have a cybernetics core Wow look at all the things it opens up for you so there's like this path that you can take to various things we're gonna revisit this soon but you have workers here now in order to produce workers you just need an axis so that's relatively simple so what we need to do is that since the Nexus produces it we need to be able to reference a nexus so from SC to Constance we're going to import Nexus and also probe because we want to produce a probe so now we're gonna do is we're gonna come down here and like in this case this on step it runs every step I don't really know what a step is it's like not I think it's not like a second I don't think it's a fixed time even I'm not really sure it might be like a game tick which might actually be a fixed time I don't think it's tied necessarily to frames that would be kind of weird but it's some sort of iteration I just don't know exactly what it is if you happen to know what each step in the game is let me know anyways so in here this this little on step is gonna contain all the methods that you want to run it just it's gonna call all the methods you want to run per step but then you might not get so lucky that distribute workers was already written for us awesome but that's not gonna happen like so for now like to build workers our function for that is not going to it doesn't exist yet or our method rather so what we're gonna do though is we're just gonna say let's say a weight self dot build underscore workers so we still call it the same as this but it just doesn't exist yet so we have to define it so we're gonna come down here async def build workers again you're gonna need to pass self here otherwise we're ready to actually build this this function so what we're to do to build I'm sorry I'm gonna keep calling at a function it's a method anyways we're gonna build this method what we need to do is for next nexus in self dot units that are of Nexus so for every nexus that we have that is ret ready and also has no queu e must have a hard time with Q so what's this mean so the Nexus itself has to be ready ie it's built it's not pending and then no Q means it's not producing something else right now so for humans who are slow and can only focus on so many things at any given time there are key build queues so you could say okay I want this building to produce five workers and then you go deuce other things so like when one worker is done the next one starts up okay that's what the queue is for but when we're running programs the program can manage all of your buildings and all of your units simultaneously so there's really no reason to to add to a queue you're just kind of allocating resources for no good reason like the it's it's slightly disadvantageous to have a queue so no reason to do it so now what we're gonna do is so for each Nexus that is ready what do we want to do well we definitely we'd like to build a probe but we need to make sure we can actually afford one so if self dot can underscore afford so if we can afford a probe now can't afford is a method that's tied to self you don't see it defined here so huh it must be inherited and it is it's one of the methods in there so if we can afford a probe then what do we want to do well we want a probe so all we need to do is we can just say a wait oops not all caps oh wait self dot do so what do we want to do well we're gonna do a Nexus so it's just this Nexus object that we found that was ready with no queue we want a Nexus dot train and we want to train this probe okay and that's it now now we're building workers let's go ahead and run this and make sure it actually it actually works I should pop up there it is so barely immediately once we have resources at least we should see that we start to produce a worker and there it is so it's already warping in a probe right now which is just the the word we use to describe bringing in units for this class so warps attend and immediately you can see it actually produces another one instantly it almost looks like we did call it despite being waited in queue I guess that's how quick it went interesting anyways that wasn't really supposed to happen I just this first time I've noticed that it does that but you've already allocated the resource when you do that that's the downside let's see what happens when we produce this one yeah immediately to armaid interesting so what you can see now is we need more of these quote unquote pylons because if you look up here this is how many this is your supply we have 15 out of 15 so we've got 15 units we we can't create anymore now with the Protoss class the way that we create units or increase our supply is with pylons now pylons do two things one they increase your supply but they also have this what's the name of the thing I can't think of what it's called it's like a psionic I think psionic something let me pull up the the tree here so I linked to this in the text-based version but yeah it's like this is like the tree that you know what you need to build certain things so you start off let me do this man do this okay so you'll start off with the Nexus and you can build pylons and assimilators and assimilators are what we're gonna build pretty soon but then let's say you want to build like an army well you're gonna need to build a gateway and with that you get the zealot but then if you build the cybernetics core you get access to all these other things and then from there you get access to all of these things that you can build and so on I mean there's when I'm interested in here's the pylon what do they call that a psionic matrix so you can only bring it you can only build buildings and warping things as long as it's within the psionic matrix and each pylon adds to that psionic matrix so one thing we could do I think it will be the shortcut would be BP probably nope I thought it would be B no it's gonna be an e so one thing you can do is when when the program is running if you're curious about something and you want to learn more about the game you can like click on one of your workers now eventually it's gonna get a new task so you got to be kind of quick but there's always shortcuts so B is build and then you find like I want a pylon so e so you could hit B e pretty quick and then so let's say okay I want a pylon right here so if we build this pylon it's gonna take a second to build but once it's done building it's gonna add to our count here for how many units we can have so immediately you'll see these guys will start warping in actually and then you can see this little circle around here and basically that's your psionic matrix and so you can build more buildings inside that matrix so as you can see now our max units is up to 23 click over here sure enough it's creating more workers for us so what we need to do now is build these probes so I'm just going to exit and so we want to build I'm sorry not probe pylon everything's a P so so the next thing we're going to do is a weight self dot build underscore pylons so then we're gonna come down here I'm gonna clear this out just so I have space a sink def build pylons and in order to build the pylons we're gonna probably need to access their type so hi lon and now we want to do is create some logic so we don't want to just like constantly build pylons it's a waste of resources and it gets in the way and all that so so we need to have some sort of logic so the way that we're gonna do this is we're gonna say if self dot supply left and again supply is kind of an unfortunate naming convention if you ask me because supply references and so as units by the way if it's like I'm pretty sure units co-responsible buildings and your actual like combat units and your workers anyway that's beside the point supply left is how many it's like supply is interchangeable really with population so supply left so if your supply left is let's say less than five what do we want to do well we want to build a pylon but what if we're already building a pylon so if the supply left is less than five and self dot already underscore pending I'm sorry if and not self already pending pylon so if we're not already building a pylon what do we want to do well we want to grab the nexuses so we're just gonna say nexus is equals self dot units nexus dot ready so it's a nexus that already exists so if nexuses dot exists if self dot can afford so if we can afford that python python pylon I think it's about breakfast time for me man I can't think a weight and in this in this case we want to await self dot build a pylon but when we build things we also have to specify where so again this is a very complicated thing and we're at least for now we're going to use near equals this is not first so we're just gonna build it close to our very our initial Nexus so which is also a funny sounding word for a command center that you can have multiple nexuses anyways we're gonna build a new the first one so already though I so like right now everything is rule-based in but but where you could begin to start using things like reinforcement learning or just deep learning in general is even this where do you want to place that Nexus and like how do you want to distribute your workers how many should go in different places these things are extremely complicated so even just that little tiny thing like where exactly should we place this pylon that's a very complicated thing right and so to be best because because of that psionic matrix that's actually a very complex question so anyways this one's gonna be super simple we're just gonna build it yeah like close to a nexus but but if you wanted to be very efficient about it you would need to be smart have a much better reasoning for it we probably should have oh it crashed what did we do do we not Pat okay seen this one before so in case you run into this one it's okay I guess that we run into errors because probably gonna hit it at some point type error build pylons takes zero positional arguments but one was given what so what's happening is we forgot to pass self so because this is a method self the object is gonna get passed into here we need to make sure when we define this method that we include self here because that's what's happening right we're actually run self is running this method so self is passing later on we might build functions that don't need itself to be passed but in this case you need to pass self anyway we'll try again we probably could turn off real time now but we'll see the result of this pretty quickly and then probably probably after we see this oh my gosh did we hit another no no it's running okay I just didn't see the game the game was supposed to pop up and it didn't because I was clicking around okay hopefully we're already building workers so any any moment now as long as once we get the required resources we should find that we build a pile on there goes a pile on right now over here so yeah just some way it's just built kind of anywhere within range of the Nexus which ideally like our next pylon and something that we're gonna see like if I was to speed this up we would find that it's gonna probably build the neck well it doesn't necessarily have to but ideally you'd probably build the next pylon like I'm pointing to the skirt you know maybe the next pile in here or here probably here because it's more defensive back here no one can come from from back here they're all gonna come from up here anyway you build maybe your next pylon here but we might find that instead it builds the next pylon just right next to this one which is pointless so yeah you probably eventually want to optimize that one quite a bit but for now that's good enough so already let's see it should be less than five so once I one more is warped in we should see that another pylon is built did he come in for some reason I guess we're still why are we still five down I swear we were okay anyway so immediately when that other one was built boom there goes the next pylon anyway and then why are we that's kind of interesting that there's two at the same time I'll have to check the logic on that one like no queue and must just be happening so quickly anyway okay so we're building pylons we're creating more workers we're expanding everything but like I said it's basically pointless for each each patch of minerals to have any more than three workers on it at any given time so we're going to exhaust our area here quite quickly so what we need to do next is actually expand to other areas so we're gonna do that and then probably work on building so like right now we've got workers but we don't have anything to actually you know build an army so I think in the next tutorial what we'll do is both expand to new sites you can see them on the map like this is a new site this is a site this is a site and basically all these little sites will have these little vespene geysers and this is where you're gonna collect this element here this best being gas anyway so the next one we'll do is we'll start collecting the gas building what we need on here to collect the gas building the buildings that we need to create our army and then probably maybe after that start building our army because even because at that point the strategy already forces its way in with things like you know we only have so much supply so how much of that should be our army versus our workers and all that stuff so anyways that's all for now if you got questions comes to search whatever feel free to leave them below as always thank you for all your support and your sponsorships and all that cool stuff and I will see you in the next tutorial
Original Description
Welcome to part 2 of the StarCraft II AI tutorials. In the previous tutorial, we wrote a basic bot that just collects resources with available workers. Now, we'd like to get more workers so we can get supplies faster!
Text tutorials and sample code: https://pythonprogramming.net/workers-pylons-starcraft-ii-ai-python-sc2-tutorial/
Chat with us on Discord: https://goo.gl/Q9euv3
Support the content: https://pythonprogramming.net/support-donate/
Twitter: https://twitter.com/sentdex
Facebook: https://www.facebook.com/pythonprogramming.net/
Twitch: https://www.twitch.tv/sentdex
G+: https://plus.google.com/+sentdex
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from sentdex · sentdex · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
Python Encryption Tutorial with PyCrypto
sentdex
Python's Logging Function
sentdex
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
wxPython Programming Tutorial 4: Panels
sentdex
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
wxPython Programming Tutorial 8: Custom Button Images
sentdex
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
Basic PHP Tutorial 12: Associative Array
sentdex
Basic PHP Tutorial 14: Foreach loop
sentdex
Basic PHP Tutorial 16: Include and Require
sentdex
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
Basic PHP Tutorial 4: Variables and Comments
sentdex
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
Basic PHP Tutorial 1: Intro to PHP
sentdex
Basic PHP Tutorial 3: HTML with PHP
sentdex
Basic PHP Tutorial 9: While Loop
sentdex
Basic PHP Tutorial 10: Switch Statement
sentdex
Basic PHP Tutorial 2: Print and Echo
sentdex
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
Basic PHP Tutorial 19: Finding words in strings
sentdex
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
Basic PHP Programming Tutorial 24: String similarity
sentdex
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
Sikuli Tutorial 1: Visually programming in python!
sentdex
Sikuli Tutorial 2: Program visually in python!
sentdex
Sikuli Tutorial 3: Program visually in python!
sentdex
3D Bar Charts in Python and Matplotlib
sentdex
3D Plane wire frame Graph Chart in Python
sentdex
Raspberry Pi Part 1 Introduction
sentdex
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
Raspberry Pi Part 11: Remote Desktop
sentdex
Twitter Analysis: How to rank a user's influence
sentdex
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex
Related Reads
📰
📰
📰
📰
ChatGPT for Photography: Take Better Photos with Any Camera
Dev.to AI
ChatGPT for Event Planning: Host Memorable Gatherings Without the Stress
Dev.to AI
SciDraw Alternative: Paper Banana for Scientific Figures
Medium · AI
AI-Powered Polish: Generating Client-Ready PDFs for Catering Proposals
Dev.to AI
🎓
Tutor Explanation
DeepCamp AI