Showing posts with label Unity. Show all posts
Showing posts with label Unity. Show all posts

Sunday, 12 July 2015

GameObject or C# Objects?

When working in Unity, I found it useful to do as much as possible within C# scripts.
Having more experience with similar languages, I was more comfortable with that style.
Rather than worry about creating GameObjects and attaching MonoBehaviours to them, I could create simple objects and do as I wished.

Difficulties arose when trying to debug features. Objects created internally through scripts are not visible in Unity's editor. The only way to get information about them was to print to the debug log, which quickly became tedious.

For this reason, I converted several of my C# objects into MonoBehaviours. When the script would create an object of that type, it would instead create a GameObject and attach a MonoBehaviour to it.
Nothing is done with GameObjects, the script retains a reference to the MonoBehaviour. This allows the script to function normally, and for the object to appear in the editor at run-time.

The downside is that MonoBehaviours cannot have constructors. A MonoBehaviour is created by attaching it to a GameObject instead of being created through a constructor. This means that they are not completely initialized upon creation, and will need a separate initialization method.

Using GameObjects also allows the properties of the MonoBehaviour objects to be exposed in the editor. These can be changed at run-time. A practice I've adopted is to rename any property that I want to be visible in the editor with an "editor_" prefix to show that while it is public, other classes should not access it.

Thursday, 7 May 2015

Intersecting Paths on a Hexagon Grid

In an earlier post, I discussed a co-ordinate system for a hexagon grid.

In this post, I will discuss a problem I worked on recently concerning such a system.

Essence of Glory is a game of formations and positioning. One way this manifests is in the system for close Assaults. These do not necessarily refer to hand to hand combat (although boarding parties are one possibility), but "close" engagements. Sort of how two airplanes coming within a mile of each other is a near miss.

The system we are using for Assault stipulates that an Assault occurs between two opposing wings of ships if they pass through each other during the Movement phase.

The question is, how do we determine when ships pass through each other?

There are two options, really. Either track ships as they move and report any collisions, or do some calculations based on their paths.

The first option isn't really useful, because we want Assaults to occur any time ships' paths cross.
We don't necessarily care if they are in the same space at the same time.

So we are better off looking at the movement paths as a whole. Remember the grid system?


A vector can describe either a position, or a change in position.
[2,0] may be the position to spaces to the right on the horizontal axis, or it may represent a movement of two units in that direction.

It would be possible to describe the movement of a unit with one vector, but it wouldn't be as useful. For example, a ship starting at [0,0] might move to [-2, 1], but did it pass through [-1, 0] or [-1,1]?
If we described that movement using only the vector [-2, 1], we would have no way of knowing.

Instead, we can store the movement of a ship with a series of unit vectors. Unit vectors are those with length one. Note that in hex co-ordinates, [-1, 1] is a unit vector, but [1,1] is not.

So, a ship starting at [0,0] and moving to [-2, 1] could have its movement described with the sequence of unit vectors {[-1,0], [-1, 1]} or with {[-1,1], [-1,0]}.

To find all the hexes through which a ship passed during the course of its movement, just add each of the unit vectors in its path to its starting position in turn.

Then, to find which ships will assault each other, find all hexes that all ships passed through during the movement phase, and then find those that are shared between opposing ships.

Thursday, 19 March 2015

Ease of Unity

This is my first project at really using Unity, and in the game development community there is a saying that you don't need to know coding that well to make a game using it. It seems that is actually the case... to a certain extent.

It seems that although you COULD technically use only the built in libraries for Unity, and how it handles cameras, assets, and rendering; to make a game using 100% code, it is entirely possible to use just assets, and use the settings provided with each asset with a few scripts here and there to glue things together.

One plus side of Unity is that the community is really big, and if you need a certain asset, what you're looking for will probably be in the store. Even if you need a certain functionality with it, you could potentially find that also. Making changes in the game to be event driven is also taken care of nicely in the engine itself, and is able to be set through simple dropdown boxes when you add it to a certain scene. Even if you do need to write code for certain events, the libraries handle a large component of the groundwork already.

