Intro to Python Programming with Mosh – Pt. 02

Intro to Python Programming with Mosh – Pt. 02

March 14, 2019

Intro to Python Programming

Tutorial – 02

Youtube – Learn Python 3 for Machine Learning & Web Development [2019]

By: Programming with Mosh
Progress

End Time: 48:40

Next Part of Tutorial: Arithmetic Operations

NOTES

Strings

You can use single or double quotes for strings, but there is a difference for certain applications. For example, if you want an apostrophe in your string, you need to use double quotes to define the string. An example for the reverse is that you use single quotes to define the string if you want something to be in double quotes within the string.

Strings can also be defined with triple quotes. This allows you to create a multiline string.

You can call a character index of a string with brackets (i.e. string[0] will return the value of the first character in that string.). An interesting feature is that you can use negative indices to start at the end of the string and count backwards.

You can also call a chunk of characters with two values separated by a colon. If you don’t place a number before the colon, 0 will be assumed. If you don’t place a number after the colon, the length of the string is assumed. Using these together, you can perform a simple duplication of a string (i.e. another_string = string[:]).

The final check with this colon/index syntax was what would happen if you entered:
string[1:-1]
Where string was ‘Jennifer’. This returned ‘ennife’, which appears to indicate this syntax always returns values from left to right if possible, as well as showing that the beginning value is inclusive, while the final value is exclusive. To further test this, I tried string[3:1] and this did not return any value, which fit my thought on “left to right is possible”.

Formatted Strings

Formatted strings are used to make complex strings easier to visualize their output. Formatted strings are indicated with quotes starting with an f (i.e. f’text’). They then use curly brackets to create holes in the string where variables can be placed.

String Methods

The first basic method is len(). This returns the value of the number of characters in a string. This function is general purpose though, so it can count the number of objects in a list as well for example. The fact that it is general purpose is what makes it a function, as opposed to a method.

We looked into the find() method for strings. This takes an input of characters and returns the index of the first instance of those characters in the associated string. You can enter a single character or entire words to search for. Multiple characters will return the index of where those characters start.

The replace() method takes two inputs; the first input is what it searches for in the string, and the second input is what it replaces that with.

Next we used the “in” operator. Using the format of:
‘characters’ in string_variable
This returns a bool value of whether those characters are found within the string value indicated.

TERMINOLOGY

  • Method: in object oriented programming, a function that belongs to or is specific to some object

SUMMARY

The main difference between a function and a method is that a function is more general purpose, where as a method is something belonging to a specific type of object.

The string topics covered here gave me a lot of new information. I’m interested to see if there is any overlap with some of this in C# for Unity programming since there’s a lot of really helpful functions and methods covered. The formatted strings are especially nice to keep code nice and tidy instead of having those ugly string concatenation lines.

Intro to Python Programming with Mosh – Pt. 01

March 13, 2019

Intro to Python Programming

Tutorial – 01

Youtube – Learn Python 3 for Machine Learning & Web Development [2019]

By: Programming with Mosh
Progress

End Time: 29:35

Next Part of Tutorial: Strings

NOTES

This tutorial covers the complete basics of Python, starting with installing it, so it is as beginner friendly as you can get. The entire tutorial will use these basic foundational teachings to lead into creating projects to show how to use what you learn in a functional sense. These projects include a shopping site, an AI system for helping a user get music to match their preference, and a spreadsheet processing program.

Upon installation, it was important to “Add Python to PATH” for Windows (which I am using). I wasn’t sure if I did that since I had installed before, so I found that this references the Path in environment variables and just adds the path of your Python folder to this list. I added my Python path to this list in hopes that would resolve that issue. The tutorial says this is critical to follow it, so I will see if I get any errors.

Next we grabbed a code editor. The suggested one to use was PyCharm. There were no special installation features or plugins mentioned to download/install so I just installed the bare minimum.

In Python, you can define a string with single or double quotes. You can also multiply a string with a number to print that string that number of times (i.e. ‘*’ * 10 gives **********).

Standard variable types:

  • int: integer values
  • float: floating point values (those with decimal points)
  • string: words/text
  • bool: true/false
  • lists
  • objects

Next we finally got into receiving input from the user. This uses a Python function simply called Input(). The input from the user for this will also initially be created as a string variable, so make sure to convert this to int, float, etc. for your needs.

TERMINOLOGY

  • Expression: a piece of code that produces a value
  • Variable: allocation of memory for a specified value

SUMMARY

