Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

c# - Looking for a better solution than using strings within Scriptable Objects to call specific methods elsewhere

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:

  • Card Name (string)

  • Cost (int)

  • Image (image)

  • Description Text (string)

  • Effect1 (script)

  • Effect1Amount (int)

  • Effect2 (script)

  • Effect2Amount (int)

  • Effect3 (script)

  • Effect3Amount (int)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use abstract class for that (with only one or two functions i suggest).

Like:

public abstract class Effect
{
    public virtual void runeffect(Card target)
    {
    
    }

    public virtual void runeffect()
    {
    
    }
}

Then for every effect you want you will write a script which use your abstract Effect class (inheritance) and add the effects you want to the main script. So your main card script will be something like that:

public Effect[] cardeffects; //add effects you want here
//like --> public Effect cardeffects={new Drawcard(2)};
public void playthecard()
{
    foreach(Effect e in cardeffects)
        e.runeffect;
}

And one effect example:

public class DrawCard : Effect
{
    public override void runeffect()
    {
    
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...