So for anyone with cool ideas for games, or is even an aspiring game developer. Unity is a great engine to experiment with, even if you're not from a coding background.

Wednesday, 18 March 2015

Unity 5 + Progress + Planning

Unity 5 was released recently, and so we decided to go ahead and move the project to that.
It seems to contain many updates to the high-end graphical and animation side of things, which this project may not touch, but there's not much reason to continue developing for an older version.

The conversion process was straightforward. All that was necessary was to open the project in the new editor, and the upgrade was automatic. It did take a surprisingly long time, and seemed to hang, but others have reported encountering this problem.

Anyways, I've been working on the rules side of the game. Essence of Glory is a table-top game for two players. It features dice, orders tokens, models, and a grid to play on. But there is also that portion of the game that exists only within the player's heads. They need to keep track of what turn it is, who has to play, and what they're doing at this juncture. And it is that non-physical side of the game that I have been tackling recently.

The game engine needs to know what turn it is, what phase it is, and what's happening, and it needs to present this information to the player in a convenient way. It also needs to account for the different ways of playing the game. Players may face off against the AI, or they may play with their friends over the internet, or around the table. I've been spending time thinking about how to handle all this, and started putting those ideas into action.

Since there aren't any illustrative screenshots to show off yet, maybe it might be fun to try to figure out my notes? Find them below the fold.

Wednesday, 4 March 2015

Events in Unity

Events are a useful tool for communicating between game objects, especially when you don't know which objects will be communicating with each other.

Say we have a Button. When the user clicks on the button, we want another Thing to do task X. Maybe the code would look like this:

void OnClick(){
     Thing.doX();
}

All very well. But what if we decide that actually, there's going to be a whole bunch of different Things in this scene, and they should all do X? What if those Things need more information to do X? What if the set of Things that should perform the task X is constantly changing?

If any of that is true, that means we'll have to change our code for the Button in order to change what and how Things do X. And all the while the button hasn't changed. It's still there, doing its thing, getting clicked by the user. So why should we have to change the Button, if the responsibilities of other objects have changed?

The answer is that we don't, if we use Events.

Events are a really handy construct. They can be thought of as a one-to-many relationship, where one object publishes or broadcasts events and a set of others receive and act on them. Just like a radio broadcaster doesn't know how many people are tuned in at any given time, the Event broadcaster doesn't know what objects are subscribed or even if any of them are.

So our button code would be changed to look something like this:


//This defines a delegate which is a little like a function pointer.
//It defines the kind of function that is needed to handle the event.
//In this case, a void function that takes an object is needed
//to properly handle this event. 
public delegate void ClickEventHandler(Object sender);

//This goes inside the Button class, and defines a delegate member of the ClicEventHandler type for that button.
//This is the event that other classes will listen to.
public event ClickEventHandler clickEvent;

 void OnClick(){
     //Now, when the button is clicked, all of the delegates subscribed to the event will trigger
    clickEvent(this);
}

public class Thing{
    public void handleButtonClick(Object sender){
        doX();
    }
}


Now, we can change the behaviour of the Thing without changing the button at all.

In Essence of Glory, events are used to update parts of the game engine about changes in other parts of it. For example, when a ship is moved in the Model, an event is raised that is received by the View, which then updates where that ship is displayed on the screen. Events can also be used for special effects that trigger under certain conditions.

More information about Events in C# can be found here and in Unity here.

Monday, 23 February 2015

Singleton Pattern for Unity + Update on Battle Engine

This post is a short description of the Singleton design pattern, and a brief update on the state of the Unity project.

Friday, 13 February 2015

More story updates

This week I finally managed to get a hold of the full version of the VN toolkit, probably going to have some fun with it with some side projects. This version has a few more quality of life features, like not having to type in all the variables you're going to use, and instead select them from a dropdown box.

Anyways, this week our characters take a day away from studying tactics and visit the bar... although to learn from experienced veterans. Don't mind the scene in the back, placeholders are a nice thing to have.

