text:
What is the recommended way to store game entity data?
The following is specific to my problem, but I believe the principles can be applied to any game.
I am currently working on a 3D game which will have many different types of spaceships.
The physics and behavior of the spaceships are controlled by a base 'Ship' class. This base class in turn derives from a 'Drawable Object' class which handles the transformations and drawing of the ship.
I want the spaceships to have a set of stats such as

Max speed
Cargo capacity
Weapon power
etc

that can be passed into the physics and behavior functions.
However, during gameplay new spaceships will be able to be bought/upgraded, but i want to be able to pre-define a (possibly large) list of setups that will be available to the player.
What is the best way to store the available stat setups?
Should I be using a large JSON/XML file or similar to store the stats which is loaded at runtime?
Or might I just as well hard-code the stats into the constructors of a set of derived classes?
The actual physics attributes (location, velocity etc) and its stats of a particular spaceship are being saved to file when the game is actually played.
I have researched about entity-component-system architecture, but I haven't found much about how to define these entity stats.
Any advice on improving my entity architecture would be much appreciated - this is one of my first attempts at hobby game development.

JSON/XML is a good way to save your data, and it's going to be easier to edit them than having them hard-coded in your ctors (because your content-creators would not be able to change them). Your current approach of using inheritance is much against the entity-component-system philosophy and opens up the door to some shooting yourself in the foot.

@AlexandreVaillancourt Should I instead be using a 'ship' class that has a 'drawable object' class as a field? I'll do some more research and maybe look at re-structuring my entity classes.

At the core of the entity-component, you'd have an entity id, with components that would represent a ship, components that represent visuals, and components that represent physics. So there would not be a "ship" class, only a "ship" system that would update ships logic. Having a 'drawable object' in a 'ship' class is the first step instead of having a 'ship' object deriving from 'drawable' because what if you have ships that are off-screen?

Having the ship stats in an external file would likely be a lot easier to maintain.

You can modify values without having to recompile your game binary
When you are working in a team with non-programmers, these can be taught how to edit these files on their own and contribute to the game balancing. It might even enable them to add and remove ships without help of a programmer.
When you pick the right format, editing the values will likely be a lot more convenient because of less language syntax around the actual values (this problem can be better or worse depending on what programming language you use. For example, when you use Javascript, a JSON config file and a file which initializes an object with fixed values is exactly the same thing)


text:
How to achieve high Level of Detail on 3D models
I'm developing a 3D space-based game which allows free movement in all directions and features planets. Currently I am using the Visual Studio content manager to import models (spheres) with 2D textures applied to them. These planets are supposed to be very large, which allows the player to come very close to their surfaces.
From afar, the textures look great. Up close, however, they become very blurred or pixelated (depending on whether I draw them with linear or point clamp).
I understand that I can enable mipmaps for the textures to change the level of detail shown at different draw distances, but obviously this can only be as high resolution as the texture files I provide it. The Visual studio content pipeline only allows Texture2D of size 2048x1024 or less and obviously I don't want to be holding enormous image files in memory for use in game.
Should I produce a set of higher resolution tiles of small sections of the planet's texture, load them on demand and stitch them together? How would this be achieved in XNA?
The effect I'm looking for would be similar to google maps.
It may be that in the future, I will decide that the textures being blurred or pixelated is acceptable, but since this is much a learning process for me, I'd still like to know how it would be solved.
Below is a demonstration of what I mean using a Mercator map of the Earth as an example:

do you mean that when you zoom in, the detail is lower than what you would normally see if you zoom in that texture from an image editor ?

I'm pretty sure he means something like texture streaming in Arma, where textures get loaded in a higher resolution the nearer you are to an object.

Yes, that's what I mean. I'd like to be able to keep the texture looking sharp regardless of how far/near the camera is to the planet.

The way Google does it
The way Google Maps does it, is indeed by having multiple levels of detail. Moving through these levels, tiles will get smaller and smaller. Fitting a 2k texture on a tile that contains a full continent, will give you less detail than fitting a 2k texture on a tile that contains a small neighbourhood with only a few roads.
It is important to note than tiles get loaded, depending on what area you are looking at (your region of interest). You can't simply switch from a 2k texture to a 4k texture as the player zooms in on the planet, as you will run into memory problems very soon, due to the exponential increase in image data. Instead, you will want to draw only the segment of the sphere the player is looking at, and only load the associated textures. What is off camera should not be rendered, nor should the textures be loaded.
The problem with this approach is, that you will need an extreme amount of textures to get the fidelity Google Maps will offer you. If you want to be able to zoom in, all the way to street level, you need textures at that level (and all levels before), for all tiles across the entire planet. Google has this data via years of gathering satellite images, but it's infeasable to have this data available in a game.

A feasible way you could do it
For that reason, I think only a procedural approach is viable in this case. A procedural approach means that the planet's geometry and textures get generated on-the-fly, via some mathematical formula. Techniques for this are readily available if you do a search on "terrain generation" or "procedural planet generation". This approach let's games like Spore and Destiny generate planets with detail up to surface level.
A downside is that you have less control over what your planet will exactly look like. For example, if you want a planet that looks exactly like earth (with bodies of water, mountians, roads, etc in the same position, you will not be able to do this in a procedural way). It is however the only feasible way to "store" an entire planet in great detail. (The key is that, rather than "storing it", you are encoding it as a set of parameters, generating actual geometry and textures on-the-fly).
Something to be aware of (as Rioki pointed out), is that textures slapped onto a sphere will not cut is. At higher levels of detail, you will need to deform the geometry (for example around mountains). Techniques such as displacement mapping or tessellation will help here. Procedural methods will give you this data for free however.
In short, your question is a problem of scale. A procedural approach is a very feasible approach for planet rendering in general, but I'm not sure if it fits your use case. If your use case is to render planet earth specifically, at a level of detail Google Maps offers, I'd say that's impossible as you cannot store the amount of satellite imagery that would be needed for the textures.

If you want to space game where you can get close to the planets you actually need to model the planets in a significant more realistic fashion, just using one humongous texture is not going to cut it.
Now it really depends on the level of fidelity on what you want to do. But the problem with the scale of planets, when they fill your almost your entire view, a large part of it is not visible.   To maintain a decent performance you need to be able to render only the relevant bits. Also you need to account for the different materials aspects, like terrain and oceans' reflectivity. Also things like atmosphere and weather play a role in the plausibility of the planet.
There are a myriad algorithms you can try, but but you may want to look at this algorithm that extends ROAM to spheres: A Real-Time Procedural Universe, Part Two: Rendering Planetary Bodies Also here is a list of research papers on this subject.
If you want an extreme illustration of the approach, you can watch this Infinity Engine Demo

This method will save you some resources

create a sphere for your planet with a small height map, make your texture 2.5k for compact pixels
add sphere with transparency + weather around the planet.

that is the best way to fudge planets for games just ask bungie

source: Stack Exchange

license: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/

sort: votes


text:
Improving performance for flocking behavior?
I have a game that I am trying to make. It is a twin-stick shooter that I want to design to have a LOT (Up to 500) of enemies on the screen. The game's base design comes from that of Geometry Wars, and serves as a learning experience.
A bit about my setup: i7 quad-core CPU @ 3.5GHz, 8GB of RAM, and Intel 4000 integrated graphics. While these aren't the best specs, they are now more and more common, so this is my target system.
There is an invisible plane that acts as the playing field. I built a NavigationMesh from this plane, and sample random points from that to spawn my enemies. The enemies use DetourCrowd to implement the flocking behavior. On my moderately-powerful machine, I can get around 150 enemies before I begin to experience drastic performance drops. Accounting for other potential issues, I made my enemies invisible, so only the agents are used, and still the performance drops significantly from 150-200 enemies on my machine. It is bad enough that this difference of 50 enemies drops my framerate from a solid 60 fps to <1 fps.
I decided to instead try using Bullet physics instead. I have the linear/angular factors setup to make Bullet act on a 2D plane. All of my enemies now use a Sphere for collision, have a mass of 1 unit, and no friction. They are not affected by gravity or external forces, but instead I set a fixed impulse at the beginning of each physics step. Additionally, the collision mask is setup so that the enemies only collide with each other. Around 350-400 enemies, I begin to see performance issues. They're not nearly as severe as the performance issues that I see with DetourCrowd, however they do come with an additional downside.
I use a small sphere set as a Trigger for firing off bullets. I handle the collision events to destroy the enemies. With the DetourCrowd method, this caused no noticeable performance drop. However with the Bullet approach, it causes a much more significant performance drop.
Overall, I can comfortably use ~120-150 enemies with the DetourCrowd approach, and ~200 enemies with the Bullet approach.
Are there any solutions that would give a much cheaper flock behavior for this game? I prefer solutions targeting 2D under the assumption that it will be cheaper than a solution targeting 3D.

Intel released an crowd collision avoidance algorithm and sample code a few years ago that's designed to solve this problem. It's parallelizable, and you may be able to adapt the algorithm to work with your entities.

all_licenses: Creative Commons - Attribution Share-Alike - https://creativecommons.org/licenses/by-sa/3.0/


text:
How to move a cube withour rolling it?
I am trying to move a cube with my keyboard on a plane surface. However, the cube always rolling instead of moving. How could I slide the cube on the plane?
What I have done:

I added a plane game object with a mesh colider
I added a 3D cube object with a box colider and rigidbody
I appended a ControllerScript to the cube

ControllerScript

private const float Speed = 10;

private Rigidbody rb;

void Start()
{
    this.rb = GetComponent<Rigidbody>();
}

void FixedUpdate()
{
    var x = Input.GetAxis("Horizontal");
    var z = Input.GetAxis("Vertical");

    this.rb.AddForce(Speed * x, 0, Speed * z);
}

It looks like the AddForce method upset the cube. I already tried to reduce the friction of the plane and the cube, but this only delays the problem.
How could I move the cube without rolling it? Whats the common way to do it?

