Wednesday 2 November 2016

Zelda Style Life System Unity Tutorial – Part 7 of 10

Zelda Style Life System Unity Tutorial - Part 7 of 10



The end result of the full tutorial.


At the end of Part 7 we will have produced this (See image 1.0).
Image 1.0


Before you start you should know these things:

You should have gone through and completed ‘Part 6’ of this tutorial before going any further. If you find yourself not understanding some of the terminology or code of the tutorial I would also recommend going through the previous tutorial to get up to speed. I will leave a link to it below to go through at your leisure.


http://joseph-easter.blogspot.com/2016/10/zelda-style-life-system-unity-tutorial_26.html

If you follow this tutorial and find I am moving too fast or if you don’t know the things in the list above, I would recommend getting up to speed and then come back to this tutorial when you are ready.


Links to Parts 1 - 10 of this tutorial series:

Part 1

Part 2

Part 3

Part 4

Part 5

Part 6

Part 8

Part 9

Part 10


With this tutorial if you want to convert this into JavaScript by all means do so but it may be easier for you to follow this in C# and stick with the language of the tutorial to make it easier.

PROJECT RESOURCES: At the bottom of the post there is a download link to a zip file. This files includes all files needed to follow Part 7 and the completed project file of Part 7.


In this part we will work on polishing what we have by adding more detail and flexibility to the system.

We will:

  • Learn what a ‘List’ is and how it can be used
  • The advantages and disadvantages of using a ‘List’ over an array in certain situations
  • How to add items to a ‘List


Step 1: – Arrays vs Lists

In this part we will be working on how to have multiple health values per ‘lifeImg’ and how to remove the ‘lifeImg’. An example of this would be if the player takes some damage, the last image would only show half or a quarter of a heart instead of a whole heart.


In most games the last health icon is used for this purpose. You’re probably thinking ‘how would we do that?’. In order to change the image of the number of generated ‘lifeImg’ UI elements we need a way to reference them.


There are a few ways to do this. The first way is to create an array and put them all in there, but this has its limitations. An array is a fixed size; you could resize the array but considering how often the health value will change this is not ideal. When an array is resized it is recreated and you need to reassign the elements again. This is only useful if you know the size beforehand and it will never exceed it.


The other way is by using a
List. A list is basically an array but more flexible, it’s elements are accessed the same, and it’s easier to add and remove elements to it on the go. When adding or removing elements, Unity recreates the array for us behind the scenes but it is not very efficient. It is useful if you manipulate the elements or resize it infrequently, otherwise things get sluggish very quickly. It’s useful when you know the exact size it will ever be and it will change frequently. For this tutorial we will be using Lists.


Step 2: - Adding Health

The first thing we need to do is create a private ‘List’ variable of type ‘Image’ called ‘livesArray’.


Code:


public Image lifeImg;


private List<Image> livesArray = new List<Image>();



The next thing to do is to add our images created during run time to this list. We could do it manually but this would be very impractical. The best thing to do is, in our ‘AddLives’ method when we instantiate a new ‘lifeImg’ game object we add it to the end of our list. To do this we reference the list ‘livesArray.’ then after the accessor ‘.’ we use ‘Add(myItem)’, and between the ‘()’ we add the item we want to add on the end of the list.



Code:


void AddLives()
{
   for (int i = 0; i < n; i++)
   {
      Image tempImg = Instantiate(lifeImg,

                                  transform.position,
                                  transform.rotation) as Image;
      tempImg.transform.parent = transform;
      livesArray.Add(tempImg);
   }

   currentHealth += n;
}

Now when we run our code, this happens (image below, figure 1.0).
Figure 1.0 ‘LivesArray’ is set to public for this image just to see the result.

Great, we have the simple bit set up. This ‘
List’ will adjust its size when we change the health value during runtime and we can reference any element we need when we need to.

Great, part seven is finished.


We have learnt:

What a ‘List’ is and the advantages to using one over an array
How to declare a ‘
List’ variable
How to dynamically add items to a ‘
List
How easy it is to add items to a ‘
List
How a list automatically changes it’s length


In Part 8 we will learn how to set the images of the elements in our sprite array to show the players health decreasing. Click here for Part 8.


If you liked this tutorial leave a comment below telling me why you like it and share it with a friend who will find it useful. If you didn’t like it, please leave a comment below saying why.

If you would would like to see more of these tutorials, please leave a comment below. And if you want more of this particular tutorial say what you want to see more of in the comments.



Download resources and project files.

No comments:

Post a Comment