Change font weight by code in Swift

let currentFont = label.font
let fontName = currentFont.fontName.componentsSeparatedByString("-").first
let newFont = UIFont(name: "\(fontName!)-Light", size: currentFont.pointSize)
label.font = newFont

This snippet changes the current font of a label to a Light version of it ( in case it exists ). Font names are ( in this case, it was ‘.HelveticaNeue-Regular’ ) appended with their weight. So I get the font name, split it by ‘-‘, and take the first part of the split, ending with ‘.HelveticaNeue’. Now I just create a new font with the light appended to it, with the same size.

Lastly, I switch the font, and there you go.

SuperStems is out for iOS

SuperStems is finally released on the AppStore for iOS! After 14 long days waiting for approval, it finally passed and it up for grabs for free! There are already over 400 downloads and counting, and players are rocking the leaderboard!

I’ve setup a new website for SkyBelow, my new gaming studio.

Edit: I’ve been updating SuperStems since Unity5 was released, and all I can say is that it runs smoother and better looking with the new IL2CPP – converts C# to Cpp to increase performance -, which is awesome! So far I’m testing it on my device, but you should get the update in a couple of weeks. Just enough time to send the new build and get it approved again by Apple.

How to create a recursive call with Unity’s Coroutines

During the development of Super Stems I’ve had to deal with chain reaction. I started a battle with one tile, and if they win, the captured tile would start a battle, and this would happen recursively. For some reason, I have this battle method call on a different thread, in Unity’s terms, a Coroutine.

Now comes the question. How do I handle recursion with Coroutines? After looking around the doc and experimenting, I found a solution.

Here’s a sample code on how to make it work.

public void StartBattle( )
{
	StartCoroutine(BattleRecursive(0));
}

public IEnumerator BattleRecursive( int depth )
{
	// Start Coroutine"
	
	yield return new WaitForSeconds(2f);

	if( depth == 0 )
		yield return StartCoroutine( BattleRecursive(depth+1) );

	if( depth == 1 )
		yield return StartCoroutine( BattleRecursive(depth+1) );
	
	Debug.Log( "MyCoroutine is now finished at depth " + depth );
}

After calling the entry point to your recursive method StartBattle, inside, you have yield return a new Coroutine call to the recursive method. This way, the execution order will be correct. Try it out and see for yourself.

Output should be.

MyCoroutine is now finished at depth 2
MyCoroutine is now finished at depth 1
MyCoroutine is now finished at depth 0

Enjoy

Super Stems Post Mortem

work-environment-superstems

B4L4pozCYAAHAwj Super Stems, originally called Stems, started on a gamejam weekend, Ludum Dare 31. The theme was “Entire Game on One Screen”, which many people complained about, me included, since a lot of people were expecting the snowman theme to be chosen. Aside from that, once the theme was announced, I started to make Stems.

So what is Super Stems? Super Stems is a board domination strategy game. You have tiles and board slots. Each tile has three sides, a number attached, and adjacent sides on the board will battle. Upon battle, the higher number will win, and the losing tile will be captured. This is a simple concept that I stripped down from another game I’m planning, which has this basic gameplay with more gameplay mechanics, inventory and multiplayer in it. B4M3HkVCAAAjR2a But since I had only 48 hours to make it happen, I stripped it down to this. A few hours in, I had the tiles and placeholder models and ready to be used in Unity.

I don’t usually take much time in planning during gamejams, I usually go all in with a minimum planning and knowing what I must do and sort and solve problems as they arrive. But since Indies vs Pew Die Pie gamejam went really bad because from the lack of planning, this time around, I took some time to plan ahead and think of what I wanted to make.

B4NwB0yCYAA60AN

So with my plan written down and my idea right fixed in my head, I started developing the game until I had basic gameplay done. If you asked me by then, how much more time would I take me to finish a minimum viable product, I would’ve said a week, maybe less, but oh boy was I wrong. First things first, the gamejam atitude of coding this and that without taking much care, because time is of the essence, is bad. I don’t use magic numbers, nor hardcode anything. I usually take care in writing beautiful and maintainable code. So for that end, I was good. On the other hand, I manually placed the grid and linked neighbors, one by one. Since the original grid had only 9 pieces, it was fast and did the job. Finding after a few days that some of those links were broken/switched, was bad tho.. Mistake #1.

B4S5hSqCYAEo2QU

But nonetheless, everything was in place, I had the grid, the basic Turn-by-Turn gameplay, basic animation, tile capturing, teams, score. I was just missing an opponent.. better yet.. an Intelligent Opponent. Making the Artificial Intelligence was hard. It was hard because of mistake n# 1, not having a generic grid, in which I could make the calculations required to make it easier for me. Having a properly made grid system, I could have made methods to make my life so much easier, but nooo.. I might have written the battle, capture, AI play code at least 5 times! And by this I mean, really starting from scratch. Taking pen and paper, putting it all in a new perspective. And every time I did it, something good came out of it. Every time it god better. Obviously by now, the 48h period had long gone. I was more like two weeks after the deadline, mainly because I was working on it part time, after work, 3 or 4 hours a day, and you can only do so much.

B7bShESIEAEFEi6

But still after those AI changes, it wasn’t working properly yet. I wanted to make it smarter. After another brainstorming session with pen and paper, I finally tweaked the algorithm to make it the way I wanted to. You can now have decent battles with it. It will lose, but also win. So far, from online gameplay and local testing, my analytics say that the AI wins above 60% of the times, so I’m ok with that. During that time, while I was trying to get the AI right, I kept changing the UI, textures and models. The images speak for themselves.

