Waves - another script.
If I want to call a function (which name is stored in “Wavesname” string) from another script, but it understands the calling function as Wavesname() not as " “Wave” + WaveNr.ToString()".
I’ve been reading for a while, that it’s supposed to be something connected with Methods and Types, but I can’t really understand them, any help?
You can do this using reflection or Invoke(). However, the question is, why are you trying to do this in the first place? It seems like very bad practice. Why not have a function that handles the desired logic from an int argument of the wave number instead? I.e. Waves.SpawnWave(waveNr).
I’m probably wrong, but from what I know you can’t call another C# class’s method just referencing the name. You could store a reference to the method using a delegate or UnityEvent and call it that way.
public delegate void TestFunction();
public class TestClass1 : MonoBehavior
{
public TestFunction FunctionRef;
private TestClass2 testClass2;
The problem is, that if we do it easy way, we do it like this:
if (WaveNr == 1)
Waves.Wave01();
if (WaveNr == 2)
Waves.Wave02();
BUT we might have hundreds of waves, so we have to simplify it, so the function we need to call would be in a string:
WaveName = “Wave” + WaveNr.ToString();
and then call the WaveName in another script (Waves).
I’ve been trying to understand the reflections/types/methods in different guides, but still can’t get it sadly.
I’m not saying that you do it like that at all - that’s still very, very bad practice. I don’t know what exactly the wave script does - does it simply spawn a group of mobs or something more? In any case, you clearly need to work with a collection of some sort, and use the wave number as index. The collection could be a list of delegates if you really have that much need for custom logic for every wave, or just an array of mobs to instantiate for every wave.
What Thomas is saying is true - it’s very, very rare that using Reflection or Invoke is the proper course of action. Besides being slow, it’s very easy to write code tht’s full of errors that don’t crop up until runtime. Plus, your code editor certainly isn’t going to understand it (no autocomplete, no code refactoring, etc). And, the icing on the cake, Reflection doesn’t even work on all platforms.
In my experience, I have only found Reflection to be the appropriate use case from Editor tools and debugging… and that’s about it.
Let’s take a step back: What do these Wave functions do, exactly? My guess would be that this is something along the lines of Tower Defense?
Yes, you are right - It’s kind of tower defence game. In the Waves script there are plenty of function, which contain everything what’s supposed to be on the wave and start different coroutines and so on… everything is working, there’s a but. The problem is - too much writing, WAY too much in another script. which is used to count other stuff and start new wave and determine which wave is it now and then call the function in another script (Waves).
Seems to me that your project is in dire need of some good ol’ refactoring then. As StarManta suggested, take a step back; think about what it is you need to accomplish when spawning a new wave, instead of trying to accommodate to a bloated script. Then, rewrite the functionality so it is easily extensible to any number of waves. It might seem that rewriting instead of writing new features is a waste of time, but it really, really isn’t.