Posts Tagged object

C++ Scripting: Part 17 – Boxing and Unboxing

Tags: , , , , ,

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++!

Read the rest of this article »

No Comments

C++ Scripting: Part 5 – Bindings Code Generator

Tags: , , , , ,

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.

Read the rest of this article »

No Comments

C++ Scripting: Part 4 – Performance Validation

Tags: , , , ,

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.

Read the rest of this article »

2 Comments

C++ Scripting: Part 3 – Object-Oriented Bindings

Tags: , , ,

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.

Read the rest of this article »

4 Comments

C++ Scripting: Part 2 – Update C++ Without Restarting the Editor

Tags: , , ,

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#.

Read the rest of this article »

2 Comments

C++ Scripting: Part 1 – C#/C++ Communication

Tags: , , ,

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#.

Read the rest of this article »

15 Comments

Making Structs More Useful with Object Handles

Tags: , , ,

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!

Read the rest of this article »

5 Comments

How to Recover Anonymous Types

Tags: , , , ,

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!

Read the rest of this article »

No Comments

describeType vs. describeTypeJSON

Tags: , , , , , ,

flash.utils.describeType has been around since Flash 9 and is the standard way to find out interesting information about a Class type, including its metadata/annotations. However, there’s a hidden function called describeTypeJSON that provides an interesting alternative. Since describeType is notoriously slow, could describeTypeJSON be the speedy alternative we’ve been looking for? Today’s article puts them to the test!

Read the rest of this article »

7 Comments

When getSize() Lies

Tags: , , , , , , , , , , , ,

flash.sampler.getSize() is a handy tool for figuring out how much memory a class instance uses. However, it is often flat-out wrong. Today’s article tries it out on a variety of classes to find out which ones it works on and which ones it doesn’t.

Read the rest of this article »

4 Comments