B9Hq3-fIgAA0Kj-

Right now the game is ready to be published. Its maybe not as polished as I would like it to be, but I have to release it, get feedback, and then I’ll see what I’ll do with it. I’m already thinking of other games I want to make. This is my second ‘board’ game. This one has a new gameplay type, which is good, since I always try to make new gameplay on each game. So I’ll be releasing the game in the upcoming weeks, hopefully this will get some players. At least more than my previous one.

Indies vs PewDiePie Gamejam

I’ve entered this week’s 72 hour gamejam hosted by Gamejolt with PewDiePie in which the top 10 games would be played by the man himself and broadcasted to the internets and his endless number of fans. As if making games wasn’t reason enough, having the chance to have your game broadcast to millions of his fans sure was another great one.

tl;dr : Didn’t finish the game. Still had fun.

So I’ve put up some time to make a game this weekend, starting on friday night (16 hours late), I didn’t plan much, booted Unity, Maya and Photoshop, and started to make stuff. I’ve came up with a small idea based on Boson X, and wanted to make a fast paced game. I started to model tubes or portals and a spaceship, and laid the foundation of what could be a level.

Screenshot 2014-11-24 09.31.36

First of, I set it all so the level would fall down on you, making the camera look up, it would give the impression of moving forward. I then changed it to side to side, only changing gravity’s axis. Which, again, I changed by making the player move so I would have realistic jumps, instead of moving the world.

About physics. I didn’t plan much of the stuff I wanted to make, I just did it on a necessity basis. So first I made a custom gravity to pull the ship back in the center after a jump. I had the world rotate instead of the ship due to jump + turning issues that would dislocate the ship to places I didn’t wanted it to go. I’ve later removed the jump and animated it, so I would have more control on the behaviour, which turned out pretty nice actually.

Screenshot 2014-11-24 09.31.52

I had a level, level parts, obstacles and items. I soon ditched the obstacles and put bigger ones with doors, changing the game mechanics. Now you would have to jump above the empty space in the doors. Which worked quite nice. At a certain point you would get enough speed and get to the end level and be warped to another one.

Unfortunately, I didn’t finish the game, due to lack of skill, the theme being highly abstract “fun”, mindset maybe, or just because I went to see Interstellar (which was aweeesooomee!!). Overall I worked around 20h on the game, which is not enough to make it. Mistake number one was to not plan. Eager to make something new and something move on screen, I jumped in, thinking, “mehh, It’ll be a small game, I’ll just make this and that, and it will all work”.. but nooo.. I did had to doodle my thoughts into a piece of paper when I was stuck. Mistake number two, take time to fucking plan your game!! Do not start placing things around without knowing what you want to do. Ok, I got it. Mistake number 3, due to the lack of planning, changing scope delayed progress, making it harder to see the end of it, and ultimately missing the deadline.

Screenshot 2014-11-24 09.32.11

Overall, it was awesome! I’ve not been making these gamejams for some time and it felt great. I’m not sad I didn’t finish it, because I’ve learned awesome stuff while making it. Now I guess, I’ll enter the next competition, letting this game go, since it didn’t struck me as fun, so.. it was a great run :)
According to CompoHub.net, The 96 Jam with rules is next, and there’s always the One game a month, so I’ll see you around ;)

Simple Achievement System in C#

Achievements are becoming more and more usual in games. They provide the player a sense of accomplishment and progress by rewarding them with badges that proves their skill and experience. Some achievements are simple and other require a combination of particular actions to unlock. In this article I show you how to make a simple Achievement System using C# and will demonstrate it using Unity3D, but this should be easy enough for you to port it to whatever language you’re more familiar with for your games.

Continue reading “Simple Achievement System in C#”

Non Blocking C# Task Cancelling

In our previous sample snippet, Cancel a Loop in a Task with CancellationTokens in c# , I try to explain how we can get out of a looping c# task, but a problem may arise from that situation. If we were to wait for any result out of that Task, we would be blocking the calling thread until the task returned, which is not good if we are on the main thread. We would locking our UI and might crash our application.

So I’ve been testing different ways to get out of that loop without causing any trouble, and you can achieve what we want many different ways.

So, to begin with, I think I would correctly assume that it is only necessary to wait for a task to complete if that task will return something. If there is no return value, why would we want to call wait on it? We can just break out of it, correct me if I’m wrong. If we have a return value, then it is necessary to surround the wait call on the task with try/catch to receive its result. But then again, we can avoid the locking here with a continuation task, which will create and start the task after the first one completes, giving us the result from the previous task to work with.

Continue reading “Non Blocking C# Task Cancelling”

Measure Method Time Performance

Quick snippet to let you check the time used by any method you have.

public static void CalculateTime( Action method )
{
    Stopwatch chrono = new Stopwatch();
    chrono.Start();

    method.Invoke();
            
    chrono.Stop();
    Console.WriteLine("Method : " + method.Method.Name + ", Time: " + chrono.ElapsedMilliseconds + " (ms)");
}

This will write to the console the time spent on the method.

DiagnosticStaticClass.CalculateTime( MyExpensiveMethod );

Just remember to use System.Diagnostics.
More info on Stopwatch.

That’s it. Enjoy.