We create objects out of structs and classes all the time, but oftentimes these evolve to the point where using them is really awkward. Today we’ll learn to recognize the telltale signs of an overextended object design and how to easily fix it.
Posts Tagged object
C# makes it easy to create large graphs of objects connected by their fields. The larger this graph grows, the more complex it is to deal with objects in the graph. It’s hard to look at code or set a breakpoint in a debugger and get an intuitive sense of all these connections. So today we’ll write a small tool to visualize an object graph!
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++!
Last week in the series we took a step back to verify that the C++ plugin’s performance was acceptable. With that confirmed, we’ll continue this week by making our programming lives easier. One pain point so far has been with exposing new Unity APIs to C++. It’s not that it’s difficult to do this, but there’s a lot of boilerplate required. That boilerplate takes time to write and it’s easy to make mistakes copying and pasting existing functions. So this week’s article introduces a code generator that will write the boilerplate for us! We’ll also reorganize the project a little so the code that supports C++ scripting is separated away from our game code. That’ll make it easy to add support for C++ scripting to any Unity project.
In the first three parts of this series, we focused on setting up a development environment that makes it easy and safe to write our game code in C++. Today’s article takes a step back to assess where we are in terms of performance. Is what we’ve built so far viable, or are the calls between C# and C++ too expensive? To find out we’ll use the existing framework to write some simple performance tests.
Last week’s article continued the series by eliminating the need to reboot the editor for every change to the C++ plugin. The idea is to make a more productive environment for us, the programmers, to work in. This week we’ll continue that theme by mimicking the object-oriented Unity API in C++. So instead of int transformHandle = GameObjectGetTransform(goHandle)
we’ll write a more familiar Transform transform = go.GetTransform()
. Also, we’ll build a simple system to automatically clean up object handles so we don’t have to do that manually.
Last week we began the series by establishing two-way communication between C# and C++. We used object handles to pass class instances between the two languages. Everything was going great, but then there was a major productivity problem: we had to restart the Unity editor every time we changed the C++ plugin. Today’s article is all about how to overcome that obstacle so you can iterate on your code just like with C#.
For all of the nice things about C#, writing code with it also comes with a lot of downsides. We spend so much time working around the garbage collector, working around IL2CPP, and worrying about what’ll happen if we use a foreach loop. Today’s article starts a series that explores what would happen if we escaped .NET and wrote our code as a C++ plugin instead of using C#.
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 MonoBehaviour
and 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!
When we just need a quick and dirty type to hold some values, C#’s anonymous types fit the bill: var person = { First="John", Last="Doe", Age=42 }
. On the down side, since these types are anonymous they have no explicit type. The var
variable is strongly typed, but you have to use the object
type when passing them to other functions. But then how do you get the fields back out? Today’s article shows you how so that anonymous types will be more useful to you. Read on to find out how to recover anonymous types!