try this http://answers.unity3d.com/questions/120956/addforce-causing-skewed-movement.html

Could I merge the meshes of a simple cube? I tried AddForceAtPosition, but it creates a similar behavior. The cube gets upset if I push the button too long.

Can't you just constrain certain rotation axis on the rigidbody?

Add the following line to your Start() method:
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ;

This will prevent the object from rotating on x & z axis, which will prevent it from rolling. If you don't want it to turn sideways as well, add FreezeRotationY as well, and then it will not rotate at all but only move:
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezeRotationY;

If at any point you wish for it to be able to roll, for any reason, you can re enable the rotation by using:
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;

You can

Freeze the rotation
Change the transform's position(with translate, not recommended for physics though)
Set the rotation equal to itself


text:
Character can't jump in other angle with fixed angle off
I turn off fixed angle so my character can rotate, but it can only jump in 1 angle but I want it to jump in every angle.
How do I do that?
Code:
using UnityEngine;
using System.Collections;

public class Player: MonoBehaviour {

  public float maxspeed = 10f;
  bool facingRight = true;

  Animator anim;

  bool grounded = false;
  public Transform groundCheck;
  float groundRadius = 0.2f;
  public LayerMask whatIsGround;
  public Vector2 jumpDirection = new Vector2(0f, 0f);
  public float jumpFocre = 500f;
  bool doubleJump = false;

  void Start ()
  {
    anim = GetComponent<Animator> ();
  }

  void FixedUpdate () {
    grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
    anim.SetBool ("Ground", grounded);

    if (grounded)
      doubleJump = false;

    float move = Input.GetAxis ("Horizontal");
    anim.SetFloat ("Speed", Mathf.Abs (move));

    GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxspeed, GetComponent<Rigidbody2D>().velocity.y);
  }

  void Update()
  {
    if((!doubleJump) && Input.GetKeyDown ("up"))
    {
      anim.SetBool("Ground", false);
      float move = Input.GetAxis ("Horizontal");
      jumpDirection.x = move;
      GetComponent<Rigidbody2D>().AddForce(jumpDirection.normalized * jumpFocre);

      if(!doubleJump && !grounded)
        doubleJump = true;
    }
  }

  void Flip()
  {
    facingRight = !facingRight;
    Vector3 theScale = transform.localScale;
    theScale.x *= -1;
    transform.localScale = theScale;
  }
}

You have posted the LevelLoader code, is your current jump code in a different script?

I don't have a different jump script, this is it.


text:
How to setup animation based events without depending on an animation?
How can I have events based off animations without relying on them? Take for example reloading in a FPS game. You'd hit a button to reload your weapon, the animation will play, then animation will hit a certain frame, then the weapon will be reloaded. There's a dependency on the animation eventually hitting that key frame before the weapon can reload and the character can go into another state. Without the animation, the weapon would never reload, and without some arbitrary timeout, the character would be stuck in that state forever.
Checking for the existence of an animation is an option. However, this requires us to still know about some sort of animation, and what it's playing. Am I over engineering this? Would it be okay for the logic of some object to have some information about it's animations?

Introduce a new abstraction: Action
An Action represents something that can be done, and encompassed the time aspect as well as information about the state and state transitions of the action. So a reload action definition might contain information about total duration, relative timestamp of reload complete etc. A spear throw action definition contains information about total duration and timestamp of spear release.
Exactly how you wrap this up in code is up to you and depends on your framework/style. Actions could be simple data objects which you just use to define different actions, and then you make use of the timestamps, durations and other information "manually" in the rest of your code. Or you could improve encapsulation by having your Action module expose events which trigger when the action changes state (OnReloadStarted,OnReloadReady,OnReloadFinished,OnSpearThrown). Or you could have a medium where there are no events, but the Action still manages its states/transitions so that you can poll it: throwSpearAction.isSpearThrown().
If you have multiple reload animations or spear-throw animations with different timings, then you define each alternative as an action, e.g. FastThrowAction and SlowThrowAction. Once again it is up to you exactly how you manage and pick from different alternatives.
The point of all this is that the Action abstraction becomes a natural part of the gameplay model when you focus on the concepts that affect gameplay (action transitions/events) and remove the concepts that don't (animation).
These Action objects can then be used to drive the animation as well. Whenever a FastThrowAction is started, then your renderer/view starts the corresponding FastThrowAnimation.

All animations are more-or-less based on the idea of "keyframes", whereby you define two or more states/poses/transforms and the length of time between them; when 50% of the animations' total time has passed, the animation is 50% complete. Animations like "reload" have fixed time-costs based on the character, weapon, being suppressed, etc..
Your InputHandler will need to ignore invalid input until the Reloading state expires (not when the animation is finished).
During that time, the animation may also progress, smoothly, from 0% to 100%. When the player presses "R", the current time is recorded. If the computer, then, lags out for X+1 seconds, the entire animation will happen "instantly", when the computer catches back up; the InputHandler would also detect the (instant) expiration of the reload state and clear it.
Basically, the game continues playing with, or without, acceptable animations:

Player presses "R"
Set the player's Reloading flag (disable reload-incompatible inputs)
ReloadTime or ExpiryTime is recorded (100% reload after 3.2 seconds)
Update() and Render() for 3.1999 seconds...
If using ReloadTime, subtract delta-time from it, every Update().
(Could be one 3.1999-second update or one-hundred 0.031999-second updates)
During the next Update(),
ReloadTime will be <=0, or
ExpiryTime will be a time in the past
If so, clear the player's Reloading flag (enable reload-incompatible inputs).
The animation is done (not a question).

That will work for something basic like reloading( which doesn't have to be exact ), but consider something more visual like throwing a spear. You'll want the spear to been thrown at some exact time in the animation( where the character actually lets go of it. ) This is not necessarily the end of the animation, because the character might follow through on the throw, and it must be precise to look accurate. Creating the animation to perfectly fit data based on this would be a pain compared to having the animation drive it.

@Ben, how do you determine when the player lets go? I'm visualizing those stupid golf games with the "swing meter". In that type of game, you fully describe the swing, then the animation happens, based on the calculated output. The point at which the club hits the ball is known before the animation starts, so stretching the time between key-frames is trivial. When the calculated time has elapsed (key-frame also happens to be hit), the animation is complete, the ball is accelerated, and then a second, follow-through, animation is played, starting with the player's current state.


text:
Send a prefab over network in Unity
I want to instantiate an object on only the server and one client. This means Network.Instantiate is out of the question and I have to instantiate it manually. Since Network.Instantiate takes a prefab, there must be some way of identifying a prefab over a network( you can't send a reference in an rpc. ) How can I do this? Drag and dropping the prefab from the inspector is also not an option, since the component with this reference is on the server only( the client doesn't have a local reference. )

It turns out Resources.FindObjectsOfTypeAll where T is GameObject, returns all game objects "resources" in the project. In other words this gets all your prefabs. They do not have to be in a "Resources" folder.
Using this list I can now search them by some sort of ID( either assigned by me, or Unity ), then send that ID over the network and find the prefab from that locally.
The problem with this is that the prefab has to be "loaded" on both machines. It's reasonable to believe that if a prefab is loaded on the server, it should be loaded in the client. As long as there is some reference to the prefab in a script( through the inspector, direct or indirect ) or the prefab is explicitly loaded using Resources.Load, it will be able to be found this way.
Also, assets are always loaded in the editor. Don't be fooled by this. It does not mean the asset will be loaded in a build.


text:
Object doesn't move smooth over edges?
In the last couple of days I'm experimenting a lot with Unity 3D to get a feeling about the environment and the behavior of game development. I come from a programming perspective and try to develop as much as possible in Unity 3D.
I created a base controller which creates a 10x10 surfaces area on start. Each part is based on a prefab plane (scale: 0.1, 0.1, 0.1) with an appended box collider. On this surface area I'm moving an object with the AddForce method. So far everything works fine.
However, if my moving object reach the edge of a surface area part (prefab plane) it breaks and I am not able to move the object directly over the edges. Sometimes it stops completely. If I use a bigger prefab plane I'm able to move my object smooth on that part, but it also stops at the edges.

Do you have an idea, why the cube stops at the edges?

Edit

I think this behavior occur because the cube hit a new collider surface. My currently fix calculate an overlaying BoxCollider over all surface elements. With this implementation there are no collider edges.
Is this an acceptable implementation?
public class Surface : MonoBehaviour {

    private static readonly GameObject GameObject1 = new GameObject("Surface");

    private static readonly GameObject GameObject2 = (GameObject) Resources.Load("Ground");

    private static readonly BoxCollider BoxCollider = GameObject1.AddComponent<BoxCollider> ();

    private GameObject obj;

    public void Draw() {
        this.obj = Instantiate (Surface.GameObject2, new Vector3 (this.X, this.Y, this.Z), Quaternion.identity) as GameObject;
        this.Add ();
    }

    private void Add() {
        this.obj.transform.parent = Surface.GameObject1.transform;

        Surface.BoxCollider.size = new Vector3 (this.X, this.Y, this.Z);
    }

    public static Surface Create(int x, int z) {
        var surface = Surface.GameObject1.AddComponent<Surface>();
        surface.x = x;
        surface.y = 0;
        surface.z = z;

        return surface;
    }
}

The single surfaces are created with a simple loop.
private static readonly Surface[] Surfaces = new Surface[Width * Height];

for (var z = 0; z < Width; z++) {
    for (var x = 0; x < Height; x++) {
        Surfaces[x + z] = Surface.Create(x, z);
        Surfaces[x + z].Draw();
    }
}

The texture is blue with a white boarder.

Have you checked Edit -> Project Settings -> Physics settings? I once had a similar issue, maybe you can find something useful there.


text:
How does Unity's Entity-Component System Work In Practice?
On the surface Entity-Component seems like a good way to program games. Everything is a game object and those game objects are made up of components. The attraction is components are very flexible, just requiring you to "add" them to a game object to inherit it's functionality.
So, obviously a good example might be a platform the character has to jump on. Maybe this thing has a collider, a moving component, maybe a rotating component, or maybe even a rigid "RotatingAndMovingPlatformComponent". Okay, that sounds great, just add all these components to the game object and that's it, you have this extra functionality. However, I find that this level of complexity is where it's usefulness ends.
Try this with menus, complex character movement with multiple states, abstract ideas like game modes or game state, and your flexibility and modularity are destroyed. Menus involve a lot of specifics, complex character movement often involves many states meaning either communication between these components is needed or there must be a controlling component made just for this type of character. On top of this, some components will need to rely on game state in some way.
I see very little benefit to programming this way over ordinary inheritance. The lack of object structures at compile time makes doing a lot of things very difficult and unreliable.
I feel the best and easiest way of going about things would be using larger components with more dependencies. I don't see this as a problem ( more like unreal 4's architecture ), but then I wonder what's the point in the Entity-Component architecture.