So far the basics seem very similar to what I’ve already learned in C++/C#, with the standard variable types and simple functions. I am speeding through these early parts, but don’t want to jump ahead since I want to make sure I don’t miss anything that does happen to differentiate from what I already know. This is also a good refresher for basic programming terminology and getting another view on the underlying basics of coding.

Coroutine Basics in Unity

March 12, 2019

Coroutines in Unity

Resources On Coroutines

Youtube – Coroutines – Unity Official Tutorials

By: Unity

Youtube – Introduction to Game Development (E21: coroutines)

By: Sebastian Lague

Youtube – Coroutines In Unity – What Are Coroutines And How To Use Them – Coroutines Unity Tutorial

By: Awesome Tuts

Youtube – Unity3D – 2 Ways to Start & Stop Coroutines (the good & bad ways)

By: Unity3d College

Coroutines are one of the tools I still don’t fully understand how to utilize in Unity so I wanted to gather some resources to really learn the basics. I am hoping this helps me use them more effectively and understand them better than as a method that lets me do something “after a while”.

The resources I’ve gathered cover a wide amount of information on coroutines. There are details on the terminology used around them with the most technical aspects of how they are made up to small examples on their different uses in Unity scripts. There are also different methods of starting/stopping them covered and how to properly control them.

Complete Beginners Python Tutorial by Programming with Mosh

March 11, 2019

Complete Beginners Python Tutorial

Youtube – Complete Python Tutorial for Beginners (2019)

By: Programming with Mosh

I wanted to try using Unity’s ml-agent toolkit and its initial setup uses a lot of Python so I got interested in learning the basics to better navigate the toolkit and its full potential. This was one of the first tutorials I came across and its size and recency made it an attractive choice to cover a lot of ground that shouldn’t be outdated for any reason. Its size is also a bit of a drawback at over 6 hours, so I will most likely break this up and cover it in the upcoming weeks.

Unity ML Agent Setup in Windows

March 11, 2019

Setting Up Using ML Agents in Unity

Windows 10 Setup

Unity – Machine Learning
Unity – Windows Installation

I was interested in trying out Unity’s machine learning agents toolkit so I started attempting to get everything setup and installed today. I have no prior experience with Python, so that took a bit of getting used to.

I had some trouble setting up initially as I followed the link in the documentation for the Python download which led me to get Python 3.7, but Python 3.6 is what is used in the installation notes. When I went to install the TensorFlow component, I couldn’t get that to work in 3.7. Afterward I went back and got Python 3.6 and setup a 3.6 environment and got the same error initially, but then got it to work after actually activating the ml-agents environment (so I may have been able to solve the error in Python 3.7 with this change).

Otherwise the setup went rather smoothly. The excessive use of Python encouraged me to look into some tutorials to start learning the basics to handle this Unity ml-agent system better.

The Windows installation also included an extra section for getting into GPU training for the ML-Agents. This seems like it adds a lot of extra complications so I am going to stick with the basic setup for now, but I may come back to this once I get a better grasp of Python and the ml-agent toolkit.

Wwise Integration with Unity

March 10, 2019

Using Wwise with Unity

Game Audio

Youtube – Wwise and Unity: Getting Started with Unity & Wwise, Part 1
Youtube – Wwise and Unity: Ambient Sound Sources, Part 2

By: WestSideElectronicMusic

Wwise is a powerful audio editing tool specifically molded towards producing game audio. It also has strong integration with the Unity game engine that makes working between the two much smoother.

These tutorials help show the basic integration steps to make sure the two softwares can communicate with each other, and then starts to get into the basics of using WWise to produce interesting audio for your games. Between the two of them, we covered adding in a simple footstep sound effect and a torch sound effect.

The footstep audio was done to show the minimum required to add audio into Unity through Wwise. It was mostly important to note the need for creating audio effects in Soundbank objects in Wwise, then generating the objects to import into the Wwise editor in Unity. These objects then need to be placed in the Unity scene to actually be accessible as audio clips. The footstep effect will also be built upon in later tutorials to add some randomization as well as modifying the audio for stepping on different terrains.

The torch example got into some stronger features of WWise, focusing on 3D spatial audio and randomization. The fire sound effect for the torches could be made 3D, which allows it to have various audio effects depending on the distance/orientation of the object relative the player hearing it. We created a simple volume depreciation over distance with a distance cap, as well as adding a low pass filter to mimic the real world effect where lower frequencies are heard further away from an object than higher frequencies.

