C
C#ā€¢9mo ago
Martinecko30

ā” How to create class like MonoBehaviour from Unity

I want to learn how to create a class like that, where the functions CAN be implemented but don't have to, how to have for example start function that doesn't need to be specifically implemented in the extending class
25 Replies
Martinecko30
Martinecko30ā€¢9mo ago
so, when using abstract, I need to implement it but using virtual I don't? and when I'm calling the function from another script would it return error or something?
MODiX
MODiXā€¢9mo ago
Zeth
REPL Result: Failure
new X();
abstract class X {}
new X();
abstract class X {}
Exception: CompilationErrorException
- Cannot create an instance of the abstract type or interface 'X'
- Cannot create an instance of the abstract type or interface 'X'
Compile: 298.665ms | Execution: 0.000ms | React with āŒ to remove this embed.
Martinecko30
Martinecko30ā€¢9mo ago
I know, but with the virtual, if it doesn't need to be implemented wouldn;t it throw error as unimplemented function?
Pobiega
Pobiegaā€¢9mo ago
virtual means it can be overridden object.ToString() is virtual, for example
Martinecko30
Martinecko30ā€¢9mo ago
do I need to sprecify the override?
Pobiega
Pobiegaā€¢9mo ago
yes
Martinecko30
Martinecko30ā€¢9mo ago
and how to they make it that u don't have to specify the override
Pobiega
Pobiegaā€¢9mo ago
abstract, with a concrete implementation in a child class actually, no, that still needs an override.
Martinecko30
Martinecko30ā€¢9mo ago
so how do I go about making the derived class not use override?
Pobiega
Pobiegaā€¢9mo ago
you don't. Unity is magic. remember, unity isnt real C# but why does it matter? whats wrong with having to write override void Start()?
Martinecko30
Martinecko30ā€¢9mo ago
IDK really, I just wanted to know how to create it without the override
Pobiega
Pobiegaā€¢9mo ago
in that case, write your own runtime that can do whatever it wants with the IL that C# compiles to šŸ˜› thats what Unity did
Martinecko30
Martinecko30ā€¢9mo ago
that may be a bit out of my scope šŸ˜„
Pobiega
Pobiegaā€¢9mo ago
It is for 99.9999% of people, I'd say The rest of us, we use override
public abstract class MonoBehaviour
{
public abstract void Start();
}

public class MartinEcko : MonoBehaviour
{
public override void Start()
{
}
}
public abstract class MonoBehaviour
{
public abstract void Start();
}

public class MartinEcko : MonoBehaviour
{
public override void Start()
{
}
}
^ this would force it to be added
Martinecko30
Martinecko30ā€¢9mo ago
Thanks, I'm gonna use the virtual one as sometimes I won't need all the functions
Pobiega
Pobiegaā€¢9mo ago
This would not:
public abstract class MonoBehaviour
{
public virtual void Start()
{

}
}

public class MartinEcko : MonoBehaviour
{
}
public abstract class MonoBehaviour
{
public virtual void Start()
{

}
}

public class MartinEcko : MonoBehaviour
{
}
yeah
Martinecko30
Martinecko30ā€¢9mo ago
now the easy part, create a 3D renderer šŸ˜„
Pobiega
Pobiegaā€¢9mo ago
trivial šŸ˜„
// Implementing the 3d rendered was left as an exercise to the reader
// Implementing the 3d rendered was left as an exercise to the reader
Martinecko30
Martinecko30ā€¢9mo ago
šŸ˜„ i mean, I did it before, how hard can it be to do it again? šŸ˜„ this time in C# instead of Java ofc šŸ˜„
Pobiega
Pobiegaā€¢9mo ago
I have done silly stuff like implementing a crude 3d renderer in a console app. Do not do this. It is stupid.
Martinecko30
Martinecko30ā€¢9mo ago
well, I need some sort of engine after what unity has done and this may only benefit me as I would have complete control over what's going on, nevertheless I could do this for a school work for my class graduation, I might just learn something šŸ˜„
Pobiega
Pobiegaā€¢9mo ago
sure! ambitious, but why the hell not
Martinecko30
Martinecko30ā€¢9mo ago
I have two years to complete it, so I hope it's enough time šŸ˜„
cap5lut
cap5lutā€¢9mo ago
if u want to avoid virtual methods and want to force certain shapes of methods for functionality, u could use interfaces as well. depending on how fine grained u want them, this could cause a lot of interfaces, then depending how u design the game loop the order would matter as well, because for casting/type checks the look up for if a type implements an interface is O(n)
Accord
Accordā€¢9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.