I was asked to describe my use case in a previous version of my question. As such this post is quite detailed and goes into specifics about what I'm trying to do. If you arrived at this question hoping it would help you with your own problem, here is a quick TL:DR to see if it's worth delving any further:
TL:DR: I use scriptable objects in my project, and want to find the best way to attach scripts to those objects. My idea of using strings to call methods elsewhere is possible, but considered expensive and error prone. Here is a link to my original question if you would like to read why it is considered as such.
FULL QUESTION
I am currently creating my own card game (single player, so I don't need to worry about multiplayer or server stuff). I've created a scriptable object (CardAsset) to handle some fields that all my populated cards will need. Stuff like:
- Card Name (string)
- Cost (int)
- Image (image)
- Description Text (string)
I'm now at the point all the other stuff for my prototype is complete (playing field, deck, I can instantiate cards populated from my CardAsset, etc), and I need to start actually making the effects for my cards and programming them.
Since every card is different but some of the effects are the same ("draw more cards" and "deal damage to a character" are very common for instance), I figured I could save a lot of time by altering my scriptable object CardAsset to include some basic scripts and variables:
This way, I could create CardAssets with up to 3 effects on them (again such as "deal X damage to a character" or "draw Y cards") and then use something in my game to call those scripts with the associated variables when the player plays that card (leaving any unneeded slots as null). While any card that is truly unique would likely need it's own script, I figured this would cut down on time significantly (rather than programming each card individually).
My issue is that I cannot attach scripts to my CardAsset, so I figured a suitable workaround would be for me to type in the name of a method (DealDamage, DrawCards) in the Effect1/Effect2/Effect3 slot on the CardAsset (changing them to a string), and then have something read them and call the associated method elsewhere.
I understand that this solution is prone to errors and could be painful to maintain/change, so is there a better way I could do it in this specific project? Unfortunately as a beginner I currently lack the metalanguage to easily find a solution to this issue, so any pointers at all would be most beneficial. I am more than happy to do additional research on my own, I just need to be pointed in the right direction.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…