Iterators aren’t magic. We’ve seen the IL2CPP output for them and it’s not complex. It turns out we can just as easily implement our own iterators and gain some nice advantages along the way. Read on to learn how!
Posts Tagged struct
Monads sound fancy, but sometimes they’re actually really simple and useful. Today we’ll look at the
Maybe monad, which is a low-overhead tool that’s extremely useful to prevent bugs.
NativeArray<T> is great, but very limited in functionality. We can fix this surprisingly easily! Today we revive a two year old series that created the iterator project. Iterators are like a no-GC version of
IEnumerable<T> and LINQ which have a lot of power but only support managed arrays (
List<T>. Today we’ll add support for
NativeArray<T> and inherit support for the same functionality. We’ll also spruce up the project with proper unit tests, assembly definitions, and runtime tests to confirm that zero garbage is created. Read on to see how this was done and how to use iterators with
A lot of powerful language features like LINQ require massive performance hits, but today we’ll discuss some easy, low-overhead ways to add some safety and usability to C#.
Value types like
int, structs, and enums seem simple, but much of what we think we know about them just isn’t true. This article explores how value types actually work in C# and uses that knowledge to improve how they’re implemented in the C++ scripting system.
The GitHub project is closing in on supporting all the “must have” features. Today’s article tackles “boxing” and “unboxing” so our C++ game code will be able to convert types like
int into an
object and then convert an
object back into an
int. Usually we want to avoid this because it creates garbage for the GC to later collect and ruins type safety, but sometimes an API like
Debug.Log insists that we pass it an
object. Read on to see how to use boxing and unboxing in C++!
The series to build a viable system to write Unity scripts in C++ continues! While these 11 articles have covered a lot of ground toward making a usable C++ scripting system, there’s still a lot to do. Writing the code for these articles takes quite a lot of time, so today I’m officially calling for collaborators on the GitHub project. If you’d like to join in, please leave a comment, send an e-mail, or submit a pull request. There’s plenty to do and your help would be greatly appreciated! Aside from that, today’s article is all about adding support for struct and enum types so we can use types like
TextureFormat from our C++ scripts.
I continue to learn a lot by reading the C++ code that IL2CPP outputs. Like reading decompiled code, it gives some insight into what what Unity’s build process is doing with the C# we give it. This week I learned that
sizeof(MyStruct) isn’t a compile-time constant like it is in C++. Because of that, IL2CPP generates some less-than-ideal C++ code every time you use it. Today’s article shows the process I went through to work around that issue and ends up with some code you can drop into your project to avoid the problem.
We code in C#, but that’s just a starting point. Our C# code is compiled to DLLs and then converted into C++ where it’s compiled again to machine code. The good news is that this isn’t a black box! I’ve recently been reading through the C++ code that IL2CPP outputs and learning quite a lot. Today’s article is about some of the surprises that I encountered and how you can change your C# code to avoid some nasty pitfalls.
Structs can be a great way to keep the garbage collector off your back and to use the CPU’s data cache more effectively. Not everything can be a struct though. At a minimum, you’ll need to use some Unity and .NET classes like
string. If your struct has any of these as fields, you can no longer use
sizeof(MyStruct). That really limits its usefulness, so a workaround is needed. Enter object handles: a simple way to represent any
object as a plain old
int which won’t break
sizeof. Read on to see how these work and some code you can easily drop into your project to start using them right away!