Thursday, 5 February 2015

Unity and toolkit troubles, and more knowledge

Unity is great for many things, but this past week there has been some shortcomings with the platform. 
So I started looking into the code for the VN toolkit a bit to see what we can throw in there, and what we can gut out. For the most part, it's a fairly well structured piece of software with areas where we can eventually start molding to form something suitable for this game. This is fine and dandy, although there's not much documentation in the code, it's easy enough to navigate and modify. Edit some parts of the GUI here and there, change some colour schemes, etc. However, it seems that the built in code editor for unity has other plans.
What? how can there be so many issues with areas that weren't even touched? Well after a while looking around, it seems that the built in editor MonoDevelop doesn't support any version of C# higher than 3.5, which is what the toolkit is built on. 
It seems that the original developers were also not pleased with Unity's editor, and probably decided to use a third party editor. Doesn't seem too unreasonable, but Unity is an engine where anybody, even someone without much coding experience can make a game; so it's not too unreasonable to say their platform for writing code is outdated.
Anyways, looks like the toolkit will need to be edited some other time. For now though, here is a new scene with our friends once again studying, in a bigger library this time. Perhaps they will learn a bit more about actual tactics, and *ahem* mechanics pertaining to what they're doing.
Here is Tarah dispensing some knowledge, while we look to make some more progress next week.

Wednesday, 28 January 2015

Unity Project Check-In

Not much to report this week. A few minor changes were made to the project. The two most notable being a rudimentary camera control suitable for testing, and some placeholder art for ships. Hopefully, by next week we'll be able to have those ships moving around according to the game's rules.

Thursday, 22 January 2015

More on visual novel tookit

This week I've been looking into more features of the VN toolkit. Mainly basic functionality to get a working modern visual novel.

Using existing crop-outs of art we had, I made a little scenario with our characters studying with each other.

I managed to get the basics of a VN covered this week. Dialogue, choices, characters popping in and out, voices, and a BGM. They were easy enough to put in the game, but a problem that might occur later is organizing all the different scenes and dialogues.

What we may want in the future is to have a flowchart like narrative, with decisions or actions in the game splitting into different paths, or multiple paths to have a common convergence point. Throughout the next week I'll probably be taking a look at how to make a neat way to organize all the different scenes we have. Currently the tookit has a tree-like structure for organizing splits in decisions; what I may code in the future is to add flags to certain points to make figuring out what events occur at what times easier.

We might not have a story as convoluted as this one, but even if it's simpler it'd still be nice something to make it easier to manage.

Wednesday, 21 January 2015

Visual Novel Toolkit

This week I've been working with a module for Unity referred to me by Tyler, called Visual Novel Toolkit.
It's a pretty neat little program, that allows you to make VN styled games at the drop of a hat, although of course, for the more complicated parts of a game such as math and combat, you'd need to do that by yourself.
Working with the module in Unity
The actual interface inside of the module's pretty easy to work with, and there's a few tutorial videos online made by the creators of the module, as well as community content, so it's not too hard to learn.
The view inside of Visual Novel Toolkit
The UI inside of the game needs to be worked with, and the sprites are a little clipped, but this is only something to do preliminary testing on, so that's not too bad. 

I'm using sprites and dialogue given to me from the other project, shout out to whoever made them, because the art  and dialogue for The Dreaming Man aren't entirely up to snuff yet, so this allows me to do some practice before they're ready.


Anyways, it's a decent little program, and this weeks project. Next week I'll look into some of the code-side stuff, and investigate what more can be done with Unity.

Model-View-Controller Design with Unity

This article will discuss the overall design architecture that will be used to create Essence of Glory in Unity. Since Essence of Glory is a tabletop game, we can compare it to other such games and think about how they would be designed in Unity.


Let's say you were going to implement a game like Jenga in Unity. How would you do it? Well, objects in Unity are modeled with GameObjects. These follow the Composite design pattern; every GameObject is a collection of GameObjects, and/or MonoBehaviors. If you are familiar with Entity-Component systems, you can think of GameObjects as Entitites and MonoBehaviors as Components.

