S&box Wiki

Revision Difference

From_Lua_to_CSharp#528884

<cat>Dev.Intro</cat> <title>From Lua to C#</title> ⤶ #What is this guide ⤶ # What is this guide This is a C# starter guide meant for developers who are well familiar with Lua. ⤶ #WIP⤶ ⤶ S&Box isn't released yet so this information is probably not useful for you unless you work for Facepunch or have somehow got very early access to the game.⤶ ⤶ This wiki is mainly for us right now. We're going to be filling it with useful information as we develop so it's all rea‎dy when the game is ready.⤶ # 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 `string` and so on. There is a `var` keyword in C# that looks similar to Lua's `local` at a glance, but it's purpose is different. All `var` does is allows to avoid explicit variable type definition when it can be inferred from the assigned expression. [Here](https://en.wikipedia.org/wiki/Strong_and_weak_typing) and [here](https://en.wikipedia.org/wiki/Type_inference#Types_in_programming_languages) is 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.