The title seems to be asking "how does it work", but the body reads more like a rant, or asking why it should be used.

How is this closed as "Opinion Based?" There is only one way Unity works, the guy is asking for that answer. There is only one answer, and it is most certainly not an opinion.

I will risk and post almost-opinion based answer. What Unity is missing is decopled "system" element. It would be much nicer to have "Component-Entity-System" instead of current "Component-Entity". That is because of the problems you mentioned.
The problem of communication you have mentioned can by solved by adding System. Unity Component, holds both, data and logic, so either you build massive Component that does alot of stuff and knows alot. This kind of monolith, can be dropped in another game, but you can reuse part of it. You can also try and break it, by building lots of small components, but you will end up with tons of tiny components exchanging events, messages and stuff. You can "reuse" them, but they won't work alone.
This is where System comes in. System is just a logic. It will tell your game manager "I can make your tree fly, but I need tree to have [wings, physics]". Game manager will then, search for entities with wings and physics, and give it to system. System will then do whatever it has to do to make your tree fly. Wings and physics are components. But different that Unity ones, they will just hold data, like how wide are the wings, or how fast will object fall (it's a flying tree, it will fall fast!). Now, all your components can be reused, as they don't know about anything else than some weird data they hold. They have no meaning, unless some System is interested. But it's System job to read and use the data. System doesn't need to know who has which component, and doesn't need to know about other system. Now you have Components and Systems ready to copy-paste anywhere!
Now how to translate it to Unity? You can write your custom manager, where components are systems, and all they do is logic (and system states if required, like in Menu system you might want to know shorthand which button clicked most recently). Your GameObject is your Entity. What about Models? Well you are using c# or JavaScript, just use normal Class/Object for this, that's why you will need custom manager. To handle non-unity models.
Second option is to split Components into Models and Systems. Component can query GameObject and whole Scene for other GameObjects with certain components. Designate one GameObject as your System Manager. This GO will hold only components who can do stuff. Other Entities in scene, will have components with pure data.
Please note, this is only theory (of adding Systems to Unity), and it might not result in effective architecture for your game.
About inheritance. Easiest way to see the difference is to use both patterns to describe object. Let's describe "Car" in standard class hierarchy. Car will Inherit from wheels so it can move around, thus, car is a wheel. Now in Component Entity. This Object has wheels, and doors, so it's a car!
Few links to articles, about Component-Entity. First one describes Unity like Component-Entity, second one adds Systems.
http://gameprogrammingpatterns.com/component.html
http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/
http://python-utilities.readthedocs.org/en/latest/ebs.html

I didn't like the idea of "car being a wheel" on inhiritance but the overall idea is pretty nice, usually I wrap my logic inside the components themselves.

This doesn't directly answer the question but, from the sounds of it your question and the information you're wanting seem to differ a little (at least to me). So this "outlook" might help connect them.
Abstractions commonly lead to generalizations so you can prepare for future unknowns. This is what Unity does since it knows barely anything about the type of game you're going to make until you start providing that information to it (i.e. game code). The more complex your abstractions (most likely) the more generalized they are.
Generalization is a great thing, it allows you to reuse code between games (with little to no changes), and make adjustments when design details change. However with generalization comes the long told saying Too much of anything is a bad thing. Over-generalization can lead to difficult and tedious implementations to do something simple.
An example:
Let's say you're a game-development company. You like TBS games and plan on only ever making TBS games. Would it make sense when designing and programming your game-state management system to generalize for future potential of RTS games, when you know you're only ever going to make TBS games? No, it doesn't you can cross that bridge if it comes.
Now don't confuse over-generalizing being bad with "don't generalize" because we gain quite a bit of flexibility when doing so. You may know exactly what this game you're making needs 100% (chances are you don't and they'll change), but for sake of the argument lets pretend you do. Well, do you know what the next TBS game you're going to do needs? No, chances are it'll be mechanically different in some way (so that people aren't bored of it before you make it). So you still need some generalization and abstraction just not to the degree that unity does since you know more.
TLDR;
Too much and too little of generalization can be a bad thing (for begginners and single-game developers it is much better to under-generalize). The more you know the less you need to generalize.


text:
How to use polymorphism with the Unity editor
For example:
[System.Serializable]
public class A
{
    [SerializeField]
    public int x;
}
public class TestA : MonoBehaviour
{
    [SerializeField]
    public A a = new A();
}

[System.Serializable]
public class B : A
{
    [SerializeField]
    public int y;
}
public class TestB : TestA
{
    // Not legal, I know. Considering Unity forbids the use of constructors in
    // monobehaviour classes, I have no idea how I'd do this.
    a = new B();
}

Given that this MonoBehaviour class "TestA" exposes an "A" object, I'd like to override this default value so that when a MonoBehaviour of type "TestB" is attach to a game object in the editor, an object of type "B" is exposed.
Perhaps I'm better off looking into creating custom property drawers, but I'm wondering if there is a way to do this with the default editor.

There are some problems with your approach.
Unity doesn't support serialization of polymorphic custom classes not derived from ScriptableObject or MonoBehaviour.
In other words, it doesn't matter where and how you initialized a field, if B if an instance of A it will be sliced of (or won't be serialized at all I don't remember exactly I need to check).
Similar to this answer.
A clarification:
a = new B();

This is legal. You don't instantiate certain classes using constructors (such as MonoBehaviours) becasue those are resources allocated in the unmanaged C++ side of the engine. MonoBehaviour are just C# wrappers for the relative components.
For everything whose lifetime and ownership is just managed by mono runtime, than you can use constructors.

So, basically it has to be a MonoBehaviour component, and attached manually in the editor?

A MonoBehaviour or ScriptableObject. Not necessary attached by hand. You can use CreateInstance and do it by code.


text:
Property replication vs RPC notifications
I'm not talking about something such as an object position, which changes very often. I'm talking about some object state that may be changed infrequently, such as a door opening and closing.
Would it be better to send an RPC to saying "The door is opening/closing" or would it be better to replicated a property that tells us the objects state? Obviously this can be done both ways, but what are the advantages/disadvantages of each?
As far as I know any competent implementation will only consider replication for properties that have changed. This means when the property does actually change, it should have the same cost( assuming it must be reliably replicated ) as sending an RPC. The other advantage to doing this would be removing the need for more complex RPC buffering. If a client connects, they get sent the state, no need to buffer/unbuffer things.
All I can see is advantages for replicating over RPCs in this case. However, I always see suggestions to have as few replicating properties as possible.

I think you've demonstrated above that, implemented well, both forms have very similar costs for this situation. It seems unlikely that the choice to property replicate vs buffer these infrequent state changes will be a major determining factor in the game's performance. Maybe there could be a significant advantage that's specific to the kind of game you're making (eg. if you have thousands of infrequently-changed state variables, and only a handful get changed in any one game session), but if there's no such case obvious to you right now then it may be premature optimization to fret over it.

That's what I've gathered from reading and a small amount of experience working with networking in Unity and Unreal 4. Obviously I'm not expert, so I'm looking for more information on the subject. Is there any other problems I might have with using property replication? Is it unreliable? Could a major state change be missed due to unreliability with property replication?

If you're using, for example, Unity's standard networking without an authoritative server, an RPC would be better because syncing something every frame can be VERY expensive because the packet header is some 20-40 bytes of bandwidth overhead in addition to the identification of the object being synced. You end up sending a lot more data than necessary.
This, compounded with the potential of having many doors, leads to a very straightforward decision. Generally, the less objects you need to constantly sync, the better.
Using an RPC does complicate some things, as you've mentioned (i.e. player joining later). Buffered RPC's would be overkill since you only need the last state change - not all of them. Instead, you can just send an RPC to the new player from the Master Client giving the game state. Your game can be semi-authoritative in that you delegate the responsibility of syncing 'everyone else', people who join later or get desynced, to a single client.
Useful Note: RPCs are reliable in Unity networking (not sure about Unreal) so you don't have to worry about your doors getting desynced as long as you send the message and the client is connected. If the client disconnects then comes back to the game, its sync should be handled by the Master Client.

This doesn't make sense, considering replicated properties are only synced when they've changed.

Oh, you were talking about delta compressed syncs? That wouldn't be too bad then, but there's always the chance that you won't get the UDP packets (assuming this syncing uses UDP), depending on how this compression is implemented. It's better to take the safe route and just reliably sync these objects since they're not frequently changed.

Apparently, all the modern AAA shooters use lockstep which calls for syncing input like "Move Left" rather than positions. This is difficult to do perfectly because of floating point accuracy errors and whatnot. You can, however, do something quite similar for less significant parts of our game. A lot of games like TF2 open doors if a player gets within a certain distance. I'm not sure if this can work in your game, but it would be a neat thing to automate door opening. Anyways, just a little idea that could be useful.

PS: RPCs in Unreal are unreliable (so good for sounds, particles etc what does not influence gameplay).

To correct myself: In UE4, RPCs now have to be flagged as unreliable or reliable.


text:
Entity interpolation with a Unity listen server
How can this be done? I've implemented client side interpolation but I'm at a loss on how to implement it on a listen server. In order for the listen server player to see the same thing as the clients, it needs to display it's past positions.
The problem with this is, as far as I know, there's no way to specify positions for rendering and positions for collision and everything else in Unity. transform.position is used for everything. Is there a way to draw in a different position?

I'm assuming you're referring to making an authoritative server? If so, only the server has the real positions of all players, and all remote players only see approximations (via proxy) of the world state. So in fact no one but the server really "sees" the exact positions of all the players, since network latency is in play.

If this is indeed the case, I suggest you go and read some materials online as this is a complex issue - some links: Great lecture by Bungie The famous Gaffer on Games blog Another great blog post


text:
Does the order in which RPCs are called match the order they are invoked locally?
For example:
void A()
{
    Debug.Log( "A was called." );
}
void B()
{
    Debug.Log( "B was called." );
}
void C()
{
    NetworkView view = GetComponent<NetworkView>();
    if( view != null )
    {
        view.RPC( "A", specificNetworkPlayer );
        view.RPC( "B", specificNetworkPlayer );
    }
}

Is A guaranteed  to be called before B on the "specificNetworkPlayer"'s machine? If not, what is the order, if any?

From the documentation:

RPC calls are always guaranteed to be executed in the same order as
  they are sent

So, yes, it's guaranteed to execute A then B as your code defines it.

Is there a slight caveat to this in the case of multiple senders? Eg. I send A B C at the same time as another player sends X Y Z. A third player might receive them in any order, like A B C X Y Z or A X B Y C Z or A X Y Z B C. So the relative order of RPCs from a single client remains identical, but does that guarantee extend to RPCs from multiple players?

It's true, the order is preserved from the send order. Luckily, I don't see a situation where you'd expect two different clients to send at the same time and expect the order to be preserved on a third client. It would be more reasonable to simply put a method locally that calls X();Y();Z(); instead of sending each as their own RPC.


text:
Overriding all object inspectors in Unity
How can this be done? I want to add a single label to every component( including non-misbehavior ones ) in the inspector. I've tried:
[CustomEditor( typeof( Object ) )]

And:
[CustomEditor( typeof( Component ) )]

This seems to work for all components that don't seem to have a custom editor already( like BoxCollider, and Transform ). It seems if a component has a custom editor already, it ignores the one I've defined. Even if I change the attribute to this:
[CustomEditor( typeof( Transform ) )]

There is still no change to the Transform inspector. How can I define my own custom editor for these components?

Have you tried making a custom editor for MonoBehaviour?

Yes. It didn't work. Transform, rigid body and other "built in" types do not inherit from mono behavior.

Using the CustomEditor attribute should certainly work for the Transform component.
I have my own custom editor for transforms to make resetting them a bit easier.
By creating the following CustomEditor you will notice that the localRotation changes from displaying three values that represent the Euler angles (x, y, z) of the rotation quaternion to the 4 values  (x, y, z, w) of the underlying rotation quaternion. This is because Unity is now using the CustomEditor which is empty and just shows the raw transform data structures.
[CustomEditor(typeof(Transform))]
public class TransformEditor : Editor
{
}

To implement your own custom inspector make sure you override OnInspectorGUI() and place the relevant code there.
As you ahve said you will need to follow this approach for each component that already has a default Custom Inspector set up.


text:
RPC events with client side interpolation
I've implemented client side interpolation, where a specified delay is put on each client. This allows them to interpolate between positions and rotations. Unfortunately, this means every thing else must be delayed, otherwise events ( like a particle effect on a collision ) will happen ahead of time( according to the interpolated data. )
Should I delay everything along with the interpolated data, or do I have the wrong idea about client side interpolation? I haven't seen a single mention of this problem anywhere, which makes me think I'm missing something.

Try it out! Don't delay RPCs yet, but design your system to be able to buffer them at a later point if you notice visual artifacts with the current implementation. Don't forget about sound effects. I would imagine that delay on sound effects is more noticeable than delay on visual effects.

Any reason why you are not using client-side prediction instead of adding delays?

The easiest way to implement interpolation correctly is by using fixed time steps. Basically, if you know you're going to get a new position about 10 times a second, you can interpolate from the last position to the current one in the time it takes to get a new position (.1 seconds).
In reality, there are things like jitter that make your job harder so packets won't always arrive .1 seconds apart if they're sent .1 seconds apart. This problem can be solved with some buffering. Instead of immediately accepting a position as the current position when you receive it, wait until the next expected sync frame to recognize it.
I.e.
Vector2 LastPos;
Vector2 CurrentPos;
Vector2 BufferPos

//When data is received, store it in the buffer
void OnReceiveData (byte[] RawData)
{
    BufferPos = DeserializationMagic (RawData);
}

//Runs every .1 seconds or however frequent this character gets synced
void FixedUpdate ()
{
    LastPos = CurrentPos;
    CurrentPos = BufferPos;
    ElapsedTime = 0;
    //Send out your position to everyone (except yourself)
    Networking.RPC (transform.position);
}

//Used for smooth interpolation
float ElapsedTime;

//Interpolate position every frame based on time since last position was set
void Update ()
{
    ElapsedTime += Time.deltaTime;
    //Lerps by a factor of 10 since interpolation needs to happen within .1 seconds
    transform.position = Vector3.Lerp (LastPos, CurrentPos, ElapsedTime * 10);
}

There are a bunch of things you can do with time stamps and whatnot for staggered syncing, but at 10 syncs per second, any advantages you get without an expected sync frame probably won't get noticed. Plus, this is the most straightforward and simple way to get smooth interpolation in my opinion.
I don't recommend using RPCs to sync objects though, since the RPC most likely is reliable. Reliable protocols involve sending an ACK packet which means another 20-40 bytes for the packet header. The sender could also resend the original packet if an ACK was not received in time.
Syncing unreliably should be fine since if 1 or 2 packets go missing, the next received packet will fill in the gap.

This has nothing to do with my question. I know how to interpolate between buffered positions. I'm wondering if this then requires me to delay every RPC and buffer every bit of replicated data so that things don't happen at the wrong time.

Look at the example code. There is a maximum delay of .1 seconds exclusive if you recognize the data every .1 seconds, since OnReceiveData can happen at any time.

According to this article: http://www.ncbi.nlm.nih.gov/pmc/articles/PMC2871325/, player response time peaks around 200 ms. They'll barely notice the 100ms delay, assuming the sync lags 100ms seconds every time (which is highly unlikely).

Plus, if you're not using an authoritative server so there's no reason why latency would be a particularly big deal. Simulate movement and whatnot locally so players get near-instant responses. Everyone else doesn't really matter.

What about explosions and other graphical effects that need to line up with the object positions? I'm not worried so much about delay in input, but about things lining up correctly. I can't delay part of the game, but not the other.

Graphics are completely artificial. The only things you should worry about that much are the gameplay aspects. If worst comes to worst, you can always hardcode exceptions to trigger graphical effects but you should focus on the game working before worrying about that. If you worry about graphics before mechanics, you'll end up with a lot more bugs to deal with at one time.

Buffered replication will make sure the game's mechanics will work okay.

Hey Ben, do you have anything else you're unsure about regarding client side replication?


text:
Does Steam provide persistent data storage to developers?
Does Steam provide persistent data storage (for custom data) to developers? For example, would an online shooter be able to store player statistics (kills, matches, player deaths, etc.) on Steam servers?

You could save all the data both locally on your player's computer, and sync it automatically to Steam Cloud, which indeed allows you to store stats/user preferences/progress/etc.. online.
Steam Cloud API :

header: ISteamRemoteStorage.h
Game settings, savegames, and other user-specific bits can
  be replicated to the Steam Cloud to provide the player with a
  continuous and hassle-free experience.

EDIT : You can also write/read everything directly into the servers. If you are worried about users tampering with data.
Steam Cloud documentation :

The Cloud functions all live in ISteamRemoteStorage.h. It is
  essentially a very simple filesystem API - your game reads and writes
  files directly to the API.
Aside from reads and writes, a few functions are provided to provide
  visibility into the files already in the Cloud "folder", and to query
  the amount of quota remaining.

Is there an authoritative way of storing data?

@Ben do you mean alternative to steam cloud api?

The API automatically syncs any files you need to use in order to keep the data. "Authoritative" depends on the code you used in your offline game. You can store data in an encrypted way to prevent modifications from user, plus the user can only modify the offline data, you might as well not use anything on the player's computer at all, and use the  files online. Anyways, instead of me listing out what you can/can't do, you can just read the Steam API doc about Cloud storage. EVERYTHING is detailed to the last scrap of code. partner.steamgames.com/documentation/cloud You just need to login.

For statistics you can use ISteamUserStats, which is a part of the Steamworks offering.
This interface allows you to define (from the Steamworks developer site) a set of tracked statistics with various properties, and can even auto-grant Steam achievements based on the stat values.
The interface supports multiple types of numerical data (integer, floating point, and moving average), access control (only allow trusted game servers to report changes), limits (both absolute and per-update), and automatic global aggregates.
Documentation can be found here: https://partner.steamgames.com/documentation/ugs
(Requires signing up for Steamworks, but doesn't require affiliation to read.)
ISteamUserStats statistics are stored to the Steam backend immediately, rather than requiring synchronization and conflict management, and do not have a fixed quota. Leaderboards can also be automatically built by Steam from the data.
ISteamRemoteStorage is designed for saving user files, such as game saves and configuration information, rather than statistical tracking.


text:
Android Density Independent Pixel Drawables Resource Folder
I'm trying to get to grips with DIP in Android (using Eclipse). I'm writing a little game with OpenGL ES 2.0. Could I get some guidance and tips on what I'm not understanding/doing right.
From what I understand I need to make different sizes of the same image so that they appear correctly on different devices. This is because each device has a different aspect ratio and a 1cm by 1cm sprite on one screen might not be 1cm by cm on another screen.
I read here that I need to place my sprite images in the res/drawables folders. I did this, but when I try to reference the resource using:
    mySpriteTexture = new Texture(context, R.mysprite);

the R class fails to find the .png. I have tried cleaning, rebuilding etc., and tried variations like R.res.mysprite. Although I don't see the point in trying R.res.drawables-XXX as according to the article Android is meant to pick the correct images on running...
I think it must be something to do with the XML manifest? But I have no clue. Any help would be welcome.

Use R.drawable.myprite. Android will choose the correct size image from those available in the drawable-XXX drawers.


text:
What is an acceptable receive rate for a client?
I've noticed that syncing a transform( in Unity, so a Vector3, and a Quaternion ) ends up being close to 1000 byes per second received on a client( send rate of 20hz ). Multiply this by a generous 20 entities a single match might have and that's about 15k-20k bytes per second.
Is this too much? What should I be aiming for? What data rates do games typically have?

Shawn Hargreaves wrote few slides for Xbox live gaming, concerning latencies, you can check the full presentation from here

Bandwidth -
  How much is available? Assume 64 kilobits (8 kilobytes) per second

0.5 Mbit/s ADSL has theoretical limit of 64kbs


text:
Get algorithm (Chance-to-Hit) from Input and Output
I am currently working on a chance-to-hit formula for an rpg. It consists of the following parts:

AttackSkill: The used Attribute of the Attacker (Range: 1-10)
AttackBonus: A Modifier from Skillbonuses (Range 0.5 - 3)
DefenseSkill: The used Attribute of the Defender (Range: 1-10)
DefenseFitness: How suitable is this defense against the attack? (Range: 0.1 - 5)
DefenseBonus: A Modifier from Skillbonuses (Range 0.5 - 3)

This is the Input. I am looking for a way to map this to the following Output:

Difficulty: How hard it is for the Attacker to hit (Range 0-10 float, later round to int)

What could I do to get the algorithm out of the value pairs? If possible, the output should be biased in direction of Difficulty 6 (like a normal distribution).

Well generally you would divide the attack power by the defense stats of the attacked player. But if he uses defense abilities you would add that too..

You are constructing a function from several inputs to meet a desired output. This function should factor the amount of each attribute in conjunction with the importance. Your use of value pairs for such a function also coheres excellently with a documented decision model. I believe that a weighted sum is in order.
The weighted sum model takes in each attribute's value and their weight and proceeds by multiplying the former by the latter. If an attribute carries great importance in the hit chance, then the weight should reflect this by carrying a higher value as well. For example, the AttackSkill attribute seems an important determinant, so it will be assigned a weight of 1.5. Conversely, AttackBonus impacts the positive chances much less, and is assigned 0.8. We are left with.
(AttackSkill*1.5)+(AttackBonus*0.8) = ATTACKPOWER

The same will be done on the defender's behalf accounting for an additional attribute.
(DefenseSkill*1.5)+(DefenseFitness*2.0)+(DefenseBonus*0.8) = DEFENSEPOWER

Weighted sum equations carry the advantage of modularity, allowing them to be scaled to any extent without breaking down. After the sums are obtained, they need to be referenced with each other to map these "forces" to a 0-10 chance. Here's a simple method.
We first measure the difference between the power of the attack and defense. If positive, the attack has 50% or greater of following through and vice versa. The difference is passed to a sigmoid function which presents a rather convenient chance curve.

The curve is mapped from 0-10 and voila.
(sigmoid(ATTACKPOWER-DEFENSEPOWER)+1.0)*5.0 = HITCHANCE

This may not be ideal, but I've found an answer on another Q/A-site.

Multiply all AttackerValues (lets call it AttackRating)
Multiply DefenderValues [Without DefenseFitness, see top] (lets call it DefenseRating)
Divide AttackRating with DefenseRating (lets call it Rating)
Divide Rating with DefenseFitness (lets call it RawDifficulty)

Raw Difficulty is a Value between 0,00333 and 600 (from the ranges in the question)
This value now needs to be mapped to the range of 0-10.

Create a table in Excel with RawDifficulty and the required Result, for instance:

600 (easiest) needs to be mapped to 0.1, 0,0033 (hardest) needs to be mapped to 9.9. Add some values in between into the table, 1 (medium) needs to be mapped to 5 for example.

Get the square root of RawDifficulty (this was to dimnish extreme values somewhat)
Create a point diagram within excel, add a trendline (in this case logarithmic) and show the formula. Bang!

In my case the following formula does work:
EndDifficulty = -1,6395Ln(SquareRoot(RawDifficulty)) + 5,0963

What I usually do is just add more defense and more damage to the enemy as the difficulty increase. Since I'm no mathemagician, I just pick how much the rating it'll increase by difficulty. For example, Add 1000 to defense and 500 to damage each time they increase the difficulty. Sorta how they used to do it on classic games. But, that's just me lol

Well thats something I want to avoid :)


text:
Why are all OpenGL function calls prefixed with "q" in the Doom 3 source code?
Why is every OpenGL call prefixed with a "q" in the Doom 3 source code? Are they custom functions or a different library, and if so, why use that over OpenGL? Any Google search comes up with the Qt OpenGL module, which I assume has nothing to do with it.

For reference - Doom3's qgl.h.
OpenGL is an odd little API. You must often work with function pointers either because the standard implementation is missing new functionality or because you want to work with extensions. For instance, if you want to call glCreateShader, but the OS you're developing on doesn't ship with new enough GL headers, you must use glGetProcAddress("CreateShader") in order to load the function out of the user's driver. And then you have to store that function pointer somewhere.
Even if your development environment has a particular function the users' machines might not. And then a certain function can come in 2-3 forms (vendor extension, ARB extension, core function) and so you might have to search for a function under several different names.
Many GL "loader" libraries (like GLEW) try to stick to the GL conventions. They create "fake" functions with the usual gl-prefix and try to magically make your code just work on any OS or driver that supports the necessary features. Essentially, instead of glFoo being an actual function, it's just a global function pointer and the loader library (during initialization or first use) searches FooEXT, FooARB, and Foo until it finds an implementation and then assigns that to the global pointer. The client code looks like it's just calling a regular function but it magically all gets resolved at runtime during initialization in a way that deals with all the different platforms and drivers out there.
Other libraries put the functions in namespaces or otherwise vary a bit from the GL norm. id wrote their own loader, either because they didn't like the ones that existed or - more likely - Quake just predates any other library that id could have used (the q in qgl likely stands for Quake, which was the first id Tech game to use GL).
The GL spec is written with this being a possibility. The spec is written without prefixes; it's the implementation of OpenGL that decides on which prefix to use (gl being the common one, of course). Loader libraries that put the functions in namespaces or using their own prefixes are completely valid.
Doom 3, being based on the Quake engine, is completely typical and justified in using a loader library to deal with the oddities of GL on multiple platforms and in choosing to use the qgl prefix.

+1, and kudos for spotting I've wrote id bad (you're completely right about the capitalization), though http://en.wikipedia.org/wiki/Id_Tech is still written with space, in all the sources I could find.


