Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

Project Tiny and structs - a Warning

Discussion in 'Project Tiny' started by furroy, Jun 11, 2019.

  1. furroy

    furroy

    Joined:
    Feb 24, 2017
    Posts:
    93
    Just a heads up for anyone else new to Project Tiny. A lot of things that were classes are now structs. I ran into this repeatedly porting over pre-tiny code, so I thought I would mention something here to save someone else a couple hours of debugging when trying out Tiny.

    Basically there are many objects like Rect, and Random, etc that seem to work like they did before, they have all the same members, properties, methods, etc. but in Tiny they are structs and not classes. So it's very common to take old code and copy/paste it in, and it will all compile and run but not work. C# will just happily pass all these things on the stack as copys instead of references, so you'll have code just mysteriously not work. You might have to go back through old code and add a lot of refs.

    Along this line, (and where I hit the wall and went splat) you can create extension methods on structs, which again will all build and run just fine, but your extension methods will all be working on copys of the structs which is probably not what you wanted.
     
    romiohasan likes this.
  2. Lucas-Meijer

    Lucas-Meijer

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    175
    Hey Furroy,

    Tiny (and dots in general), uses structs extensively, and as you've noticed they behave a little different than what you might be used to. The "method operates on a copy" problem is defenitely a common one. We tackle that internally often by passing a struct as ref. You can also pass the "this" as ref in an extension method.

    In general, when targetting tiny, you cannot expect to "copy paste old code in". It would be amazing if it would work like that, but to make that work, we'd have to keep everything the same until the end of time.

    We'd love to have you have a good experience though, even if things are still a bit rough around the edges, so please continue to let us know which parts are hurting so we can direct our engineering efforts into those places.

    Thanks, and good luck, Lucas
     
    sniffle63 likes this.