S&box Wiki

Revision Difference

From_Lua_to_CSharp#528889

<cat>Dev.Intro</cat> <title>From Lua to C#</title> # What is this guide This is a C# starter guide meant for developers who are well familiar with Lua. ⤶ # First things first⤶ ⤶ ## What is Lua? ⤶ [Lua](https://en.wikipedia.org/wiki/Lua_(programming_language)) is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications. Lua is utilized in [Garry's Mod](https://gmod.facepunch.com/) as a scripting language, it is integrated into engine and game code in a way that allows you to extend game logic, create new entities and behaviors with scripts without changing the original C++ code.⤶ ⤶ ## What is C#?⤶ ⤶ [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)) is a general-purpose, multi-paradigm programming language encompassing static typing, strong typing, lexically scoped, imperative, declarative, functional, generic, object-oriented (class-based), and component-oriented programming disciplines. It is utilized in S&Box in the same as Lua in Garry's Mod.⤶ # Why is Lua bad?⤶ ⤶ Lua isn't bad. Lua is great for what it is, offering a shallow learning curve and fast development of simple things without thinking about types, structures and architecture of your project too much. You can just write a few pieces of code, glue them together and it works.⤶ ⤶ However, when you want to design something well-structured and strongly-typed, when your project becomes something beyond simple, the effort required to keep the code manageable starts increasing.⤶ ⤶ # Why is C# good?⤶ ⤶ C# offers many ways to better structurize your code such as a proper module system, namespaces, classes and strong typing, you always know what things are and where they come from.⤶ ⤶ ## Static typing⤶ ⤶ Static typing allows your IDE and the compiler to understand the code much better, thus providing great coding assistance (autocompletion, autocorrection, etc.), ability to navigate your code using references, finding errors before and during the compilation stage saving you from errors during runtime.⤶ ⤶ ## Rich syntax⤶ ⤶ C# also provides a much richer syntax compared to Lua, enabling you to express yourself using more intuitive and precise expressions. Even though mastering C# is not as easy as Lua and some of its constructs might seem a bit too much for small simple scripts, as soon as your projects grows to something more than 100 lines of code you'll appreciate its capabilities a lot.⤶ ⤶ # What's different⤶ ⤶ That's what this page is all about. Some of the main differences between Lua and C# are covered below, this information will help you project the knowledge about one language onto the other one and speed up its learning.⤶ ⤶ # Type system⤶ ⤶ ## Lua: Weak, dynamic typing⤶ ⤶ In Lua variable type isn't fixed. The same variable can be assigned data of various types, thus we can say variables in Lua don't have types. You can check what type of *data* the variable currently stores using runtime functions like `type`, `isnumber`, `isstring`, etc., but they have nothing to do with *variable* type.⤶ ⤶ ## C#: Strong, static typing⤶ ⤶ In C# you define and always know the variable type. You can't assign a variable of type `int` to a variable of type `string`. At first glance C#'s `var` keyword looks similar to Lua's `local`, but it's purpose is different. `var` allows you to skip writing the variable's type when it can be inferred from the variable's value. [Here](https://en.wikipedia.org/wiki/Strong_and_weak_typing) and [here](https://en.wikipedia.org/wiki/Type_inference#Types_in_programming_languages) has more on that.⤶ ⤶ # Number types⤶ ⤶ ## Lua: a single universal number type⤶ ⤶ All you have in Lua is `number`. It is used to store both integers and decimals, signed and unsigned. It's a 64-bit signed double under the hood.⤶ ⤶ ## C#: many number types⤶ ⤶ - Integers⤶ - `byte`, `sbyte` (8 bit)⤶ - `short`, `ushort` (16 bit)⤶ - `int`, `uint` (32 bit)⤶ - `long`, `ulong` (64 bit)⤶ - Decimals⤶ - `float` (32 bit)⤶ - `double` (64 bit)⤶ - `decimal` (128 bit)⤶ ⤶ Having various number types to choose from gives you the control over precision and memory usage. This is a C# starter guide meant for developers who are well familiar with Lua. There are some comparisons and code examples that will help you understand how to do things using C#. ⤶ ## Why S&Box uses C# instead of Lua⤶ ⤶ While Lua is flexible to an extent, easy to learn and build simple systems with, it lacks certain capabilities for creating solid scalable projects which are offered by C#. Lua was the best option at the time when Garry's Mod came around, but now it's the time to make a step further. ⤶ ⤶ # WIP⤶ ⤶ This article is a work in progress. Please consider not making any changes to it at the moment.