text:
CS:GO workshop awp skin scrambles
I was starting make my awp skin, but I was wondering that my skin at CS:GO looks bit different than on photoshop. So I came here for help. I will show just a test skin (fully red).
First I was save my skin with photoshop to .tga format (32 bits/pixel).

Then importing to VTF editor, but not changing any of these settings, they are default.

After that I checked only "No Level Of Detail", "No Mipmap" and "SRGB".

Opening CS:GO and workshop, awp is not fully red, there is some scrambles.

I hope for solution. :)

I don't think this question is suited for this website. It is not about game development and it is way too localized

Mod questions are allowed, though they typically don't do well.

This question was from here.

From the looks of it, you have some other texture layer that's adding "wear and tear" to your model (for example the corners of the ammo cartridge). You should poke around for decals or other layers being applied to the model.
I'd be particularly interested in the slider at the bottom:


text:
How can an asset bundle's resources be destroyed after destroying the compressed bundle data?
Using:
AssetBundle.Unload( false );

Will clear the compressed asset bundle data. This, unfortunately, destroys the bundle object as well, which means nothing is keeping track of the loaded resources.
Using:
AssetBundle.Unload( true );

Will clear all resources correctly. However, it may be desirable to load the required resources, free the compressed data, and then free the loaded resources at a later time. How can this be done?