Coming back to Jenga, you would probably have each block as a GameObject and attach the right MonoBehaviours to it to give it meshes, textures, shaders, physics collision and so on. Furthermore, you'd want to know when a block hit the ground. So you could add script, in the form of a MonoBehaviour, to each block that would trigger when it collides with the ground.

But Essence of Glory is not a game like Jenga. A better comparison might be chess.


What happens when, in a game of chess, a piece falls over? Unlike in Jenga, this occurrence has no bearing on the rules. You would simply pick up the piece and replace it.

But hold on, I just posted a picture of a game of chess, but that isn't what chess looks like.

This is a game of chess:
And so is this:
 Or even this:
"1. e4 c5 2. Nf3 d6 3. Bb5+ Bd7 4. Bxd7+ Qxd7 5. c4 Nc6 6. Nc3 Nf6 7. 0-0 g6 8. d4 cxd4 9. Nxd4 Bg7 10. Nde2 Qe6!? 11. Nd5 Qxe4 12. Nc7+ Kd7 13. Nxa8 Qxc4 14. Nb6+ axb6 15. Nc3 Ra8 16. a4 Ne4 17. Nxe4 Qxe4 18. Qb3 f5 19. Bg5 Qb4 20. Qf7 Be5 21. h3 Rxa4 22. Rxa4 Qxa4 23. Qxh7 Bxb2 24. Qxg6 Qe4 25. Qf7 Bd4 26. Qb3 f4 27. Qf7 Be5 28. h4 b5 29. h5 Qc4 30. Qf5+ Qe6 31. Qxe6+ Kxe6  32. g3 fxg3 33. fxg3 b4  34. Bf4 Bd4+ 35. Kh1! b3 36. g4 Kd5 37. g5 e6 38. h6 Ne7 39. Rd1 e5 40. Be3 Kc4 41. Bxd4 exd4 42. Kg2 b2 43. Kf3 Kc3 44. h7 Ng6 45. Ke4 Kc2 46. Rh1 d3  47. Kf5 b1=Q 48. Rxb1 Kxb1 49. Kxg6 d2 50. h8=Q d1=Q 51. Qh7 b5?! 52. Kf6+ Kb2 53. Qh2+ Ka1 54. Qf4 b4? 55. Qxb4 Qf3+ 56. Kg7 d5 57. Qd4+ Kb1 58. g6 Qe4 59. Qg1+ Kb2 60. Qf2+ Kc1 61. Kf6 d4 62. g7 1–0"
In fact, some people play chess blind-folded, or via mail.

What does this tell us? That the way a game of chess is visually represented is distinct from what constitutes the actual game. The game of chess is an abstract rules construct which is merely represented in some form or another to players. This is in contrast to the earlier example of Jenga, in which the pieces that players manipulate are also significant to the rules.

Furthermore, this indicates that chess (and Essence of Glory) is a good fit for the Model-View-Controller (MVC) software architecture pattern. In MVC, the software is divided into three areas of concern:

  1. The Model - simulates the state of the system. For a game like ours, this would involve tracking the position of pieces, resolving the outcome of moves, rolling dice, and determining if a player has won. This can be implemented with plain-old-C# classes and need not involve Unity at all.
  2. The View - represents the state of the system to the user. Everything that the user sees is part of the View, this includes any buttons or other interface elements. As such, the View will mostly be made up of Unity GameObjects. It is updated based on information received from either the Controller or directly from the Model.
  3. The Controller - updates the Model based on user input. May also be responsible for updating the View based on changes in the Model. Often implements the Observer pattern with UI elements.
This provides separation of concerns. Each part can be updated relatively independently of the others. It doesn't matter if a ship being moved is being represented by temporary place-holder art or a finalized and polished high-def model, the state contained in the Model and the command issued by the Controller will be the same.

