Embedded IDs in Network Structures

Here’s an issue I’ve been wrestling with and cannot come up with a solid solution:
Should IDs be included inside structures, or is that metadata that should be encapsulated at a higher level?

Here is a more concrete example. We are tracking the state of a given Fighter in the game. This is a stateless server environment, so we need to transfer as little information as possible, but we must still describe everything. We assume there is a larger datastore on the server that has additional constant “definition” information for each Fighter.

Here is what the definition looks like for the external metadata version. Incidentally we are using Thrift, but the issue is the same in any environment.

// current state for fighter.
struct FighterState {
1: required i32 CurHealth;
2: required i32 MaxHealth;
}

struct FighterList {
// indexing
1: required map fighters;

// connects a fighter to the definition datastore.
2: required map definitionIndex;
}

Immutable information is clearly separated from mutable. However, every method that needs to operate on a Fighter requires two parameters: The FighterList and the FighterID to identify which one is involved.

So this is what the information looks like if we encapsulate it all in one location.

// current state for fighter.
struct FighterState {
// indexing. this is immutable.
1: required FighterID FighterID;
2: required string DefinitionID;

// actual state
3: required i32 CurHealth;
4: required i32 MaxHealth;
}

This produces a mixed-mutability object. This feels less than ideal. Every time I transfer the state between client and server, I repeat the DefinitionID. However, if I want to pass a Fighter to a method, I can simply pass the FighterState and have enough information.

So let’s approach this in another way. What if we use compositing?

// current state for fighter. This is mutable.
struct FighterState {
1: required i32 CurHealth;
2: required i32 MaxHealth;
}

// immutable
struct FighterInfo {
1: required FighterID FighterID;
2: required string DefinitionID;
}

// combine them for runtime use
struct Fighter {
1: required FighterState State;
2: required FighterInfo Info;
}

We have now clearly separated the mutable from the immutable. We also can pass just the one Fighter to methods. But this feels very wordy. Also, the needs of the client and the server are slightly different. The server needs the complete “view” of the fighter definition, while the client can use a trimmed down “view”.

This can be simplified by merging the FighterInfo into the Fighter. Not quite as clean of a separation, but it maintains the mutability model.


// current state for fighter. This is mutable.
struct FighterState {
1: required i32 CurHealth;
2: required i32 MaxHealth;
}

// Network view of the fighter. It includes a mutable base, but the additional fields are immutable.
// This is minimal information for sending across the network.
struct FighterNetworkView {
1: required FighterID FighterID;
2: required string DefinitionID;
3: required FighterState State;
}

// the server is now able to add additional information - the complete definition for the fighter.
// This no longer must be handled in Thrift, it can be a subclass of FighterNetworkView, which simplifies accessing.
struct FighterServerView {
1: required FighterNetworkView Network;
2: required FighterDefinition Definition; // we no longer need to look this up externally
}

Now it is implied that only the base FighterState is mutable, while preserving just one parameter to methods. Additionally, this assists unit testing because there is no longer an external dependency to grab the Definition information at each use.

This still feels slightly wrong however because we are making a FighterServerView technically “mutable” because the base class is.

On the other hand, I’m used to SQL tables where IDs are regularly included with the data rather than external, necessary for lookups.

Opinions anyone?

This entry was posted in Uncategorized on by .

Bottom-up Dependency Injection in Unity3d

This is a potential solution to the dependency injection problem. That is, when an object needs access to some shared resource, how does it get a reference to it? Shared resources are typically handled in games as Singletons, but in truth that is not strictly necessary. Some of those resources could be unloaded and reloaded on an “as needed” basis. Why do we care? Because to test the client object, we could ideally use something completely different for the datasource and the client will be none the wiser.

 

Why not Editor references, or prefabs? Because a reference is meant for indicating a controller-controlled relationship. The object with the reference is the controlling object, meant to make changes to the controlled object. If we were to add references to dependancies as well, then it would give us dependency injection. The catch is, if we later want to change the object we depend on, perhaps to change the specific subclass, the reference that the child objects have can be invalidated. Then you would have to manually reconnect every single object reference to the new object, manually.

This goes the same for prefabs. There are ways of keeping the prefabs sane, but one change (like deleting an unrelated GameObject from the prefab) will require a new prefab, invalidating every reference to (and from) it.

 

This solution is designed for game/application code. It is not designed for separate libraries or assemblies. it also is designed as simple as possible. Therefore some performance costs are incurred so that the final code can be simpler.

First we define how a piece of game code will request a dependency. Perhaps it should look something like this:
SharedObject thing = SharedObject.instance;
That sure is pretty. The only gotcha is, when our SharedObject is an interface, we can’t declare an instance with a static property. Okay, then let’s get lower level.
SharedObject T = DependencyRegistry.Find();
Not bad. Brief, but effective. We intentionally avoid the DependencyRegistry.instance.Find<>() pattern because it’s more wordy. We will never be passing the DependencyRegistry as a parameter to a method, for example.

What if the caller needs to choose from a list of providers of the same class type? Perhaps a list of database connections. We will consider this a rare case, as most of the time you just need a single class or interface implementation. In any event, this is type-safely solved by creating a wrapper class or interface that offers the list in whatever form makes the most sense for the task at hand. It could be a list object, or even a custom class that has a field for each specific instance as needed.

That SharedObject.instance thing was nice though. And it would work perfectly fine for any concrete class. At first blush, it would seem inconsistent to allow two ways of accessing the same feature. However, the mindset behind an interface is very different from a class, so we can allow it. We just need to remember that a concrete provider that isn’t a singleton has to define an instance getter that resolves to the Registry.

