Giter VIP home page Giter VIP logo

safe-refs's Introduction

When components keep references to other components, it's easy to end up with null pointer problems. SafeRefs are a simple way to always know whether a component reference is valid.

Install

From your unity project folder:

npm init --force # only if you don't yet have a package.json file
npm install --save beatthat/safe-refs

The package and all its dependencies will be installed under Assets/Plugins/packages.

In case it helps, a quick video of the above: https://youtu.be/Uss_yOiLNw8

Usage

An instance of SafeRef.value will be null if the component or its GameObject has been destroyed.

MyComponent someComponent = GetComponentsInChildren<MyComponent>();

var ref = new SafeRef<MyComponent>(someComponent);

// ref.value will return someComponent
Destroy(someComponent.gameObject);
// ref.value will return null

...SafeRefs are structs to handle them accordingly

public class HandlesStructCorrectly
{
  public void SetComponent(MyComponent c) {
    m_ref = new SafeRef<MyComponent>(c);
  }
  private SafeRef<MyComponent> m_ref;
}

public class HandlesStructIncorrectly
{
  public void SetComponent(MyComponent c) {
    var ref = m_ref; // ref is a copy of m_ref, no longer a reference to the same thing
    ref.value = c; // m_ref has not changed

    // the example above is contrived
    // but just to illustrate the point
    // that you need to be careful
    // to avoid editing copies of struct
  }
  private SafeRef<MyComponent> m_ref;
}

safe-refs's People

Contributors

beatthat avatar

Watchers

 avatar  avatar

Forkers

xiaofeilong1987

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.