Unity Documentation: AssetBundle.Unload
When unloadAllLoadedObjects is false, compressed file data for assets inside the bundle will be unloaded, but any actual objects already loaded from this bundle will be kept intact. Of course you won't be able to load any more objects from this bundle.
When unloadAllLoadedObjects is true, all objects that were loaded from this bundle will be destroyed as well. If there are game objects in your scene referencing those assets, the references to them will become missing.

In your case, you want to use
AssetBundle.Unload(false)

This will destroy the loaded assets, but keep intact the already created GameObjects.
These being just GameObjects, you can destroy them at any later time using Object.Destroy.
public static void Destroy(Object obj, float t = 0.0F);

PS: You should take a look at Resources.UnloadUnusedAssets too.
As the function name implies, it Unloads assets that are not used.
An asset is deemed to be unused if it isn't reached after walking the whole game object hierarchy, including script components. Static variables are also examined.

But the loaded object is a prefab, right? When will this be destroyed? If I load a prefab from an asset bundle, instantiate the prefab, then call AssetBundle.Unload( true ), the instantiated object is missing all it's resource references. If I instantiate the loaded prefab and then call AssetBundle.Unload( false ), followed by using DestroyImmediate on the loaded prefab, the instantiated object is completely in tact. This seems to imply using destroy on the loaded object does not free the resources it uses( meshes, textures, etc )

Those will eventually be cleaned up when Unity decides to do it's own Garbage Collecting. However, it can be forced programmatically too. Have a look at: http://docs.unity3d.com/ScriptReference/Resources.UnloadUnusedAssets.html