Okay, API defined. How do we implement it? Clearly some searching is needed. Initially I considered using the Transform Hierarchy to search, but I realized that most objects in that tree will not be providers. Additionally, some of the providers may not even be in the same Transform root of the hierarchy. This leads me to creating an object registry. That is, a central list of objects that offer themselves as datasources.

 

The registry itself is as simple as a list of objects. We chose weak references so that a provider could use the OnDestroy or IDisposable interfaces to know when no consumers are making use of that provider any longer. Also, there should be relatively few providers at any one time, so the ram overhead should be negligible.

As an optimization, we assume that objects added later to the list will be more relevant to the potential consumers than the earlier ones. Therefore when searching we start at the end and work our way backwards.

Getting added to the registry is as simple as asking to be added. To prevent waste, we dedup additions. This can be safely done from within a MonoBehaviour’s OnEnable() method. This will be called before Start(), and work correctly from within prefabs as well as scenes. Likewise, removal is easily done during OnDisable().

It is not required that you add/remove yourself to the registry at these times. You can add or remove yourself at any time. The only things that care are the potential consumers. It is perfectly possible for a consumer to sit in a Start coroutine and wait for the provider object to become available.

The full code will be made available as soon as I can work out permissions with my bosses. That said, it’s a simple enough design that implementing it yourself would be an easy task.

This entry was posted in Uncategorized on by .

Advertising Done Right

Your attention please. Please yell if you’re paying attention. This is worth watching. We will wait. YouTube Video

This is how advertising should be done. I don’t really know or care anything about the product, or the company behind it. But seeing this video changes my mind. *This* is exactly how advertising should be. It changes my mind, without making me feel bad for doing so. Yes, it uses the “tug at my heartstrings,” but it is more about hope than it is about selling something. It respects the viewer, instead of pandering to them.

This is worth pointing out as an example of what this world should be more like.

This entry was posted in Uncategorized on by .

Don’t Fear the Button

I’m afraid to push a button.
 
User interface design is something I care about. I am a developer, which practically makes me a power user by default. And yet I still come across boneheaded design in commonly used applications. Design that would make computers less accessible to the general public. Worse, in places that didn’t previously have these issues.

Latest: Youtube on a desktop browser. Drag the playhead to sometime later in the timeline. If it’s not loaded yet – tough cookies! It will only work on the section that’s loaded. Worse – it changes the “scale” of the playhead to only show what’s loaded, but *only while you are holding the playhead.* Absolutely no semblance of user expectation.

Why is this suddenly too difficult for YouTube to manage? I know a year ago I could easily have just skipped right to where I wanted to be, and simply wait for the data to download starting at the new location. As a developer, I can imagine why this choice was made. Perhaps the data format is not well suited for seeking. Is a “please wait” indicator too much to ask? Then the playhead scale will stay consistent. The user will have an understanding of where things stand. Instead, somewhere inside Google, a bunch of extra code was written to support this “seek only within loaded area” code, with special cases for the display and input system. Someone had to deliberately break the user’s trust.

Okay, fine, I can’t move the playhead to the actual intended target. There is another button, with the tooltip “watch later.” Maybe that will download it in the background so I can close the page and return. Oops! Nope, that means… well, I’m not exactly sure. Some menu shows up on the left side of the browser and there is no indication of how it relates to the action I’ve requested – “watch later.”

That’s not what I wanted it to do. But I’m afraid that if I push the button again to “undo” whatever I just did, maybe the page will reload and I’ll have to wait for the whole thing to reload all over again. At this point there is no trust between me and the application that it will do anything that I expect it to.

We must always keep a watchful eye on our designs, whether they be games, websites, or applications. Every “feature” needs to be examined through the lens of the intended audience for clarity, not confusion. This is an ongoing battle that has been fought for many years, and will be for many years to come.
 
Let’s not forget it!

This entry was posted in Uncategorized on by .

Why I’m not worried about PC and Console decline

A good number of well respected (and better read) people than I have lately voiced concerns about the state of indie and art gaming in the near future. Consoles, once the easiest and cheapest way to get hyper realistic games, are now regularly passed on the turnpike by off the shelf PCs. And the PCs are slowly being replaced with simpler tablet touchscreen devices. Devices that can play games whenever you want, wherever you are. And, barring some boneheaded designs, are as easy to play on as their original Console counterparts.

 
Good.
 

I’ve been around long enough to know the phrase “zero wait states”. The idea was that the machines will get so powerful that there will be no time delay between when you ask a computer to do something and when it happens. Some current systems take that to heart, most notably Apple. Most current systems are so mired in their computer science that the feel of a machine is a distant fifth priority item to anything else. That is a serious disservice to the power our current computers command. I bring this up because it is no longer vitally important to know that this machine has 1.21 gigawatts of processing power. These easier to use devices *should* be where we are going.

And yet people are freaking out that there will no longer be a venue for “that” kind of application or game. Why not? Touchscreens? They can certainly use controllers just like consoles, if the game works better for it. Big displays? The internal resolution of most tables far exceeds our “high def” televisions, and the devices have been able to drive them for years.

So what is the problem? Visibility of indie and art games? There is this strange thing called an interweb thingy where you can see other peoples opinions about all kinds of things. So what if Steam suddenly closed its doors on PC? As a website alone, even if it were no longer directly selling a thing – it would still be a highly respected resource for new games on the Internet because of its partially crowd based decision system. This is all it takes.

So what is all this grousing about? Things are getting simpler. Apart from a few better cable combinations (free charging cable with hdmi out for iPad) and some extra software support (his controller support via Bluetooth), we are already there with a bright future. People who want to know should be able to learn what they want about machines. But for the bulk of the world, they should not *need* to know. And that is exactly where we are heading.

This entry was posted in games on by .