Search Unity

Managing name collisions for similar objects

Discussion in 'Scripting' started by gdbjohnson, May 5, 2015.

  1. gdbjohnson

    gdbjohnson

    Joined:
    Dec 16, 2014
    Posts:
    40
    I tried scanning the forums on this, but it's a hard one to easily search for. This is more of a best-practice question, but I'm sure every developer faces this challenge in some way.

    With creating a game in Unity, I am finding that I'm writing lots of small scripts for objects, and hence I have a lot of classes with similar names. For instance, I have several buttons labelled "Play", and depending on context, will perform a slightly different function. Currently, on three difference "screens", I have three buttons named "PlayButton", which is causing a name collision.

    What do most people do to manage this?
    - Namespaces?
    - Naming classes uniquely? ie: PlayButton1, PlayButton2, PlayButton3?
    - Other ideas?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,536
    I use namespaces combined with unique names.

    So like I might have a namespace 'com.apoc.Entities.Player', and in that namespace a few classes. Some of which use the Player name just for ease of naming... others not so much.

    with unique names:
    PlayerEntity - all my entities have an entity component attached to them that represents the collection of gameobjects that is this entity. The player has a special version of this that inherits from the more generic 'Entity' class.
    PlayerBrain - glues the user input/interactions with the various components that move the player

    with more generalized names
    PlatformingMovementStyle - handles moving the player around screen in the platforming movement style (unlike swimming, so on)
    GravityResolver - a component for gravity. This is useful cause the player may have several movement style, but gravity doesn't really differ from style to style (of course each style can opt to use the grav or not, or to even modify the grav... but with out the need of copy-pasting the code everywhere)

    namespace_folder_layout.png
    Here you can see my organization, naming, etc on a project I'm working on today (it'll be up on the web in the next 2 days).

    The namespaces mirror the folders. Like ManateeAnimator would be:

    com.pepper.Entities.Actors.Manatee.ManateeAnimator
     
    gdbjohnson likes this.