But the assets are in use, on the scene object that was instantiated. So, Resources.UnloadUnusedAssets does not destroy them in this case, but AssetBundle.Unload( true ) does.

You're losing me Ben. Why do you want to destroy something that you want to remain in use? Resources.UnloadUnusedAssets will clear up memory after the GameObjects referencing those assets get destroyed.

It would be nice to have something be explicitly destroyed so entire assets can't be accidentally left in memory due to a single reference. But you're right, this will work, it's just a bit more fragile.


text:
Is it possible to load asset bundle dependencies manually in Unity 5.0?
Is it possible to tell Unity to not load asset bundle dependencies automatically so they can be loaded manually. If so, how can this be done?

The asset bundle dependencies are not processed in the "load", but when the asset bundle is created. Unity basically loads the entire asset bundle in at at a time, and then instantiates particular assets inside the asset bundle. To remove asset bundle dependencies at build time, take a look at the AssetBundle.PushAssetDependencies function. Any assets that are pushed into the dependency list will be automatically excluded by bundles built afterwards.


text:
MonoGame deltaTime isn't the same in Draw function as in Update function
In the Update function gameTime.elapsedGameTime.TotalSeconds always gives me 0.01666666667 which is the deltaTime value for 60 FPS, no matter if the FPS is 30 it always shows me 0.01666666667 but the Draw function on the other hand shows 0.03333333333 if the FPS is 30.
Is this a bug or is this how it is meant to be? Should I just create a variable called deltaTime and make it equal to gameTime.elapsedGameTime.TotalSeconds in the Draw function? Maybe this is a problem on Macs only
(I'm on a mac and I'm using Xamarin Studio MonoMac to build the project)

Shiro my mac can handle 60FPS the 30 fps thing was just an example

Shiro I understand now, Thank you. Just post that as an answer and I'll accept that answer

This tutorial explains it in a bit more detail: http://www.gamefromscratch.com/post/2015/06/15/MonoGame-Tutorial-Creating-an-Application.aspx

There is no need for the Update() to be called as frequent as Draw(). Your Draw() method is generally called the same or less times than your Update(), because games are usually not CPU intense but rather GPU. So locking on 30FPS is normal (or even 15 or 7,5). However, there are probably problems with your code's efficiency, or your mac hardware is really bad, to not be able to handle 60FPS.
The reason that Update() being called more times than Draw() is acceptable in games, is because even though the visuals doesn't feel smooth, the game still runs at a higher frame rate, so the input you give, gets processed inside your game. So, it feels like the gameplay is the same, but you just don't see as many frames as you would hope to see.
This is why you need to keep in mind that Draw() should not change your game's variables or game's logic. This should only be done in the Update() method.


text:
How does Unity load asset bundle dependencies and how can they be accessed?
Loading assets from an asset bundle is done explicitly. You give the bundle the name of the asset to load, then it returns it( either now or later. ) However, loading dependencies is done completely automatically.
How is this done? Is the asset bundle completely removed from memory after the required objects are loaded? Are all the bundle's assets loaded, or just the necessary ones? How can these assets be accessed outside of the reference that caused the dependency in the first place?
I ask this because I am trying to collect some type information about the assets loaded in asset bundles. I need to be aware of all assets of a certain type that are loaded into the game.

The asset bundle system does not automatically remove anything nor does it load dependencies automatically. Dependencies are included in asset bundles automatically, but actually exist in each asset bundle that depends on them, unless you manually removed them. For example, if you have a 2 different player models, Hero1 and Hero2. They both share a Gun texture. If you put hero1 into an asset bundle, that asset bundle will contain Hero1 and the Gun Texture. If you put hero2 into an asset bundle, it will contain Hero2 and the Gun Texture. If you load both asset bundles simultaneously you will have 2 copies of the gun texture loaded. The way to get around this is with the asset bundle dependencies. The way these work is that at build time, you would create an asset bundle with the Gun texture, and then call AssetBundle.PushAssetDependencies(), passing it the gun texture. You can then build the Hero1 and Hero2 asset bundles, and each of them will not include the Gun texture. At runtime, it will be up to you to load the Gun texture bundle along with the Hero1 and/or Hero2 asset bundles, but it will be only loaded a single time.
The memory management of asset bundles gets a bit more complex than this too, as far as memory usage is concerned. If you have a compressed asset bundle, what will happen when you load it is the entire compressed asset bundle is loaded into memory. From there, individual assets are decompressed and loaded. Once all the assets have been loaded, you can unload the asset bundle and you'll still have the assets inside in memory (assuming something is referencing them). The downside of this approach is that at some point, you'll have both the entire compressed asset bundle in memory, and simultaneously the uncompressed asset bundles in memory as well. With the new parameter to AssetBundle.Unload, you can specify whether you want it to destroy only the compressed asset bundle or whether you want it to free the uncompressed copy as well.
The other way to generate asset bundles is to generate them as uncompressed asset bundles. If you generate uncompressed asset bundles, they will be larger, which will negatively affect your download time in getting the assets, but your loads will be much faster. When you load an uncompressed asset bundle, there isn't a second copy of all of your data, the objects that reference data in your asset bundle are pointed to the actual asset bundle in memory. When you unload the asset bundle, anything pointing to things inside the asset bundle are unloaded, and so you can't unload the asset bundle while still referencing things inside it. The upside is that you avoid the extra memory of having both loaded, and you don't have the performance hit that is incurred by the decompression, since its loaded in place.

With uncompressed asset bundles, when you load the asset bundle, are all assets in the bundle loaded? Is there any way to load only select assets from the bundle?

Not for the purposes of saving memory, no, the entire bundle gets loaded into memory. This information may be out of date, as I did this investigation a couple years ago, but in the case of a compressed bundle, the entire compressed bundle gets loaded into memory. It then allocates memory for the uncompressed version of it, and decompresses it into that memory, for a total memory usage that is greater than the size of the compressed bundle. If you use an uncompressed bundle, it loads it into memory directly, so you only have the size of the uncompressed bundle to deal with.

It turns out if you open an uncompressed asset bundle without using WWW, it streams it as you ask for assets.


text:
Is there a way to build asset bundle that exclude texture2d from an image?
In unity 4.x, I can archive this because I can select my resource type directly as Sprite variable before putting it into the bundle via BuildAssetBundle. However, in Unity 5.x, I need to put the name in the Editor can I cannot choose my resource type as Sprite only in the Editor. When I make the bundle, bundle will have both Texture2D and Sprite which I only want Sprite to save the memory and disk space. The bundle will exclude Texture2D only if I create the bundle from Prefab not the image itself but I want only the raw image. What should I do?


text:
LWJGL: loading textures of various types
I googled around a bit and nobody seems to have asked this question.
I have images in multiple color formats (all of them are PNGs).  Most of them are ARGB but my bitmap fonts are gray scale, and I would like them to stay that way.  All I want to do is find out what format BufferedImage uses to store my pixel data and then use that information with glTexImage2D.  Java, in all its wisdom, seems to be determined to hide that information from me at all costs...
I also need to know how BufferedImage aligns its pixel data in both of these formats (glTexImage2D cares).
Could someone please tell me how to:

Determine the pixel format of my BufferedImage.  If it is ARGB32, I'm going to have to reorder the bytes and use GL_RGBA.  If it's grayscale, I will be using GL_INTENSITY.
Extract the actual bytes from the image.  I have seen a few examples on the web that use BufferedImage.getRaster().getDataBuffer().  This is nonsensical.  Why are there different types of buffers like DataBufferInt?  Because of Java's strong typing, I need DataBufferByte.  If this is the only way, could somebody give me specific directions to use the different type of buffers with glTexImage2D?
Figure out how the aforementioned image data is aligned.  I will use this information with glPixelStorei.

In addition, I come from C and C++ programming.  In C this was 100 lines of simple libPNG and GL calls.  Should I expect more trouble like this in the future?

It seems that BufferedImage does not keep that kind of information from the original PNG file. The type of the BufferedImage may or may not correlate with the type of the PNG.
You can check the type of the BufferedImage with BufferedImage.getType(), which may help you, but the implementation of image loading is free to convert the images to other formats at will.
As you are comparing to C/C++ and libpng, I'll offer you an alternative solution. Use a similar low level library in Java as libpng. PNGJ: Java library for PNG encoding looks very promising. This gives you access to the actual PNG data instead of loading them to BufferedImages.

In addition, I come from C and C++ programming. In C this was 100 lines of simple libPNG and GL calls. Should I expect more trouble like this in the future?

Yes, you should always expect trouble when expanding to unfamiliar areas. And you will learn a lot when you do encounter those. Respectively a Java developer who has used to passing BufferedImages always as just RGBA data to OpenGL would be annoyed for needing to write 100 lines of low level code for that.
From my own experience I can tell that the biggest issue when moving from C++/OpenGL to LWJGL was the need to use java.nio buffers instead of arrays. See https://github.com/LWJGL/lwjgl3-wiki/wiki/1.5.-Bindings-FAQ for more information. Once I learned that, the transition was pretty straightforward.

I can create a BufferedImage using BufferedImage.TYPE_BYTE_GRAY and write to it using Graphics2D, and when I save that image with ImageIO, it is saved in gray scale format.  Are you saying that there is no analogue for reading images in this fashion?

The format of BufferedImages more or less correlates with the created or loaded PNG files, but there is no guarantee about that and there is not much control over it. You can try using BufferedImage.getType() and it may work for you and even for everyone. But it definitely cannot tell the difference between 1, 2, 4 or 8 bit grayscale PNGs.


