Hi everyone, I’m a C/C++ developer and I’m new to C# and Unity. while I was working on my game, I tried defining something using #define
:
#define SkibidiRany 5 //tells compiler: replace every “SkibidiRany” you find with number = 5
I soon found out that there isn’t a define command that defines a word to be something. I searched and searched for ways to do something close to #define
, but I either found the answers too complex or that they don’t satisfy my need.
My question: Is there a simple way, that “#defines
” in C#?
Thank you in advance!
1 Like
What is your task?
What are you trying to achieve?
In Unity, we can use #define TESTPLUGIN
, for example, to define the existence of 3rd-party plugin in the project. Then we can check it with the next code like plugin-dependent compilation thing:
#if TESTPLUGIN
// code
#endif
I am basically trying to achieve this:
I am only trying to do the constant one, any idea how to do that?
1 Like
What is your specific use case related to Unity?
I have multiple arrays containing different types of objects, all the arrays are related in the index stuff: array1[0] is in relation with array2[0], array1[1] is in relation with array2[1] and so on etc.
I need to assign multiple stuff to the same index in the different arrays at the same time. example:
I want to convert this code from c++ to c#:
#define SUMMER 0
#define WINTER 1
#define FALL 2
#define SPRING 3
class Example{
type1[] Season;
type2[] SeasonRelatedThing1;
type3[] SeasonRelatedThing2;
void ResetSpring() //when called, I want to set all items in the index SPRING = 3 to be null
{
Season[SPRING] = null;
SeasonRelatedThing1[SPRING] = null;
SeasonRelatedThing2[SPRING] = null;
}
};
I’m sorry for my bad explanation, and I know that there are 1000 different ways to do what I did up there better and more efficiently, but I tried to simplify what I want as much as I can…
1 Like
In C# we use variables for this inside classes:
public int v1 = 5;
You can also use constants if the values will not be changed later:
public const int V1 = 5;
What if I want to execute big functions that I only need at compile and build time, wouldn’t I need such macros for that? (my goal here is to improve runtime experience)
I am sorry for my dumb questions C# is new to me : )
1 Like