Unity Community |

So when you say its event based and not stream based, does that mean no serialization? Everything is reliable/unreliable RPC's?
There is a stream for tranform data + extra data you can attach, but it's mostly handled automatically by the engine and not something you need to think of. You can write your own position tranform/data stream synchronizer if you for example only need 2 axes (2.5D game, etc) or only one axis of rotation, etc. Or if you want to attach some extra data like rigidbody velocity.
But in general, most of the data you send back and forth is done using events (not RPCs, they exist also, but are of little real use other then for one off things like "Login", "enter world", etc.).
Last edited by fholm; 02-21-2012 at 07:23 AM.
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
Interesting... so what's a unity code sample look like of say, a client executing an attack or something? both the client side request and the server side event?There is a stream for tranform data + extra data you can attach, but it's mostly handled automatically by the engine and not something you need to think of. You can write your own position tranform/data stream synchronizer if you for example only need 2 axes (2.5D game, etc) or only one axis of rotation, etc. Or if you want to attach some extra data like rigidbody velocity.
But in general, most of the data you send back and forth is done using events (not RPCs, they exist also, but are of little real use other then for one off things like "Login", "enter world", etc.).
We can take an example from the demo (simplified it a bit to make it easier to understand), first you need to define an event, here's a simple CastSpell event:
Code:
public sealed class CastSpellEvent : Event<Actor> { public override byte EventId { get { return HeaderBytes.UserStart + 3; } } public override ProximityLevel ProximityLevel { get { return SlimNet.ProximityLevel.Horizon; } } public override int DataSize { get { return 2; } } public override bool Reliable { get { return true; } } public Actor SpellTarget { get; set; } public CastSpellEvent() : base(EventTargets.Owner | EventTargets.Remotes, EventSources.Owner) { } { stream.WriteActor(SpellTarget); } { SpellTarget = stream.ReadActor(); } }
Explanation of the standard properties of the event:
- EventId is a byte that is unique to this event, basically it's id.
- ProximityLevel is an interest based flag that decides what players should get an event based on how close they are to the origin of the event
- DataSize is the size of the data in the event, in this case 2 as we're sending an actor reference over the wire (the target of the spell)
- Reliable decides if the event should be reliable or not
The property SpellTarget is specific to this event, and it's the target of the spell (duh ;p).
The call to the base constructor is a bit specific, the constructor in SlimNet.Event<T> looks like this:
Code:
EventTargets controls what peers the event should be raised on.
EventSources controls what peers are allowed to raise the event.
In this case of the CastSpellEvent the event will be sent to the owner of the actor (game object) and to all remote players. It's only allowed to be raised from the owner of the object (it would not make a lot of sense to raise "cast spell" for an actor you don't control)
Pack and Unpack is basically the Serialize/Unserialize methods which packs or unpacks the data of the event.
Now, assume you have a target selected and want to throw a fireball at it, it would look like this:
Code:
actor.RaiseEvent<MMODemo.CastSpellEvent>((ev) => { });
This event will be raised on your local client (as you're the owner) and then sent to the server which distributes it to all other clients, they listen to the event by doing this:
Code:
{ actor = this.GetSlimNetActor(); actor.RegisterEventReceiver<MMODemo.CastSpellEvent>(onCast); } void onCast(MMODemo.CastSpellEvent ev) { // Cast spell }
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
Also note that all events that are sent are automatically synchronized to the movement of the players on the remote clients and server, so there is no need to manually align movement and actions, it's done automatically for you.
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
After some really good feedback from a user on IRC I implemented an "embedded development server" inside Unity which compiles and runs the server for you when you hit play, easiest way to explain it is to just watch the video and see it in action:
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
I'm impressed how easy it is to set up quick networking with this framework. There's some things I'm not a hundred percent sure how you'd set up. Like I'm trying to get a server to only send nearby entities. Additionally, I want my server to unload sections of the game world not currently in use (my game requires sending map data as the player moves around). It's probably not terribly hard, I guess I'm just not used to this kind of coding standard. It looks really good so far though! I'll mess around with it some more to see if it's what I need.
I Might be blind or something, but don't understand how the system knows to instantiate "Player" prefab in the resources folder of the demo. Been searching for 2 hours. Might be missing an open door here?![]()
ActorExtension.cs
Call me stupid but this is the code of actorExtension:
Code:
/* * SlimNet - Networking Middleware For Games * Copyright (C) 2011-2012 Fredrik Holmström * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * * Attribution * The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. For any works using this * software, reasonable acknowledgment is required. * * Noncommercial * You may not use this software for commercial purposes. * * Distribution * You are free to share, copy and distribute the software in it's original, * unmodified form. You are not allowed to distribute or make publicly * available the software itself or it's sources in any modified manner. * This notice may not be removed or altered from any source distribution. */ using UnityEngine; namespace SlimNet.Unity { public static class ActorExtensions { { return GameObjectMap.Retrieve(actor); } { return GameObjectMap.Retrieve(actor).transform; } } }
But where does it define that the "Player" prefab should be used as the instantiate object "Actor"?
it is not in ActorExtentions.cs but in peerutils.cs
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
Sorry, he changed it in the latest version, it's located in the PeerUtils.cs
Ok I am finding it very hard to understand the structure etc. How does this work? There are 3 prefabs, and for some reason it chooses "Player.prefab" to be the prefab for input etc. I am sorry if I am lacking skills to see this through just like that, but none of the videos talk about this, and for some reason, I just don't get it.
Why is player.prefab loaded as the actor for input/camera and other players?
Next to that, what happened to the idea of exporting the scene for the server? Is it still authorative?
It looks like a promising framework and I'd love to give it a spin but for some reason I get confused like crazy..
So yeah I might have been sleeping like a baby or something. The following code loads the actor based on what the derrived name is of ActorDefinition:
Code:
The contextPlugin of MMODemo.Server spawns the actor with prefab "Player" since the name is PlayerActorDefinition (above code removes actor definition to define the prefab name):
Code:
{ Context.PlayerEventHandler.Raise<SlimNet.Events.Authenticated>( (ev) => { ev.IsAuthenticated = true; ev.Error = ""; } ); } }
I am just filling this in for future reference for others, so they don't have to go through this.
The only thing I do not understand yet is what config-prefabs.xml has to do with this, and if it does anything, how I'd go and generate it.
If I would want to spawn say, a car, and this object would be important because the player drives in it and thus its location should be synced at all players based on the input, would I make something like a "CarActorDefinition" and spawn it at some point with a velocityEvent attached?
Kind regards
Jan: I will try to give you a longer response later, config-prefabs.xml is part of some old, now unused code and is not needed anywhere. 0.2.1 will be a nice cleanup of all the "old code" being removed + embedded editor + linux/osx support + a proper video tutorial of about 10 parts.
though, as I said in another post on this forum, I took a big piece out of my index finger with a steak knife yday and can't program for about it a week. It (0.2.1) was supposed to be out mid-end next week, but I have to rest my left hand sadly ;p
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
Hey man didn't know that no worries. Make sure your finger heals right. It just takes a lot of time to go through it all. Have you considered one or more github repos? As I am spitting through for example I'd be happy to make notes here and there..Jan: I will try to give you a longer response later, config-prefabs.xml is part of some old, now unused code and is not needed anywhere. 0.2.1 will be a nice cleanup of all the "old code" being removed + embedded editor + linux/osx support + a proper video tutorial of about 10 parts.
though, as I said in another post on this forum, I took a big piece out of my index finger with a steak knife yday and can't program for about it a week. It (0.2.1) was supposed to be out mid-end next week, but I have to rest my left hand sadly ;p
I have three github repos, but they are all private currently.
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
Ok, let me know if you want me to help out on something.
for now my scenario is:
I want a network synced car that can be controlled by a player. Obviously when the player enters the car it does not need to sync the transform anymore and would become dependant on the car. The car should then start syncing. The player would probably become a child of the car transform (sitting right etc). I'd figure this would be an "enter car event" where the player actor would stop syncing transform and the car starts syncing? Further I guess the car that the player sits in should start beeing considered "IsMine" as well.
Right now I created a CarActorDefinition and CarBehaviour. It instantiates some dummy Car prefabs correctly when the client connects (obviously). How would you suggest the above scenario and further more, am I following your pattern when I'd be doing it this way?
JanHelleman: Currently, the engine doesn't support changing ownership of an actor, this will be supported in 0.2.1 however. Other then that what you described is probably what I would do also.
Also, I'll throw in a quick update on 0.2.1. It's done, but I'm holding it back while finishing up a much better version of the example game and the tutorial videos. Expect everything to land early next week. Here's a screenshot of the demo, complete with working chat + player portrait + three spells.
(large version: http://dl.dropbox.com/u/19877504/021_demo_3.png)
My Unity Assets: RPG Controller & Camera, Ultimate Minimap, Combat Text, Rewinder, Voice Chat, Fog of War (Desktop), Fog of War (Mobile), Lidgren Basic Setup, AoE Spell Targeting, Sniper Scope Textures
Need unity consulting? Contact me over PM or Here
Cool! Ok, now subscribed to the 0.2 Beta thread. I'm going to take a look at this over the weekend. Excellent stuff from what I can tell so far!![]()