text:
USB NES controller
I just recently bought the Retrolink USB NES Controller.
I would really like to make simple platformer games (for PC - engine/ frame/ library doesn't matter) for this pretty popular retro controller. But I can't find a website associated with the company where I could find either dev tool/ sdks or a library of existing games.
Ever since I bought this controller I did nothing, but testing the buttons. - I didn't even find a game that supports it 100% and I don't own an NES.

Are you on Windows? I think Windows will present that controller as  a joystick, you should be able to read it's state with standard input libraries.

I figured some unity games work.
They apparently detect it as an input device with vertical and horizontal axis as well as four buttons.
My question remains..do they have a website - who are these people that designed it. I don't feel right about this product.

@bornander On GNU/Linux, SDL is a safe bet.

As others have explained, controllers (especially simple controllers like these) are supposed to work out of the box, with the standard input libraries of your operating system (DirectInput in Windows).
It seems like you're interested in knowing who makes these, so I tried a bit of google, and found that "RetroLink" is a registered trademark of a USA company called "Kool Brands LLC" registered in Reno, Nevada.
From what I could see in a low resolution scan of the back of another controller of the same brand, the bottom part barely reads "???????? and RetroLink ????? are trademarks of Kool Brands LLC", which corroborates this information.
They also seem to hold trademarks for "GameBully", "Robokit", "Exo-Guard", "Buck-Shot", "Retro gen" and "Sumoto". All game controller related.
They don't seem to have a website, but you can find more information about this company if you google them.
From what I see, they seem to be a tiny operation, probably solely a trademark holding company, and the RetroLink products are very likely rebranded generic chinese products.
I don't think they will have SDKs or other information like those you're looking for.
Googling a bit further, it seems like Kool Brands LLC is deeply involved with another company, which does have a website, called Innex. They sell RetroLink as well as other brands. If you have specific questions about the RetroLink products, they may be able to help you.


text:
Unity Licensing
I'm sorry if this gets asked a lot but google gives up answers from back in 2008 and all sorts so  could somebody please clear up a couple of questions for me please.
I want to make 2D games in Unity, I've made my first one following a tutorial online with Unity free version and I don't see that I will really need any more advanced features than what's available in there.
However, I do want to release to both Android and iOS. When I finished making the game it already lets me build to an APK which I can run on my phone and tablet so my question is really do I need the pro licenses?
Also I'm considering at some point in the future I may want to include paid adverts in my game, can this still be done with free versions?

Unity 5 gives you all engine features for free. There's nothing you shouldn't be able to do with it that you can do with the paid version. Unity 5 does not require separate licenses for those mobile platforms. Yes, you can have ads in your game with the free version. The biggest 'problem' you may have with the free version is that it must include a "Unity personal edition" splash screen.

About your needs of the pro version: you're the only one in position to determine if you'll need the features or not. And about the licensing: how about reading the license?

I'm voting to close this question as off-topic because it is about the details of a software license, best answered by that software license or its writers.

You don't need the pro license to release games for any platform. You just have to upgrade if you made more than 100k in funding or revenue.
I believe paid advertisements can be done with the free version.
https://unity3d.com/unity/faq

Thank you for the fast reply. For arguments sake, lets say I did want to use the pro license, do I have to then buy iOS and Android pro too?If not, what are they actually for?

@WinterSoldier5 yes, you would.


text:
Applicaton.ExternalEval(String script)

I was wondering, if Application.ExternalEval(String script); in Unity3D can return something.
Like:String playerName = Application.ExternalEval("prompt("Please enter your name", "player1")");
Thank you in advance!

I don't think . But you can try to call a javacript like this (using Application.ExternalEval(.. ):
<script type="text/javascript" language="javascript">
<!--
//initializing the WebPlayer
var u = new UnityObject2();
u.initPlugin(jQuery("#unityPlayer")[0], "Example.unity3d");

function SaySomethingToUnity()
{
    u.getUnity().SendMessage("MyObject", "MyFunction", "Hello from a web page!");
}
-->
</script>

who sends a message back to Unity..
check this and let me know..

So theoretically I could use document.getElementById("someId").innerHTML += <script>..</script>; to add a method to the document and then call it using Application.ExternalCall("MyFunction1"); the method would respond using u.getUnity().SendMessage("MyObject", "MyFunction", "Hello from a web page!"); - right?

Disclaimer. My first intention was to update existing answer, but then I understood that I totally rewrote it. That's why I am creating new one.

Yes, it's possible. The communication is possible between the browser and unity in both directions. In the unity documentation this topic is well described. So it's possible to create a general function that will allow you to call javascript code from c# and get the result of it's evaluation in c#. Just remember that it will work only in WebPlayer or WebGL, but not in Editor, since Application.ExternalCall and Application.ExternalEval will just log to the editor console in Editor.
public static void Evaluate(string js, System.Action<string> callback);

So actually you need to use callbacks here. The only catch is to store those callbacks somehow. You can do a dirty trick by using and id for them. And storing callbacks in a dictionary where the key is id. And attach this id to js string, so in javascript you extract id, evaluate js and then send result with attached id. And then in the c# you again extract id and get callback by id from dictionary of callbacks and call this callback on the result. Sounds creepy. So it's simple implementation will look like
public class JSEvaluator : MonoBehaviour {

    private int id;
    Dictionary<int, Action<string>> callbacks;

    public static void Evaluate(string js, Action<string> callback) {
        ++id;
        callbacks[id] = callback;
        // ':' is separator to know where id ends
        Application.ExternalCall("evaluate", id.ToString() + ":" + js);
    }

    private void OnJSResult(string result) {
        var stringSeparators = new string[] {":"};
        var res = source.Split(stringSeparators, StringSplitOptions.None);
        var i = Convert.ToInt32(res[0]);
        var cb = callbacks[i];
        var rem = res[0] + ":";
        cb(result.Remove(result.IndexOf(rem), rem.Length););
        callbacks.Remove(i);
    }

}

Several catches here. First, we increase id. For every Evaluate call. And if you make a lot of them. At some point you might experience strange results. By a lot of I mean more than 4294967296. Also we don't check of result for having id.
Also important thing here. OnJSResult is not static. You can't call static methods from javascript. Due to how SendMessage works. Also, JSEvaluator must be MonoBehaviour.
In javascript it would be something like.
function evaluate(input) {
    var inp = input.split(":");
    var id = parseInt(inp[0]);
    inp.shift();
    var arg = inp.join(":");
    var result = eval(arg);
    // for UnityPlayer you need to u.getUnity().SendMessage
    SendMessage("GameObjectName", "OnJSResult", result);
}

Just don't forget to create an object named GameObjectName on the scene and attach to it JSEvaluator. I think that's pretty much it. Just remember that in general this thing is not very great. But I shown you an example of communication.


text:
Monogame / XNA - Multple 2D Cameras (Rectangle) on screen
I recently started building a simple 2D roguelike game using Monogame 3.4. I was keen on building the game "engine" myself as I saw it as a fun learning curve. Monogame was pretty much the best option for me to get started as I'm a C# dev by profession.
After getting my sprite to run around on the screen, I figured I probably need a proper level to continue with the more interesting stuff. So I decided to build a level builder for the game.
I've been struggling and digging around tutorials regarding building a 2D Camera.
I used this camera tutorial and I preferred this specific implementation.
My only problem is, currently, I have two Rectangles (Cameras) on my screen.

The Rectangle on the left represents a scrollable panel with all the available sprites but is also a sort of overlay. Underneath it, the level "map" is drawn.
My question is simply, how would I enable my level Rectangle to scroll past the overlay to view the left-hand side of it, which is currently being obscured by the panel.
Relevant code files are here
I would appreciate all the help I can get.
Thanks!

Managed to fix this by using Viewports when drawing the Rectangles / Cameras
        GraphicsDevice.Viewport = new Viewport(_level);

        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, _camera.ViewMatrix);
        DrawLevelTiles();
        spriteBatch.End();

        GraphicsDevice.Viewport = new Viewport(_panel);

        // draw panel
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, null, null, null, null);
        _spritePanel.Draw(spriteBatch);
        spriteBatch.End();

        // draw tiles to panel
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp, null, null, null, _panelCamera.ViewMatrix);
        DrawTiles();
        spriteBatch.End();


text:
Color key or transparent images?
Ive seen a lot of people make sprite sheets magenta instead of transparent and then they make the magenta transparent using code. What is best (Performance) to use transparent images or use a color (example : magenta) instead of transparency?

I could give a more helpful code sample with my answer if you give more details- what kind of graphics are you going for? How many different levels of transparency do you need?

It depends entirely on your needs: using transparency keys, you usually only have fully-transparent or fully-opaque colors*; using an alpha component, you can have 255 different levels of opacity. Do you want transparent smoke and flames for your explosion sprites, or just Super-Mario-Fireball style ones, with all-or-nothing opacity? It depends on your application's needs.
*Unless you use multiple transparency keys to represent different levels of opaqueness. For example, your program could treat magenta pixels as fully transparent; pixels with odd-numbered R-components as half transparent; and pixels with prime-number G components as 3/4 transparent. Whether that's a more memory-efficient option requires a lot of case-specific math to determine, but it's pretty much always more complicated than just using an alpha component in the source content, and it can really limit the colors available to you if you use too many color keys.
(Please comment if you'd like a code sample and/or images- I'm on mobile right now.)

It is quite the same thing. When rendering occurs, the program can compute opaque and transparent pixels to be drawn for color interpolation on the screen. The only difference on the two techniques you mentioned is that trasparent images carry transparency information on their own, and color key gives transparency information to an image from within the program, but when the engine draws on the screen it still checks for what to draw and what not to draw, every frame.

There are some important differences. Alpha transparency requires a separate colour channel, and so it can increase the size of the texture on disc & in memory by about 33%. But it's also natively supported by GPU texture compression formats, so if your textures are amenable to compression this can often be just as efficient or even moreso than storing an image with palette transparency.

@DMGregory: More generally, in some systems the fastest graphic formats to work with may use color-keyed transparency, while on others alpha-keyed transparency might be better.  Historically, some systems had hardware support only for color keying, and some embedded systems (typically those with 16-color or 256-color hardware) still do, but most systems today support alpha channels.

@supercat, can you give some examples of where color-keying has hardware support? I read a few good articles on that years ago, and some references would be a great refresher.

