-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogramming_references.cs
More file actions
513 lines (373 loc) · 17 KB
/
programming_references.cs
File metadata and controls
513 lines (373 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
// Author: Wyatt Murray
// Collaborators: Zebulon
// Date: 9/7/2023
// Description: This document serves as a brief but extensive reference file for programmers,
// encompassing a wide range of commands, syntax, and concepts in both C# and Unity.
// It offers a comprehensive repository of valuable references for both new learners
// and experienced developers.
// Version: v3.1
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Media;
#region 6 pillars of Object Oriented Programming (OOP)
// 1. Abstraction: Simplify complex systems using classes and objects to model real-world entities and behaviors.
// 2. Encapsulation: Bundle data and methods into classes, ensuring data integrity and access control.
// 3. Inheritance: Create new classes based on existing ones, promoting code reuse and maintainability.
// 4. Polymorphism: Treat objects of different classes as if they share a common base class, enhancing code flexibility.
// 5. Composition: Compose complex objects from simpler ones, fostering modularity and reusability.
// 6. Interface: Define contracts that classes must adhere to, enabling code extensibility and polymorphism.
// These principles guide the design and implementation of C# programs, making them more organized and maintainable.
#endregion
#region Click the plus on the left of this message!
// this is a region, a way to quickly hide bulk sections of code when you are working so you dont scroll 2000 miles to find the method you wrote 3 minutes ago.
// hovering your cursor over the plus icon and reveal the contents for a brief look
// this is a collaspable button!
#endregion
#region Accessing Private Variables in Unity Inspector:
// In Unity, use [SerializeField] to expose private variables in the Inspector.
// It lets you edit them without making them public, which follows the discipline of encasulation
[SerializeField] private int myVariable = 10;
// Now 'myVariable' is visible in the Inspector and can be adjusted there.
#endregion
#region Types of Access Modifiers:
// private:
// - Available only within the class or method it is declared in.
// - Variables and methods marked as private are not accessible from outside the class.
private int myPrivateVariable;
// public:
// - Publicly available to every script.
// - Variables, methods, and classes marked as public are accessible from any other part of the code.
public int myPublicVariable;
// protected:
// - Accessible within the class it is declared in and its subclasses.
// - Variables and methods marked as protected can be used or modified by derived classes.
protected int myProtectedVariable;
// internal:
// - Accessible within the same assembly (a compiled unit, e.g., a DLL).
// - Variables, methods, and classes marked as internal are not accessible outside the assembly.
internal int myInternalVariable;
#endregion
#region Getters and Setters (Properties):
// Declare a public variable 'variableA'.
public int variableA;
// Declare a public variable 'canSet' to control whether 'variableA' can be set.
public bool canSet = true;
// Use properties to control access to 'variableA'.
public int PropertyA
{
// 'get' allows you to retrieve the value of 'variableA'.
get { return variableA; }
// 'set' allows you to modify 'variableA' with additional logic.
set
{
// Check if 'canEdit' is true before modifying 'variableA'.
if (canEdit)
{
variableA = value;
}
else
{
Debug.Log("The variable can't be edited.");
}
}
}
#endregion
#region Variables!
// Variables are structured by access modifier, type, name, and then any properties youd like to set.
// The order follows: AccessModifier Type name
// one can apply [SerializeField] in front of any standard data type.
// Any standard datatype can be uninitialized, or set to null.
int myInteger; // Integers!
double myDouble; // A float, but has double the precision.
float myFloat; // A 32-bit floating-point number.
bool myBoolean; // Boolean expression, can only be true or false.
string myString; // An array of characters.
char myChar; // A single ASCII character.
object myObject; // A reference to any type (including user-defined types).
null myNull; // Represents an invalid or empty value.
#endregion
#region Type Casting
// Type Casting in C#:
// Type casting converts data from one type to another.
// Implicit Casting: Automatically converts smaller types to larger types.
int num = 10;
double decimalNum = num; // Implicit cast from int to double.
// Explicit Casting: Manually converts larger types to smaller types.
double bigDecimal = 15.75;
int smallInt = (int)bigDecimal; // Explicit cast from double to int.
// Casting with Reference Types: Allows treating objects as different types.
class Animal { }
class Dog : Animal { }
Animal animal = new Dog();
Dog dog = (Dog)animal; // Explicit cast for accessing Dog-specific members.
#endregion
#region Boolean Expressions and Operators:
// ? (Conditional Operator):
// The conditional operator (ternary operator) is a concise way to express an if-else statement.
// Syntax: (condition) ? trueExpression : falseExpression
bool isTrue = true;
bool isFalse = false;
bool result = (isTrue) ? trueExpression : falseExpression; // If isTrue is true, result is trueExpression; otherwise, it's falseExpression.
// > (Greater Than):
// Checks if the value on the left is greater than the value on the right.
bool greaterResult = (5 > 3); // true, because 5 is greater than 3.
// >= (Greater Than or Equal To):
// Checks if the value on the left is greater than or equal to the value on the right.
bool greaterOrEqualResult = (5 >= 5); // true, because 5 is equal to 5.
// < (Less Than):
// Checks if the value on the left is less than the value on the right.
bool lessResult = (3 < 5); // true, because 3 is less than 5.
// <= (Less Than or Equal To):
// Checks if the value on the left is less than or equal to the value on the right.
bool lessOrEqualResult = (3 <= 3); // true, because 3 is equal to 3.
// == (Equality Operator):
// Checks if the values on both sides are equal.
bool equalResult = (5 == 5); // true, because 5 is equal to 5.
#endregion
#region Creating Structs in C#:
// A struct is a lightweight data structure in C# that groups together related variables.
// Unlike classes, structs are value types and are typically used for small, immutable data.
// Defining a Struct:
// You can define a struct using the 'struct' keyword followed by the struct's name.
// Inside the struct, you declare its fields, which are variables that hold data.
public struct Player
{
// Fields for the Player struct.
public string playerName;
public int playerScore;
}
// Using a Struct:
// You can create instances of a struct like any other data type.
Player newPlayer = new Player();
newPlayer.playerName = "John";
newPlayer.playerScore = 100;
// Accessing Struct Fields:
string name = newPlayer.playerName; // Accessing the playerName field.
int score = newPlayer.playerScore; // Accessing the playerScore field.
// Advantages of Structs:
// - Structs are value types, which means they are stored on the stack and can be more memory-efficient.
// - They are suitable for representing small, self-contained data.
// System.Serializable Attribute:
// The 'System.Serializable' attribute is used to mark classes and structs as serializable in C#.
// Serialization is the process of converting objects or data structures into a format that can be easily stored or transmitted.
// Marking a Struct as Serializable:
// To make a struct serializable, you apply the [System.Serializable] attribute to it.
[System.Serializable]
public struct Item
{
public string itemName;
public int itemID;
}
// Using Serializable Structs:
// Once marked as serializable, you can use the struct in various contexts, such as saving and loading game data.
// Example: Serialize and Deserialize Using JSON:
// JSON (JavaScript Object Notation) is a common format for data serialization.
// Serialization (Convert to JSON):
Item myItem = new Item
{
itemName = "Health Potion",
itemID = 1
};
string json = JsonUtility.ToJson(myItem);
// Deserialization (Convert from JSON):
Item deserializedItem = JsonUtility.FromJson<Item>(json);
#endregion
#region Arrays lists and dictionaries
//Arrays, lists, and dictionaries can be initialised with the 'new' keyword, but must be initialised to use in the inspector
//Array Syntax:
// Declaring an array and allocating memory.
int[] Array1;
Array1 = new int[10]; // Creating an integer array of size 10.
// A different way to declare and allocate an array in one line.
int[] Array2 = new int[10];
// List Syntax:
// Declaring a generic List and creating an instance.
List<dataType> List1;
List1 = new List<dataType>(); // Creating an empty List.
// Another way to declare and create a List in one line.
List<dataType> List2 = new List<dataType>();
// Dictionary Syntax:
// Declaring a generic Dictionary without creating an instance.
Dictionary<int, string> Dictionary1;
// Additional Context:
// Arrays, Lists, Dictionaries, and other data types are reference data types.
// They store data and have their own memory allocation.
// When you declare multiple variables of these types, each one gets its own memory space.
// Changes to one variable do not affect others, as they have different memory addresses.
#endregion
#region Loops:
// For Loop Syntax:
for (int counter = 0; counter< 10; counter++)
{
// Code to be executed repeatedly.
}
// While Loop Syntax:
int whileLoop = 0;
while (whileLoop< 10)
{
// Code to be executed while the condition is true.
whileLoop++;
}
// Do-While Loop Syntax:
do
{
// Code to be executed at least once, then repeated if the condition is true.
} while (true);
// Foreach Loop Syntax:
int[] array1 = { 1, 2, 3, 4, 5 };
foreach (int item in array1)
{
// Code to be applied to each element in the array.
}
#endregion
#region Classes:
// Class1 with a public field named 'name'.
public class Class1
{
public string name = "WyattMurray";
// Constructors can be defined to initialize objects.
public Class1()
{
// Constructor logic can be added here.
}
}
// Inheritance:
// Class2 inherits from Class1.
public class Class2 : Class1
{
// You can access the 'name' field from the base class (Class1) since it's public.
public void SomeMethod()
{
Debug.Log(name); // Accesses 'name' from Class1.
}
}
// Methods:
// Private Method1:
private void Method1()
{
// Private methods are only accessible within the declaring class.
// They cannot be accessed from outside the class.
}
// Public Method2:
public object Method2()
{
// Public methods are accessible from any part of the code.
// 'object' is the return type, which means this method can return any object.
return null; // You can return an object or value of the specified type.
}
#endregion
#region Interfaces in C#:
// An interface is a contract that defines a set of methods and properties that a class must implement.
// It allows you to define a common set of behaviors that multiple classes can adhere to.
// Defining an Interface:
// You define an interface using the 'interface' keyword followed by the interface's name.
public interface I_Find
{
// Interface members (methods or properties) are declared without implementation.
void Find();
}
// Implementing an Interface:
// To implement an interface, a class must provide concrete implementations of all the interface's members.
public class FindMe : I_Find
{
public void Find()
{
// Implement the 'Find' method according to the interface's contract.
}
}
// Using Interfaces with foreach:
// You can use interfaces to find components or objects that implement a specific interface.
// Example: Search for Objects with the 'I_Find' Interface:
foreach (I_Find findableComponent in components)
{
// 'findableComponent' is an object that implements the 'I_Find' interface.
// You can call the 'Find' method or access any other interface members here.
}
#endregion
#region All miscellaneous stuff!
#region Reading and Writing Files in Unity:
// In Unity, the "StreamingAssets" folder is a special folder that will be included in the build.
// You can use it to read and write files that are bundled with your game.
// Write Content to a File:
void WriteToFile()
{
// Create a StreamWriter to write to a file in the StreamingAssets folder.
StreamWriter newFile = new StreamWriter(Application.streamingAssetsPath + "/target.txt");
// You can also use Application.persistentDataPath to store data that persists between sessions.
// Write content to the file.
newFile.WriteLine("Hello!");
// Close the StreamWriter to save changes.
newFile.Close();
}
// Read Content from a File:
void ReadFromFile()
{
// Create a StreamReader to read from a file in the StreamingAssets folder.
StreamReader reader = new StreamReader(Application.streamingAssetsPath + "/target.txt");
// You can also use Application.persistentDataPath to read data that persists between sessions.
// Read lines from the file sequentially.
string firstLine = reader.ReadLine();
string secondLine = reader.ReadLine();
// Close the StreamReader when you're done reading.
reader.Close();
}
// Check if We've Reached the End of the File:
bool IsEndOfFile()
{
// The StreamReader.EndOfStream property returns true if we've reached the end of the file.
return reader.EndOfStream;
}
#endregion
#region Error Handling in C# and Unity
// with Try-Catch Statements in C#:
// Try-catch statements are used for handling exceptions (errors) in C#.
// They ensure your program continues running even when unexpected issues occur.
try
{
// Code that may cause an exception goes here.
int result = 10 / 0; // Example: Division by zero will throw an exception.
}
catch (Exception ex)
{
// Catch the exception and handle it gracefully.
Debug.LogError("An error occurred: " + ex.Message);
// You can log an error message, notify the user, or take appropriate actions.
}
// Unity Debugging:
// In Unity, the 'Debug' class provides methods for displaying messages in the console during runtime.
// Debug.Log: Used for general logging and information.
Debug.Log("This is a regular log message.");
// Debug.LogWarning: Used for non-fatal warnings.
Debug.LogWarning("This is a warning message.");
// Warnings indicate potential issues but won't stop the program.
// Debug.LogError: Used for critical errors.
Debug.LogError("This is an error message.");
// Errors indicate serious problems that should be addressed.
// Debug.Assert: Used for conditional debugging.
bool condition = false;
Debug.Assert(condition, "This condition is false.");
// Asserts help catch logical errors during development.
// Debug.DrawLine and Debug.DrawRay: Used for visual debugging.
Vector3 start = Vector3.zero;
Vector3 end = Vector3.forward;
Debug.DrawLine(start, end, Color.red);
// DrawLine and DrawRay visualize vectors and lines in the scene view.
#endregion
#region Reading Exceptions in C#:
// When an error occurs, exceptions provide vital information for debugging:
//1.Exception Message:
//-Use `ex.Message` to get a description of the error.
//2. Stack Trace:
//-Access `ex.StackTrace` for a detailed report of the call stack leading to the error.
//By examining these details, you can quickly identify and fix issues in your code.
#endregion
#region Creating File Menus in Unity:
// In Unity, you can use the [CreateAssetMenu] attribute to define custom file creation menus for ScriptableObjects.
// Example Usage:
[CreateAssetMenu(fileName = "New Weapon", menuName = "ScriptableObjects/Weapon/RangedWeapon")]
// - 'fileName' specifies the default file name when creating an asset.
// - 'menuName' determines where the asset will appear in the "Create Asset" menu.
#endregion
#endregion