Revision Difference
From_Lua_to_CSharp#528890
<cat>Dev.Intro</cat>
<title>From Lua to C#</title>
⤶
<warning>This article is a work in progress. Please consider not making any changes to it at the moment.</warning>⤶
# What is this guide
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.
⤶
# Comparison⤶
⤶
Not all of these code examples will compile and run as-is, some constructs are missing or simplified for less distraction and easier understanding of covered topics.⤶
⤶
## Top-level statements⤶
⤶
Things you can write in a file⤶
⤶
**Lua**⤶
```lua⤶
-- In lua you can have any code in a file on the top level,⤶
-- it will get executed when the file is processed.⤶
⤶
print('Hello!')⤶
⤶
function foo()⤶
return 100⤶
end⤶
⤶
print(foo())⤶
⤶
myGlobalTable = {⤶
hello = 'hello'⤶
}⤶
```⤶
⤶
**C#**⤶
```csharp⤶
// Currently in C# all you can have on the top level is imports⤶
// and type defenitions (classes, interfaces, enums, etc., possibly⤶
// scoped in namespaces).⤶
# WIP⤶
⤶
This article is a work in progress. Please consider not making any changes to it at the moment.// You'll need to wrap all of your code into types, classes and their⤶
// methods in order to actually do something. It's simple - on the top⤶
// of the file you can only define things, but not allocate variables⤶
// or execute expressions.⤶
⤶
enum MyEnum⤶
{⤶
One,⤶
Two⤶
}⤶
⤶
namespace Boo⤶
{⤶
public class Foo⤶
{⤶
public int myInt;⤶
}⤶
}⤶
⤶
// The below code isn't allowed as a top-level expression and will not⤶
// compile⤶
int a = 100;⤶
Something.DoStuff();⤶
```⤶
⤶
## Local and global variables⤶
⤶
**Lua**⤶
```lua⤶
-- Global variales are accessed from anywhere in the code⤶
globalVar = 100⤶
⤶
-- Local variables are accessed in the current lexical scope⤶
local localVar = 100⤶
```⤶
⤶
**C#**⤶
```csharp⤶
// MyClass.cs⤶
⤶
// No global variables, if you want a globally-accessed variable⤶
// you'll have to define it as a public static class member⤶
⤶
public class MyClass⤶
{⤶
public static int staticVar = 100;⤶
⤶
public static void DoStuff()⤶
{⤶
// This variable is local to the DoStuff method scope⤶
int a = 200;⤶
⤶
// You can also use '{' and '}' on their own to delimit⤶
// an arbitrary scoped code section⤶
{⤶
// This variable is local to the current arbitrary scope⤶
int a = 300;⤶
}⤶
⤶
Console.WriteLine(a); // 200⤶
}⤶
}⤶
```⤶
⤶
```csharp⤶
// Example.cs⤶
⤶
// We need to import the class in order to access its members⤶
using MyClass;⤶
⤶
public class Example⤶
{⤶
public void DoMoreStuff()⤶
{⤶
// This can be accessed from anywhere like this⤶
int boo = MyClass.staticVar;⤶
⤶
// This variable is local to the DoMoreStuff method scope⤶
int a = 100;⤶
}⤶
}⤶
```⤶