The torch example also got into the basics of creating randomization of sound effects. In Wwise, we created a Random Container object, which can hold several audio effects to randomly select from/play as well as modify randomly to give a play effect varied sound outputs. We duplicated our fire sound effect 3 more times in this container (4 in total), and just moved the starting/ending looping times of play in the different audio files to make them feel a bit different. We then also added a pitch randomized variation to one of these sound effects to give even more varied feels (I believe you can also have this on the Random Container itself to apply to all the objects, that might be what the tutorial wanted to do and just misclicked).

When you create these Random Containers, you just make sure to generate the Random Container object and use that as your audio clip. In Unity, you would reference the Container as your sound object and it contains all the information to produce random varied effects based on what you created in WWise.

Overall Wwise seems like a very powerful tool for creating audio effects for your games, especially in the Unity game engine as it has decent integration capabilities.

Unity Player Pref Editor

March 8, 2019

Unity Player Prefs Editor Script

Editor Script

Unity – Player Prefs Editor in Unity

By: Romejanic

As I was finishing my Unity tower defense tutorial, I wanted to look into deleting player pref values to do bug testing for the level unlocking system. When I searched how to do this, I came across this very helpful Unity editor script that gives a few very basic functionalities dealing with player prefs in Unity.

This tool lets you set or get the values assigned to player pref variables you’ve created. This can be useful to double check exactly what a player pref is set at currently. It also has an option to delete the value assigned to a specific player pref, or just delete all player pref values. This gave me the exact option I needed, as well as giving a nice option for checking how the game works for a new player just starting the game.

Tower Defense Tutorial – Brackeys – Ep. 28

March 8, 2019

Tower Defense Tutorial

Episode 28 – Winning Levels

Youtube – How to make a Tower Defense Game (E28 WINNING LEVELS) – Unity Tutorial

By: Brackeys
Ep. 28

This tutorial sets up the system for what happens when the player beats a level.

We began by creating a UI panel for all of the UI that should appear when the player completes a level.

Once we had that setup, we looked into creating some functionality for this screen. This started by creating a RoundsSurvived script to just place on any round counting objects (one in the game over and level won screen) to display the text properly. As an added effect, we wanted to animate the round counter to count up from 0 to give it more of an impact than simply displaying the number.

To get this round counter animation effect, we used an IEnumerator for its ability to WaitForSeconds. We created a while loop that would count from 0 to the number of rounds the player survived one at a time, with a WaitForSeconds of 0.05 each time. This provides the rapid counting effect we’re looking for to spice up the round counter.

The tutorial goes over a bug where enemies could die multiple times because of the fact that Destroy(gameObject) is not a very immediate process within Unity. Since our enemy script just checked if health reached 0 or less to determine if an enemy would call the Die method, which would then destroy it, sometimes this would allow it to call the Die method multiple times if it got shot repeatedly in a small time frame. To solve this, we just added a bool isDead, and have the check to call the Die method see if health is at/below 0 AND !isDead to ensure this is only called once (the Die method sets isDead to true).

PROBLEMS

As indicated in the comments for the tutorial video, there is an issue when selecting the Menu button upon completing a level. The Continue method is where the levelReached Player Pref is updated, so if the player simply selects the Menu button instead, the levelReached is never updated so the player will not be able to select the next level in the level selector.

SOLUTIONS

To keep the levelReached and levelToUnlock variable in the CompleteLevel script (as opposed to the Game Manager) and fix this issue in as clean a way as possible, the comment suggested moving these variable updates to an OnEnable method. This is nicer than just adding it to both methods, and keeps it within the same script which is helpful if I intend on making the GameManager into a prefab in the future.

SUMMARY

  • Rule of Thumb: “Bigger” highlight animations for bigger buttons
  • In Unity, if you want your button to be just text, you can also drag the Text element of it into the Target Graphic of the Button (script) so the text is the specifically clickable area
  • IEnumerators are used when you want a method that can be paused to continue at a later time (can be as small as a frame or two)
  • Destroy(gameObject) in Unity is SLOW. This is always a good piece of code to look around if you are having strange bugs/issues.

Tower Defense Tutorial – Brackeys – Ep. 27

March 7, 2019

Tower Defense Tutorial

Episode 27 – Unlock Levels

Youtube – How to make a Tower Defense Game (E27 UNLOCK LEVELS) – Unity Tutorial

By: Brackeys
Ep. 27

This tutorial gets into creating a system for locking/unlocking levels.

We started by going into our LevelSelector script and creating an array of UI Buttons. This would hold all of the buttons that allow you to select a specific level on the level select screen. We then added a for loop to go through all of these buttons and intitialize them as non-interactible buttons.

