Tuesday, July 6, 2010

How to: Lidgren Network

Lidgren Networking
Homepage: LidgrenHome

Lidgren Network is quite easy to use networking library. It can be used for anykind of software.
I started to work with this library about month ago. Before that, i had never ever done anything that relates to networking.

Setting up chat server was quite easy. After that, i started to think about game server architechture, handling of multiple different kinds of packets, worldState handling, client updating, bandwidth usage.. To be honest, its big task, to build working multiplayer game server and client. You have to handle ever growing amount of different kind of packets. Keep updating clients, with data they need etc. It took me a while to understand all this.
I thought first, that internet must be full of example code, but no.. it's not. Atleast i did not find any. Or if i did, it was so professional code, that i could not even read it.

Things are about to change :)

I created small console based "game", that works over internet, but you can test it on your localhost. When it ask's for IP, just type "localhost". It's very simple example and i tryed to comment it, but feel free to ask help, if something is confusing.
This example covers the very basics of game networking. Message handling, movement, server world state handling etc. Hope you learn something from it.
Here it is: Game Network Example

Monday, July 5, 2010

How to: Mercury Particle Engine

I was looking for good, fast and reliable particle engine, for XNA and after a while, i decided that i will use Mercury Particle Engine. It looks fantastic and its fast and it comes with graphical particle effect editor. How cool is that? Theres really no limit, on what kinds of effect you could create.

Mercury project page can be found from here!

( Source of this tutorial and compiled exe )
Download: MercuryExample.Zip

Only thing that bothers me, is the lack of tutorials for it. There really isnt anything, that helps "newbie" coders to get those beatifull particles from editor, to XNA. Well, i made a short example project, that loads and draws the "BasicExplosion", that comes as an example with the editor.

Its quite easy to get particles to drawn on screen. You need to add that particle xml file and all the textures it needs, to content of your XNA project. After that, its just coding.

// Add MercuryProject.dll reference to your project
// Add using statements
using ProjectMercury;
using ProjectMercury.Emitters;
using ProjectMercury.Modifiers;
using ProjectMercury.Renderers;
Inside the Game class, add:

 // Renderer that draws particles to screen
 Renderer myRenderer;
 // Particle effect object to store the info about particle
 ParticleEffect myEffect;
 Inside the Game constructor add:

 // Create new renderer and set its graphics devide to "this" device
 myRenderer = new SpriteBatchRenderer
         GraphicsDeviceService = graphics

 myEffect = new ParticleEffect();
Inside LoadContent add:
  myEffect = Content.Load("BasicExplosion");

Inside Update add:
  // get the latest mouse state
  MouseState ms = Mouse.GetState();
  // Check if mouse left button was presed
  if (ms.LeftButton == ButtonState.Pressed)
         // Add new particle effect to mouse coordinates
         myEffect.Trigger(new Vector2(ms.X, ms.Y));
  // "Deltatime" ie, time since last update call
  float SecondsPassed = (float)gameTime.ElapsedGameTime.TotalSeconds;
And finally, add to Draw:

Now, compile.
Remember to add the dll to Content references too, or else, it wont know how to import those files. Now, if its running, just click with mouse and enjoy.

Saturday, July 3, 2010

How to: Lua + XNA

Binding Lua to XNA, may sound hard, but its not. However, you can't easily create fully scripted game logic. I guess the reason is the fact, how XNA works. Internals of XNA are calling, update(), loadContent(), Draw() etc and if you dont let it call ( ie. stay in while(scriptrunning) or something ), it messes things up.

Lua can still be used as scripting language, but its only used to load, save and such. You cant just run some script, you have to create it event based. That means, that you check in update() loop, if something needs to be done, then do it and let the XNA do the magic again, until next update() comes.

In order to get Lua working, in your XNA app, you have to do few easy steps.
Step1: Download LuaInterface
Step2: Set it as reference dll in your current project
Step3: Add some code:
Lua LuaVM = new Lua();  // Create new Lua instance// Register Function params 1= name you use IN lua, to call this method
// 2 = object target, the object, whos function you are registering
// 3 = Method you want to call LuaVM.RegisterFunction("luaName",this,GetType().GetMethod("MyFunction"));
LuaVM.DoString("LuaName()"); // Executes Lua code. This example calls MyFunction()
So, this is all you need, to get started with Lua in your own projects. What ever is your method, to get the command input from user, its fine, as long it can be changed to string and passed to DoString(); Notice thou, that when registering method, you must call it in Lua as method, so "LuaName" turns to "LuaName()". You can use parameters in there and do what ever you want. 

Other important funtion in Lua is "DoFile()". You pass a file path/name and it executes it as a Lua code. With this, you could ( for example ) load starting settings for game levels, or different kind of characters or something. I use Lua in my current project as a quake style console. I can set engine variables and call engine funtions while game is running, witch makes testing phase a lot easyer.
LuaVM.DoFile(string path); // Execute file, full of Lua stuff
You can also set references of objects to Lua. You can do it like this:
LuaVM["MyLuaObject"] = MyCsharpObject;
LuaVM.DoString("MyLuaObject.Property = 100"); // This changes MyCsharpObject Varible named Property to 100;
If that object has methods inside it, you can call em easily from Lua
Simple, eh?
Thats all you need to know, in order to get started with Lua. I might add some working example project, if its needed.

Friday, July 2, 2010

Hello World!

My name is Riku Koskinen and i have coded for fun, for few years. My main language is C# and for games i add XNA to it. I love to code. Im not much into game logic coding, but creating "engines" and such, is what i like. Im not expert, in any way, but i know my way around.

Im 27 years old, im studying Business Information Technology at HAMK University of Applied Sciences, in Finland. Im not expert at english language either, but i hope you understand, what im trying to say.

I certainly hope, that i can provide some usefull info, that might help someone, when troubles come.

First real blog entry will come.. soon.