Code Monkey home page Code Monkey logo

Comments (7)

prodigga avatar prodigga commented on May 29, 2024

@JoshuaStrunk suggested created a NakamaID struct or class of some sort that wraps the byte[]. We could then just compare the objects if(nakamaID1 == nakamaID2), use them as keys for dictionarys
Dictionary<NakamaID,SomeOtherType>, store lists of them List<NakamaID>, etc.

from nakama-unity.

brinca avatar brinca commented on May 29, 2024

I usually compare them as strings:

if (System.Text.Encoding.Default.GetString(Id) == System.Text.Encoding.Default.GetString(OtherId))
Debug.Log("Match!");

Though this might be prone to errors...

from nakama-unity.

prodigga avatar prodigga commented on May 29, 2024

The issue isn't how to compare byte arrays, but that there isn't really a nice simple and quick way to do it out of the box :) Your example is fine, though it generate a lot of garbage and that could be a problem is code that is run, say, every frame. I just have a static helper method that takes 2 byte [] and compares them element by element.

from nakama-unity.

prodigga avatar prodigga commented on May 29, 2024

We want to be able to:

  • Compare them
  • Use them as keys for dictionaries
  • Store them in Hashsets to see if we an incoming ID is within the set already.

Very common use cases, ones I've come across a lot personally, but none of them are possible "out of the box" with a byte[]

from nakama-unity.

amirreza-kr avatar amirreza-kr commented on May 29, 2024

also you can use this extension method
`public static bool ByteArrayCompare(this byte[] a1, byte[] a2)
{
if (a1.Length != a2.Length)
return false;

    for (int i = 0; i < a1.Length; i++)
        if (a1[i] != a2[i])
            return false;

    return true;
}`

or convert them to a hex string and compare strings

public static string ByteArrayToString(this byte[] ba) { string hex = System.BitConverter.ToString(ba); return hex.Replace("-", ""); }

public static byte[] StringToByteArray(this string hex) { int NumberChars = hex.Length; byte[] bytes = new byte[NumberChars / 2]; for (int i = 0; i < NumberChars; i += 2) bytes[i / 2] = System.Convert.ToByte(hex.Substring(i, 2), 16); return bytes; }

from nakama-unity.

novabyte avatar novabyte commented on May 29, 2024

@prodigga @brinca @Amir-LooLoo I've opened a pull request to add methods you've requested. There's both a helper class called NIds and an extension method on the byte array value type. I've not created a custom type to represent IDs (for use in maps and lists) because it'd break the current API. I think the change is valuable but might as well be deferred until Unity 2017 is available. Lets move the discussion onto the pull request - #29

from nakama-unity.

novabyte avatar novabyte commented on May 29, 2024

I've added a helper class for this use case. This work is now on master at 9e087ad. If you have any questions drop into the community channel.

from nakama-unity.

Related Issues (20)

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.