Html/Javascript widget

Monday 14 July 2014

Common Data Structures in C#

As used in Unity 3d, that is! Today we are going to review some basic data structures and the right syntax within which they are arranged in C#.

Queue

A queue in computer programming stands for a sort of array of the first in first out kind. This is comprised of a rank of items being entered into a set and then the first item inserted is also the first one to be taken out. The items assigned to the queue can be strings (ASCII characters that don`t take part in any kind of specific calculation), integers, float (a special kind of integer that supports an impressive amount of decimal digits) etc. The basic syntax is as follows:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class QueueManager : MonoBehaviour
//this is important. When dealing with C sharp within a common Unity Engine domain,
//it is necessary to declare a class (a class basically carries your main program)as public
//inheriting MonoBehaviour. It is crucial to the successful compilation of your program for it to //inherit MonoBehaviour because that`s what will interpret your code as more fitting in an
// in-game environment than that of a common application. Therefore, you shall make your mission
// to have your public class inherit MonoBehaviour. Also note that the public class name
// will always be named after the script created in the game object screen
{

public Queue <string> panhandle = new  Queue <string> ();
/* Pay heed to the way the above statement looks like. We start off by declaring a variable of the queue kind. Except that you don`t declare it the usual way. Before moving on, notice that the queue is to accessed publicly anywhere in your project. That`s why it was made necessary to state that it is of the public kind. AFter making it clear that you`re declaring a public queue variable, you need to specify what kind of data will be inserted into this queue. This is specified between < >. In the example above, it will take in string variables.

Ok, so far you know that every queue that for the life of you you have to declare should follow the following pattern:
1- access level (public, private etc)
2- the key word `Queue`
3- what kind of variables will come into this (<string>, <int>, <float> etc)

This should be enough for you to witness the mighty power of a newly declared queue, right?
Again, do recall that life is full to the brimming with glaring exception that are only there to mar the beautiful thought process that you have allowed to shape up in your bonse. After making sure that your queue is of the public kind and that it will be receiving strings, you should prepare it for future use. That`s right, you should set this to be started in the program. This is accomplished with `new`. You have basically created  a queue data structure and now the compiler needs to understand that it is a new queue. Worse yet, it needs to be told that it is a new Queue that will store strings. Hence the  <string> bit.

The last tidbit are the round brackets enclosing themselves. () are a common staple of object oriented programming and apparently necessary for the public declaration of a queue to be complete.

From what you can glean by reading the above it is possible to conclude that:
1- a queue should first be determined whether it will be of the public kind
2- it is always necessary to specify the type of variables that are going to be administered to the declared public queue
3- don`t forget to give your queue a name. Here it is going to be called `panhandle`.
4- It will receive (hence the equal symbol) a new queue. You can think of the process as a soulless body lying prostate on the ground. Provided that it remains soulless, it will be of no use. As soon as it welcomes a warm and amiable soul, the body will be ready to serve whomever its master is.
5- it will receive a new attribute, which calls for the specification that it is a queue for <string>
6- () is a common staple of queues, stacks and lists.

public Queue <string> panhandle = new Queue <string> ()

After this thorough understanding behind the rationale for creating a queue, you can type this up without giving it much thought!

void Start()
{
// here is everything that is going to take place at the game`s start
panhandle.Enqueue("Damnd");
//there you have it. You`ve included your first item in the queue. A string named "Damnd".
//notice the imperative command `enqueue` to make this happen
panhandle.Enqueue("Abigail");
panhandle. Enqueue("Eddi E.");
panhandle.Enqueue("Won Won");

//we`ve added a total of 4 <string> items to our queue.
 //it might be interesting to create a loop to check the <string> variables in the queue.

foreach  (string number in panhandle)
{
Debug.Log (number);
//number was a quick string created only to be a reference to access the queue
}

//one might want to display their neat queue. Debug.Log is the best option for a quick Print.Out //command

Debug.Log

//If we need to take an item out of the queue, we simply use Dequeue ().No need to specify which //item will be withdrawn becauseit will always be the first one in.
}

void Update ()
{
}

}}
.....................

Let`s create a complete application to highlight the use of queues:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class queuemanager : MonoBehaviour {

public Queue <string> elements = new Queue <string> ();

void Start ()
elements.Enqueue ("Kung Lao");
elements.Enqueue ("Tony Marquez");
elements.Enqueue ("Goro");

foreach (string number in elements)
{
Debug.Log (number);
}

void Update ()
{
if (Input.GetKeyUp (KeyCode.Space))
{
elements.Dequeue ();
Debug.Log ("___");

foreach (string number in elements)
{
Debug.Log (number);
}}}

.............................

List

A list is, at its most basic concept, a queue, bar the except that it doesn`t necessarily follow the first in last in pattern.

Example:

using EngineUnity;
using System.Collections;
using System.Collections.Generic;

public class listmanager : MonoBehaviour {

public list <int> listofintegers = new list <int> ();

void Start ()
{
listofintegers.Add (1);
listofintegers.Add (2) ;
listofintegers.Add(3);
listofintegers.Removeat (0);
listofintegers.Remove(2);

//(0) simply means that we are going to remove the item at the top of the list.
//Remove(2) will remove 2 items at once off the list

for (int i=0; i<listofintegers.Count;i++)
{
Debug.Log (listofintegers[i]):
}
}
void Update ()
{
}
}

................................

Stack

Another kind of queue, except that it is of the first in last out kind.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class stackmanager : MonoBehaviour
{

public stack <string> staxrcool = new stack <string> ();

void Start ()
{

staxrcool.Push ("Akuma");
staxrcool.Push ("Gouki");
staxrcool.Push ("Shin Gouki");

// you may be intelligent enough to have noticed that to add items to stack is to push them in.

staxrcool.Pop ();
//Pop removes one item from the stack. Again, the last one in is always the first to leave.

}

void Update ()
{
}

}


No comments:

Post a Comment