0

I'm trying to assign player inputs for new spells unlocked by a player, I'm using Unity's Input system. When a player selects a spell it subscribes the relevant binding to the spell's cast method (e.g. 'CastFireball()')

I want to replace the manual typing of "CastSPELLNAMEHERE()" on each spell with a string variable represented by _spellMethodID. I've been trying all sorts and googling from dawn to dusk and I can't get it to work. >:(

playerInput.FindAction(_spellID).performed += context => CastFireBall();

public void CastFireBall() { Debug.Log("fire"); }

I'd LIKE to be doing it like this instead:

string spell_methodID = "CastFireBall"
playerInput.FindAction(_spellID).performed += context => _spellMethodID;

public void CastFireBall() { Debug.Log("fire"); }

Help!

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
ZOMGbies
  • 51
  • 1
  • 1
  • 7
  • Please refer to the section entitled _"Should I use tags in titles?"_ under [What are tags, and how should I use them?](https://stackoverflow.com/help/tagging). I've removed the tags from your question title for you. – ProgrammingLlama Jan 19 '23 at 09:46
  • I am confused, you want the event handler of performed action to be a function returning string instead of void? – kuskmen Jan 19 '23 at 09:52
  • if you are asking how to build a spell system then you need to look into pillars of oop programming like inheritance and polymorphism. – Çağatay IŞIK Jan 19 '23 at 09:57
  • in theory you could use [`Invoke`](https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html) for call a method via its name ... not really the best approach though tbh – derHugo Jan 19 '23 at 10:08
  • @kuskmen If I write "...context => CastSpellMethod()" it works. I want to achieve the same results but by referencing a string, or some other pre-defined field/variable. For exampled I've tried Invoke(_methodID) but Invoke won't work here for some reason. – ZOMGbies Jan 19 '23 at 10:10
  • @derHugo Tried Invoke, it says I can't use it. "The name invoke does not exist in the current context" – ZOMGbies Jan 19 '23 at 10:11
  • 1
    well it needs to be used on a `MonoBehaviour` .. we don't really have a lot of detail about your setup .. – derHugo Jan 19 '23 at 10:12
  • Sorry i was trying to keep it short. The code appears within a scriptable object. Basically I am creating 1 SO per "spell", which is stored on the player in a list. The spell SO contains the stats which control casting cost/feed the UI, and its method (e.g. CastFireball). On login, if the spell is "learned" it instantiates the S.O. on the player. Doing so gives them access to the spell via the provided lines of code. I have no idea if my setup is ideal, but I wanted one class per spell in my system and this seemed like a good way. – ZOMGbies Jan 19 '23 at 10:47
  • Have you considered simply having a list of string ids indicating what spells a player has available and match that against a dictionary of to then cast the spell in each SpellSO (or whatever you’re calling your spell scriptable objects). Your game already has the so’s available, there’s no real need to instantiate new ones. – Milan Egon Votrubec Jan 19 '23 at 13:06

0 Answers0