@MichaelHoffmann: Some TFT-LCD controllers of the style used in cell phones have a color-keyed mode; I can't think of any I've seen with an alpha-channel mode; when clocking out 16 bits per pixel, they always use the extra bit for green.  Display hardware with fewer than 16 bits per pixel almost always uses a reserved color for transparency if it supports it at all.  I find it somewhat curious, actually, that transparency support isn't more common in TFT-LCD controllers, since nearly all of them allow an arbitrary rectangular area to be written in any combination of row-major, column-major,...

...row increasing/decreasing, and column increasing/decreasing, without having to reload coordinates during the process (very useful for DMA) but on devices without transparency support the value of that ability is limited unless the application only has need for opaque rectangles.


text:
What does it actually mean by World, Window and Viewport?
I am studying Computer Graphics myself. I am stuck with the concept of World, Window and View-port.
As far as I understood:
(1) World - is an abstract and imaginary space that is imagined to exist inside the Graphics rendering software (like Blender, 3D Studio Max, and so on) or a framework/library/engine like OpenGL or Directx, or, Unreal Engine where the objects/models (to be displayed) are measured in units specific to that software or library or engine. We are generally habituated to measure pictures of all geometric objects as the units of integer/real numbers on papers or books. And, in computer monitor screens they are measured in Pixels. But, due to some problems like ________________, developers use a coordinate system imagined and implemented in their software/framework/library/ engine and that is called a World Coordinate system. For example, OpenGL uses fractional values to measure and plot an object which seems to be totally impossible, because, computer screens do not have such abilities, they only understand pixels. So what is the catch? Before displaying on the computer screen, the OpenGL's world coordinate measurements are converted to actual screen pixels (through a mechanism called Graphics Pipeline) and that is how a model or object can be displayed on the computer screen.
(2) Window - Window is the a cut-section area (that we want to display on the computer screen) of the imaginary graphics World.
(3) Viewport - Viewport is the actual image we see on the screen. That is displayed by converting the world coordinate into screen pixels.
Am I correct?
Please, fill in the blank and review my conception thus far.

And, if you do down-vote, or, closed-vote, please, leave a comment so that I can understand the reason.

The world coordinates are the ones where you model your simulated objects. You can use meters if this makes sense to your needs. I'd model everything as a "real world" size, in meters, for instance. There is nothing to fill the blank. You could use meters, feet, kilometres, miles, baby steps, AU, or invent an askkekdksl length standard. This really depends on the implementation of what you're simulating, and could depend on the context. The view and projection matrices take care to transform the world items to screen coordinates.

Consider the real world. We have all kinds of things measured in meters and feet. Now how many, say, meters across is your field of view? Clearly, you need to know more about that question to continue. At what distance? Horizontally? Vertically? Even the arrangement of cells on your retina doesn't indicate how much of the world you can see.
The same problem exists in projection. We have some 3D surfaces in an imaginary world (the unit of its coordinates is just a matter of interpretation - we use different units depending on the scale we're simulating), and we want to project them onto a 2D surface that we will then display on the monitor.
So, we define a 3D surface with some particular size in the world on which to project things onto (in practice the surface is only implied by the matrix) and then scale and translate the 3D vertices so they lie in line with that surface. Imagine drawing a line from every vertex through the surface to some point, then moving them to the point where the intersect with the surface. That is the duty of the projection matrix.
Now that all the 3D stuff is 2D, we just have to scale the surface to the pixel resolution and map the textures' coordinates onto it.
This is a very simplified view, since there's a lot of tweaks like filtering and z-buffering that usually need to happen in between, but that's the problem and a short explanation of the solution.

are my conceptions about Window and Viewport correct?

While trying to understand the subject I found this great book. It's a dense and complete cheat sheet for all spaces you will possibly deal in 3D. Answering your question:

"The purpose of world space is to provide some absolute reference for all the objects in your scene."
No, Window is usually a synonym to Viewport. Possibly that you talking about is a clip space.
That is somewhat correct, "converting the world coordinate into screen pixels" doesn't sound particularly right because there is no direct transformation, unless you have at least a view point.

I think my basic idea will be helpful here: i found from different material during my study.
A rectangular area specified in world coordinates is called a window.
A rectangular area on the display device to which a window is mapped is called a view port.
The window defines what is to be viewed; the view port defines where it is to be displayed.
Often windows and view ports are rectangles in standard position with rectangle edges parallel to coordinate axes
 The mapping of a part of world coordinate scene to device coordinate is referred to as viewing transformation.   hope this is useful.


text:
Texture artifacts depending on texture size
I get some strange artifacting with textures depending on their size. I run OpenGL 3.3 with an GTX 580 so it should definitely support non power of two textures. I've narrowed down the problem specifically to the texture's size, I've tried checking if transparency had anything to do with it, color channels etc.

As you can see, the 512x512 and 300x300 textures look just fine but the 437x437 is all distorted. What could be the cause of this and how can it be fixed? I could of course just stick to power of two textures which seem to work fine but since this is an personal educational project I really want to understand what's going on.

Do you have mipmapping on or off?

Mipmapping is off (or I haven't make a conscious decision to enable it). I just upload the texture using glTexImage2D and the sampler I'm using is set to use GL_LINEAR for GL_TEXTURE_MIN_FILTER and GL_TEXTURE_MAG_FILTER.

Looks like you forgot to glPixelStorei(GL_UNPACK_ALIGNMENT, 1)


text:
Why do we need a fourth coordinate to divide by z?
I read the response here:
What does the graphics card do with the fourth element of a vector as the final position?

"The fourth component is a trick to keep track of perspective
  projection. When you do a perspective projection, you want to divide
  by z: x' = x/z, y' = y/z, but this isn't an operation that can be
  implemented by a 3x3 matrix operating on a vector of x, y, z. The
  trick that has become standard for doing this is to append a fourth
  coordinate, w, and declare that x, y, z will always be divided by w
  after all transformations are applied and before rasterization. "

but I didn't understand why couldn't we divide by z using a 3x3 matrix?
can't we just multiply by
1/z 0 0
0 1/z 0
0 0 1/z

to get [x/z y/z 1]
?

Try to express a transformation (or composition of transformations) that includes translation somewhere in the chain. Without a w value, you can't express it in a single matrix.

I do understand the translation part but I just didn't understand how adding a fourth coordinate will help or is a a trick to divide by z

For what it's worth you totally can do what you said. Dividing x and y by z is a valid method for converting from 3d coordinates to a 2d screen space with projection where distant objects get smaller. The w is a homogeneous coordinate to take it up to the fourth dimension to be able to do translation.

Because if you only divide [x, y, z] by z you get [x/z, y/z, 1] and you lost the actual value of z, which is actually useful if you want to do near/far plane clipping or fill a Z-buffer.
The best way to keep some information about z, at least on the GPU, is therefore to use 4 components instead of 3. In practice, what is actually in the last two vector components before the perspective division depends on what kind of projection and effects you want.
For instance, in the case of a perspective projection, this is the resulting 4-component vector:
| a 0 0 0 |   | x |   |   ax   |
| 0 b 0 0 |   | y |   |   by   |
| 0 0 c d | × | z | = | cz + d |
| 0 0 1 0 |   | 1 |   |    z   |

After perspective divide the vector becomes:
|  ax/z   |
|  by/z   |
| c + d/z |
|    1    |

And the c + d/z part leaves us with enough information to fill the Z buffer.

You could divide only the X and Y by Z, yielding [x/z, y/z, z]. The GPU doesn't have to do vector division, it could have been designed to do any calculation.

Technically, you could do that. But why bother? By the time you have that final z, you could either:

construct a 3x3 matrix as you described, wasting 9 * sizeof(float) bytes of space, spending cycles to compute 1/z (one division) and then doing nine multiplies and six adds to get your final vertex, or
you can do three divisions, as the modern pipeline currently does

One of these seems far more optimal to me, and it's not the first one. Even if optimized hardware exists for the matrix multiply, as it most certainly does, it's still conceptually more complex than a simple division.
Plus, a 3x3 matrix cannot encode a translation, and so a 4x4 matrix (and thus the fourth w coordinate) is used earlier in the pipeline anyway. This means you already have that fourth component sitting there so you may as well use it to transport a useful value and do your division with it.


text:
How to deal with a many to many relationship between objects
In my game, I keep a list of all clients currently connected to the server and a list of all actors in the game. Each actor is "relevant" ( currently replicating ) to 0 or many clients, and each client has has 0 or many actors that are relevant to it.
In some situations, I want to do something like this:
NetworkSystem.AddMulticastRPC( rpcCall, actor.AllRelevantClients );

This would send an RPC to all clients who have this actor as relevant.
Sometimes I need to do things like this:
foreach( var actor in client.RelevantActors )
{
    ...
}

Looping through all relevant actors.
Storing a collection on both the client and actor objects would be a duplication of data. Storing a collection on only one of these leads to some complex searches.
Is there an approach to dealing with data like this that avoids both of those problems?

I didn't understand the second problem, only the duplication part. Can you make it clear, please?

The space/time tradeoff is inevitable, so there's no way around it unless you tell us more about your use case. For example, what determines if an actor is "relevant" for a client, or vice versa?

Plese don't consider it as an anwser yet, for good or bad, I just need some place to tell about half of the problem (duplication)
I think you need to choose one of them to be the central point, either actor or client (I don't know how to use RPC to tell you how the API would work).
Suppose you choose that an Actors have many clients
foreach(var client in actor.AllRelevantClients)
{
    actor.doRPC(client);//work out with true RPC inside a function
}

and
forech(var client in actor.AllRelevantClients)
{
    actor.foo(client);//or client.foo(actor);
}

Invert the types to invert which class is the central point.
What is being referenced could only points to the central point when added
class Actor
{
    List<Client> _clients = new List<Client>;
    public void Add(Client client)
    {
        _clients.add(client);
        _clients.actor = this;
    }
}