To go along with making the buttons non-interactible, we added a Button Disable animation. This was a simple animation that makes the button much more faded out than normal to make it clear that it is non-interactible. To make sure this effect is removed in an interactable state, we added an alpha animation for every other button animation state to just set the alpha back to 1.

Next we needed a way to keep track of the player’s progress to determine what levels they have access to. We wanted this to persist even if the player closes the game, so static variables are not enough. To do this, we need to save data out to a file. We used Unity’s player prefs as the simplest way to do this. We started by creating an int levelReached, which uses PlayerPrefs to GetInt levelReached. When using a get variable command, you also specific a default value to use if there is nothing associated with this value currently (i.e. this is the first time the player is playing the game), so we set that to one so the player will always have access to level 1 initially.

Now we setup that initial for loop that was just disabling every button to only disable buttons if their index (+1) is greater than the levelReached variable. The levelReached variable is now set at the end of a level if the player completes it, increasing the possible level options.

In testing the level select setup, they discovered the bug I mentioned as a possible issue in a previous tutorial. You can sometimes get a scenario where the enemiesAlive in the middle of a wave reaches 0, which signals to our system that the wave is over, even though there are actually more enemies left in the wave. They ended up using my suggested solution as well: just set the enemiesAlive variable equal to the total number of enemies in the wave at the very start of the wave.

They then go over some issues with modifying the game in its current setup. The changes we made in the Game Manager only apply to the current scene, so they have to be redone in every other level (or since we don’t have anything yet, delete the current ones and duplicate the updated level scene). The simple work around for this was to make objects like the GameManager a prefab so when you update it in one scene, it will be updated for every scene. Then, for an even stronger method they suggest is that Unity allows you to put everything that is repeated in each level into its own scene, and only the objects that actually change between levels are their own scene.

SUMMARY

  • Look into serialization for saving player data; many more advanced games use .json or .xml to store player date
  • Unity uses Player Prefs to do a very simple version of saving data
  • When using get/set variables, make sure to use the EXACT SAME identifier name for the variable. This will not generally return an error if entered incorrectly as it will just set it to the default value instead.
  • Unity has a system to let you load different scenes in at once, look into properly using this feature (this was the suggested ideal method if you want to create a lot of levels)

Tower Defense Tutorial – Brackeys – Ep. 26

March 6, 2019

Tower Defense Tutorial

Episode 26 – Fading

Youtube – How to make a Tower Defense Game (E26 LEVEL SELECT) – Unity Tutorial

By: Brackeys
Ep. 26

This tutorial gets into creating a level select menu for the player.

This would require its own scene, so we duplicated the Main Menu scene as a place to start since it’s already fairly empty. We started by setting up the hierarchy of UI assets for the level content selection area. It was noted that this is a general way to setup a scrolling menu for selecting objects in Unity.

The hierarchy is as follows:

  • UICanvas
    • Title
    • Levels
      • ScrollRect
        • Content

We then added a button to our Content UI object, and gave that object a Grid Layout component. This allows Unity to place everything in an even grid layout for you. We also added a Content Size Fitter. This helps allow the object it is attached to scale to fit the content placed in it. We also made sure to move the pivot anchor point of this UI object to the top of the object to make sure it scales downward from that point instead of out from the center.

We added a bunch of buttons to help see that all of the scaling and clipping was working properly with the UI elements. To help keep them from flowing out of the menu (which also works with the idea of creating a scrolling menu), we added a Rect Mask 2D component to the ScrollRect object which blocked out child objects that were not within this rectangle space.

Speaking of scrolling, we added the Scroll Rect component to the ScrollRect object next. This just required dragging in the ScrollRect object as the viewport and the Content object as the content to start this working. This did have a slight issue for me where it seemed that I could only scroll if I clicked/held on a button in the content area. If I clicked/held on an empty space within the content area, I could not scroll the menu, so that would have to be fixed.

We created a simple LevelSelector script and placed it in an empty gameobject. This script provides the method for the onclick event for each of the buttons to reference to take the player to the corresponding level. To help test this, we just duplicated our initial level and changed the number of lives to create a level 2.

Finally, we added a scrollbar component to the Levels object to go along with the scrolling of our scroll rect. It just gives another, clearer option for scrolling, especially for someone using a computer.

SUMMARY

  • Check pivot/anchor point locations for scaling UI elements, especially those using a component like Content Size Fitter.
  • Implementing scrolling into UI elements in Unity is straightforward with ScrollRect and Scrollbar components.