Posts Tagged ‘Programming’
TransformUtilities
Posted by Daniel Rodríguez in Game development, Programming on February 6th, 2010
Hello. I’ve been working on a set of four tools to aid in the Unity3d development. After using Unity for a while I encountered the classic problem of needing to copy the position of an object over and over. This led me to create the “Copy” tool. I’ve found a similar script on the Unify community, but I wanted to extend the tool. It is now possible to copy position, rotation and scale of any axis you want. Also, the values apply to all objects selected in the scene.
After that, and inspired by the tools I found on the Gamebryo Lightspeed editor, I created the Randomize and Add Noise tools. Those tools work almost the same, the randomize the position, rotation of scale of any object selected (or all objects selected). The difference is that while Randomize sets the absolute values for position, rotation or scale, Add noise adds a delta to the current value of the transform. This is useful for example to randomize the Y rotation of all the trees in your level, or to make a bunch of boxes look more natural by not being perfectly aligned.
Finally, after been learning Autodesk 3ds Max, I created the Align tool inspired by a tool in that software. It basically takes a source transform, just like the Copy tool, then uses it’s collider and the collider of the object (or objects) selected to align it. You can align for example, the minimum point of your selection with the maximum point of your source (what you are aligning to) to place an object over another. It let’s you align using the minimum point, maximum point, the center of the object or the pivot point on both the selection and the source. You can align on all 3 axis.
To use these tool, all you need to do is download the script file, and add it into a folder named Editor in your project’s Asset folder. Once compiled by Unity you find the new functionality in Window -> TransformUtilities, or simply press Ctrl+t (Cmd+t for Mac users)
Developed by Daniel Rodríguez (Seth Illgard) in January 2010
Hope you find them useful, and please let me know how can I improve theses tools.
Coroutines in Unity3d (C# version)
Posted by Daniel Rodríguez in Programming on January 23rd, 2010
Coroutines in C# work the same way that they do in Javascript (UnityScript), the only difference is that they require more typing (they have a slightly more complicated syntax). You should see the blog post on Javascript coroutines first.
Here, I present the differences:
Coroutines must have the IEnumerator return type:
IEnumerator MyCoroutine()
{
//This is a coroutine
}
To invoke a coroutine you must do it using the StartCoroutine method:
public class MyScript : MonoBehaviour
{
void Start()
{
StartCoroutine(MyCoroutine());
}
IEnumerator MyCoroutine()
{
//This is a coroutine
}
}
The yield statement becomes yield return in C# :
IEnumerator MyCoroutine()
{
DoSomething();
yield return 0; //Wait one frame, the 0 here is only because we need to return an IEnumerable
DoSomethingElse();
}
Remember that we need to return an IEnumerable, so the Javascript yield; becomes yield return 0; in C#
Also, since C# requires you to use the new operator to create objects, if you want to use WaitForSeconds you have to use it like this:
IEnumerator MyCoroutine()
{
DoSomething();
yield return new WaitForSeconds(2.0f); //Wait 2 seconds
DoSomethingElse();
}
Happy coroutining
Coroutines in Unity3d (Javascript version)
Posted by Daniel Rodríguez in Programming on January 22nd, 2010
I’ve been working with Unity3d for some time now and I couldn’t really make good use of the coroutines because I was unable to truly understand them. After some time playing with them and making some experiments I realized their true power. I couldn’t believe what I was missing! So, a couple of fellow game devs asked me to explain the concept to them. I decided that a blog post was the perfect way to do it and, at the same time, share this with everyone.
Coroutines: Special type of functions(in the programming sense) that allows to stop it’s own execution until certain condition is met.
A coroutine looks like this (on javascript, the C# version is here):
function MyCoroutine()
{
DoSomething():
yield; //Mystery here
DoSomethingElse();
}
When you invoke this function (start the coroutine) it will behave just like any other normal function you have ever seen, until it reaches the yield instruction. The yield instruction explained: The yield instruction works like a return statement in the sense that it stops the execution of the function and returns control to the code that invoked that function. The main difference is that the yield instruction lets you delay the execution of the code that is after it (in the last example, the DoSomethingElse() statement).
function MyCoroutine()
{
DoSomething(): //Do this immediately
yield; //Return control to the caller
DoSomethingElse(); //This will be executed one frame later
}
void Start()
{
MyCoroutine();
}
What happens if you have more code after the MyCoroutine call? Let’s see an example with some prints:
function MyCoroutine()
{
print("This is printed second");
yield; //Return control to the Start function
print("This is printed one fourth, exactly one frame after the third");
}
void Start()
{
print("This is printed first");
MyCoroutine();
print("This is printed third");
}
You can control when do the code after the yield instruction will be executed. It depends on the parameter of the yield instruction, according to the following table
Nothing: It will wait one frame
Another coroutine invocation: It will wait until the invoked coroutine finishes execution
A WaitForSeconds object: It will wait certain amount of time
Confused? Here are the examples:
function MyCoroutine()
{
DoSomething(): //Do this immediately
yield WaitForSeconds(2); //Return control to the caller
DoSomethingElse(); //This will be executed 2 seconds after
}
void Start()
{
MyCoroutine();
}
function MyCoroutine()
{
DoSomething(): //Do this immediately
yield MyOtherCoroutine(); //Go and execute MyOtherCoroutine!
DoSomethingElse(); //This will be executed after MyOtherCoroutine finished execution
}
function MyOtherCoroutine()
{
DoStuff(): //Do this immediately
yield WaitForSeconds(2); //Return control to the caller (in this case the Start function)
DoMoreStuff(); //This will be executed 2 seconds after
//MyOtherCoroutine finishes execution here
}
void Start()
{
MyCoroutine();
}
As you can see, coroutines are very powerful and easy to use once you understand how they work. I will post some usage examples and the C# version of the scripts on this post soon.
Mixins in game development
Posted by Daniel Rodríguez in Game development on October 2nd, 2009
Programmers tend to think in the Object Oriented Programming paradigm all the time. We create complex class hiercharies that describe every single entity on the game. Thats what we learn at school, we are programmed (deeply into our firmware) to do that, but there is a whole world of different pradigms and approaches we can use, and the scripting languages are bringing the dynamic style into the game.
Functional programming can be great for game scripting, Lua has beeen a very popular game scripting language for a very long time, and a bunch of game engines uses it (including CryEngine 2.0 and Gamebryo LightSpeed). The ability to attach properties and functions into an object at runtime, for example, is incredibly useful to create high level game logic and even artificial intelligence.
We can talk about dynamic languajes and their benefts here, but I want t focus on Mixins as the modern approach to game entity description:
Mixins are chunks of functionability that can be applied to game objects. You can think of them as little pieces (building blocks) that you use to create a complex game entity. The most basic mixin every game entity is the entity’s transform which defines its position, rotation and scale. Each game engine implements entity’s transform in a different way, but the concept is the same.
Mixin complexity can vary from a simple mixin that lets you name an object (holds a string identifier), to very complex mixins like the one that provides characters the ability to use pathfinding to navigate the scene.
Each engine handles mixin agreggation in it’s own way, some engines even let you use multiple inheritance with mixins. Please note the difference between mixin inheritance and mixin agreggation:
- Inheritance:
- Mixins can be inherited
- You can have mixin class hierarchies just the same way you can with normal classes
- Aggregation:
- Game objects agreggate a number of mixins to aquire their functionability
- Mixins are agreggated (added to game objects) to create entity model descriptions
For example: A mixin called BoomerangTarget provides the ability to be the target of the player’s boomerang. BoomerangTarget is a child of another mixin called Target which provides the generic ability of being a target of a player’s weapon. At the same time, an Enemy agreggates the following mixins: Actor, BoomerangTarget, SwordTarget. Actor lets the enemy play animations, BoomerangTarget is the mixin desvribed above and SwordTarget provides the ability to be hit by a sword. SwordTarget mixin is also a child of Target, thus, both BoomerangTarget and SwordTarget share the basic functionability defined in the Target mixin.
A lot of game engines are using Mixins these days. Mixins have become the standard way to represent game object functionability.
Seth out.
