C++ Scripting: Part 21 – Implement C# Properties and Indexers in C++

Part 19 of this series started to allow our C++ game code to derive from C# classes and implement C# interfaces. The first step was to override methods as they’re the most common. Today we’ll tackle the second-most common: properties. We’ll also handle indexers, which are like properties with more parameters. Read on to see how to use this and how it works behind the scenes.

Read the rest of this article »

C++ Scripting: Part 20 – Performance Improvements

The last time we looked at performance was way back in part four of the series. Ever since then we’ve been relentlessly adding more and more features to the C++ scripting system. So today we’ll take a break from feature additions to improve the system’s performance in a couple of key areas.

Read the rest of this article »

C++ Scripting: Part 19 – Implement C# Interfaces with C++ Classes

Implementing interfaces and deriving from classes is commonplace in many codebases. Today we’ll make it so C++ classes can implement C# interfaces and derive from C# classes. This means our C++ game code will be able to implement custom IComparer classes for sorting a List and derive custom EventArgs for dispatching in events. Read on to see how this is implemented and how to use it in our projects.

Read the rest of this article »

C++ Scripting: Part 18 – Array Index Operator

When we covered arrays in part 14, we skipped implementing the [] operator with them. Instead, we opted for a simpler pair of GetItem and SetItem functions. Today we’ll address that oversight so our C++ game code can index arrays just like in C#.

Read the rest of this article »

C++ Scripting: Part 17 – Boxing and Unboxing

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 »

C++ Scripting: Part 16 – Events

Last week’s article covered delegates, so it’s only natural that we follow up this week by covering events. Supporting delegates has laid a good foundation for supporting events, so let’s dive in and see how to implement and use them in C++.

Read the rest of this article »

C++ Scripting: Part 15 – Delegates

This week’s article adds another major feature to the C++ scripting system: delegates. These are vital so C++ game code can use features like Unity’s UI system (a.k.a. UGUI). Without them, we wouldn’t be able to handle button clicks or other UI events. So read on to learn how these were implemented in the GitHub project.

Read the rest of this article »

C++ Scripting: Part 14 – Arrays

The series continues by adding support for a major feature: arrays. These are used very frequently throughout the Unity and .NET APIs and the lack of support for them has been a big missing piece of the puzzle for most games. The GitHub project has been updated to support single- and multi-dimensional arrays. Read on to learn how this support was implemented!

Read the rest of this article »

C++ Scripting: Part 13 – Operator Overloading, Indexers, and Type Conversion

Today’s article continues the series by adding support for C++ to call the various overloaded operators and indexers that are written in C#. This includes support for all 24 overloadable operators in C# plus the explicit and implicit type conversion operators. Indexers aren’t quite overloaded operators, but they allow for array-like indexing into C# types so they’re included today. Read on to learn how all this support was implemented in the GitHub project!

Read the rest of this article »

C++ Scripting: Part 12 – Exceptions

Like them or not, exceptions are the standard way of handling programming errors in C#. We need to be able to catch C# exceptions in our C++ code so we can gracefully recover from them. Likewise, we need uncaught C++ exceptions to behave like unhandled C# exceptions: display an error and move on instead of crashing. Today’s article continues the series by implementing both those features in the GitHub project and explaining how that implementation was done.

Read the rest of this article »