Game wise I'm putting together a feature where the player is able to absorb abilities from enemies/environment and use them with the same button.
Player presses 1 near a volcano and absorbs three charges of molten rain-
Button one will now rain lava and decrement the remaining charges by one when pressed-
Upon the charges reaching zero button 1 once again reverts to absorption mode effectively "emptying the chamber"-
I expect twelve "chambers" for desperate buttons (though only four active at anyone time...I don't like the idea of my players hand needing to roam far from the movement keys in a keyboard setting)-
@BorisTheBrave That Action<> is pretty snazzy...I have never ran across that command and it makes sense immediately. It reminds me of how people construct and use databases to store things like item or bullet values where it would make sense to adjust certain values to simulate like....different guns or something similar.
I.E. High frequency, high bullet speed and and a barely visible line texture would effectively recreate a machine gun...adjust down the frequency, slow the bullet speed and change the texture to a little rocket to recreate a rocket launcher...all by adjusting similar values.
The reason this won't help me in this particular instance is that the "abilities" I have in mind for my "chambers" are very different from each other...I don't expect many will have similar values or variables aside from how many charges each ability grants and perhaps an "ability ID".
Something like...absorbing volcanos grants three charges of lava rain, absorbing pterodactyls would grant 1 charge of thirty seconds of flight, absorbing an angry mob would grants ten charges of summoning an angry caveman that would follow and assist you in battle till dead, etc...
I've been looking for a work-around to access methods from different classes without specifying the class name...
Here let me show you this bit of code I've been tossing together to test ways to achieve this, granted the whole "string variable to attempt tricking the compiler into calling upon different classes" didn't work but it shows what I'm getting at.
Class 1:
public class ChamberTest : MonoBehaviour {
private string scriptName = "Nothing";
private int charges = 0;
private bool loaded = false;
public GameObject spellHolder;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("O")) {
CaptureB();
}
if (Input.GetButtonDown ("I")) {
CaptureA();
}
if (charges < 1) {
loaded = false;
scriptName = "Nothing";
}
if (Input.GetButtonDown("U") && scriptName == "Nothing") {
Debug.Log (scriptName);
}
else if (Input.GetButtonDown("U") && scriptName != "Nothing"){
//scriptName loadedMagic = spellHolder.GetComponent<scriptName>()
//loadedMagic.Magic();
//charges -= 1;
Debug.Log (scriptName);
}
}
void CaptureA(){
loaded = true;
scriptName = "SpellTestA";
charges = 3;
Debug.Log ("Magic A is loaded");
}
void CaptureB(){
loaded = true;
scriptName = "SpellTestB";
charges = 5;
Debug.Log ("Magic B is loaded");
}
}
Class 2:
public class SpellTestA : MonoBehaviour {
public void Magic(){
Debug.Log("Magic A has fired!");
}
}
Class 3:
public class SpellTestB : MonoBehaviour {
public void Magic(){
Debug.Log("Magic B has fired!");
}
}
Toss classes 2 and 3 into the "spellHolder" object, child that object to the object containing class 1.
Check out the commented-out bits in the "else if" statement in class one, I was seeing if it would work to contain the names of classes 2 or 3 within the string "scriptName" and invoke methods from these using something like...scriptName loadedMagic = spellHolder.GetComponent<scriptName>()...then...loadedMagic.Magic();...no success obviously.
Honestly I think I am overthinking this a bit too much and can get away with something like this.
public class ChamberSwitchMethod : MonoBehaviour {
private int abilityID = 0;
private int charges = 0;
void Update () {
if (Input.GetButtonDown ("U") && abilityID == 0) {
//look for collision data to change the abilityID and charges appropriately
} else if (Input.GetButtonDown ("U") && abilityID != 0) {
//swtich statement to search through cases of the ability ID
//and use coresponding methods within this class
//decrement charges as needed and revert ability ID
//to absorb made should charges fall under 1
}
}
void AbsorbModeMethod (){
//absorb abilities via collision data
}
void LavaMagic (){
//rain lava
}
void SummonCavemanAlly (){
//summon an AI friend to assist with battle
}
void FlyLikeAPterodactyl (){
//fly for awhile
}
}
Now granted doing it this way the code itself will likely reach absurd lengths as more methods are added and the switch statement lengthens to accommodate new methods, but aside from when the button is pressed this doesn't look like it would be too bad a burden on the update.
@gimymblert I hear assigning your own delegates and events is the way to go, it's nice that Unity put in a more user friendly "event system" but I've touched upon subscribing objects to your own custom events and it seems to be more...customizable I guess...to do it yourself.
Now I just need to refresh my memory on events/delegates, haven't used them (or quite a few other aspects of C#)since I started using Unity. Thank God I kept extensive notes from the "old days" of creating visual studio abominations...this will come in handy for a completely different aspect of this game
.
@bittwyst Sadly they were separate scripts/classes, but I appreciate the reaching out