This also helps for input. Only the Controller is concerned about where input comes from. Whether a command is received from mouse, keyboard, touch, over the network or generated by the A.I., the change in the Model will be the same and so will its effect on the View.

Model-View-Controller does not seem to be common in Unity, this article was the only real source I found after a quick search. It's an informative read, though it references the no-longer-used NGUI library. The example project linked in the above article is a very good example of how MVC can work.

Thursday, 15 January 2015

Visual novels and tabletop games

No game's complete without some sort of narrative, and for Essence of Glory we decided to take an interesting route to deliver that.

Since we're using the Unity engine to develop the project, we have the benefit of being able to use many tools for development. What we're doing at the moment is messing around with the Visual novel toolkit by Sol-tribe.

Using a visual novel interface will be very easy to have our characters interact with each other. The end goal is to modify the interface provided by our friends at Sol-tribe, so that eventually we will have a tool suitable for our game that is visually appealing and easy to maintain.

I'm just playing around with this at the moment, trying to see how many things are editable in the free version (since we might not want to fully commit to this yet). This tool seems to be able to recreate most modern visual novels, but there are some options we don't need for Essence of Glory; such as the save and load button provided with this toolkit.

All of the other options are very nice, including special effect and scripting options. It seems pretty picky about the textures that's used for characters, it must be cropped and specifically sized for the toolkit.

There doesn't seem to be more interesting features about the free version of the toolkit, it seems this week is a good time to lurk on forums to see features for the full version. But other than removing features, this seems like a good way to accomplish what we eventually want to deliver the story.

Wednesday, 14 January 2015

First Steps with Unity - Hexagons

This is the first in a series of posts about the development of the electronic incarnation of Essence of Glory.

We are just beginning work on this game, and we're using the Unity engine. We chose Unity for its availability, ease-of-use, and wide variety of deployment platforms.

This week's post will mostly be about hexagons.

What's so great about hexagons? Well, as the logo at the top of the page may suggest they are a pretty significant part of a lot of tabletop and wargames. They provide a more accurate and natural way of moving game pieces around than a square grid. However, they are a little trickier to draw than a grid of squares.

Unity measures the position of game entities with a three-dimensional vector. Our game board will mostly be flat, so we can ignore the height dimension for now. That leaves us to consider position on a two-dimensional plane, like a cartesian co-ordinate plane.

Remember math class?
As you might imagine, it's rather easy to draw squares on such a grid. The grid is already based on squares! Just draw a square at every position (x,y) and there you have it.

But how do you draw a grid of hexagons?






Well, it might help to imagine them as a grid of squares where every second column (or row, depending on your perspective) is offset a little.

Notice how each tile has 6 neighbours touching it.
What we want is a way of numbering hexagon tiles so that we can easily determine where to draw them.

Seeing as a hexagon grid is analogous to a square grid with offset rows, it may be tempting to try to number it like a square grid. Depending on how this is done, the results may not be helpful.

If you try to keep the axes at 90 degrees to each other, you may end up with something like this.

x-axis in red, y-axis in blue
As you can see, the y-axis is really ugly. Converting from these co-ordinates to Unity's world co-ordinates is not too difficult, but the reverse is a bit of a pain. What we want is axes like this:

axes as above
Much better. Converting from these hex co-ordinates to world co-ordinates and back will use the same formula at every position.

Now, to implement this in Unity, I followed the approach described here. As a programmer, I am much more comfortable with creating and modifying game objects from scripts rather than using Unity's drag and drop features. I won't go into detail here, but I first created a HexManager script, which spawns new game objects and attaches the HexModel script to them. Ideally, I would like to create a prefab out of the hexagon objects, as the game board will likely be the same in every level, but this is a useful construct and will do for now.

So, we have a way of drawing hexagons where we want them, and a logical way of describing where that is. Next week, we will be working on adding some interactivity to this video game, getting something to happen when you click on these hexagons.

In addition the article linked above, I also found 'Hexagon grid: Generating the Grid' and 'Hexagon grids: coordinate systems and distance calculations' to be of the greatest help.