Skip to main content
edited tags
Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
Removed tags from the title
Link
Vaillancourt
  • 16.4k
  • 17
  • 56
  • 61

Attaching scripts which trigger on specified events to objects (Unity, C#)

added 116 characters in body
Source Link
Sal
  • 103
  • 3

I'm new to Unity and C#. For concreteness, I'll phrase the question in terms of building a TCG like Magic: the Gathering or Hearhtstone, although the method seems of more general utility.

Suppose we wish to create cards that have special properties, such as: "when played, does [a thing]", or "Immunity to fire". Having created a card class, here is the pseudo-code that I would like to write, when initializing the 'library' of cards:

Card testCard = new Card( /*generic arguments here like HP, etc*/);

testCard.abilities = [fireImmunity, drawCardOnPlay, ...];

Where I would have defined the abilities fireImmunity, drawCardOnPlay, and all others in some convenient location. Of course, I could manually define these abilities on each card, but that seems like a short route to insanity: many cards will share the abilities.

First question: it seems I need to create a 'container' on each card to hold the abilities of that card; how does one do this, and what's it called? How/ where to write the abilities list that will go into these containers? First thought was the container should be a list, but I don't know what type of objects we should populate it with.

Second question: in order to handle effects that occur at specified times ("when drawn", "at end of turn", etc) I think I need to be able to create custom events, similar to the built in ones such as onCollisionEnter. These custom events would then be referenced in the scripts such as drawCardOnPlay. How to do this and what's it called?

Third question: is this unnecessary? Is there a more obvious way to do these things?

I'm certain this has answers somewhere (here, probably), but without knowing what these things are called I haven't made any progress. I've read some similar questions here but none that have addressed the implementation.

I'm new to Unity and C#. For concreteness, I'll phrase the question in terms of building a TCG like Magic: the Gathering or Hearhtstone, although the method seems of more general utility.

Suppose we wish to create cards that have special properties, such as: "when played, does [a thing]", or "Immunity to fire". Having created a card class, here is the pseudo-code that I would like to write, when initializing the 'library' of cards:

Card testCard = new Card( /*generic arguments here like HP, etc*/);

testCard.abilities = [fireImmunity, drawCardOnPlay, ...];

Where I would have defined the abilities fireImmunity, drawCardOnPlay, and all others in some convenient location. Of course, I could manually define these abilities on each card, but that seems like a short route to insanity: many cards will share the abilities.

First question: it seems I need to create a 'container' on each card to hold the abilities of that card; how does one do this, and what's it called? How/ where to write the abilities list that will go into these containers?

Second question: in order to handle effects that occur at specified times ("when drawn", "at end of turn", etc) I think I need to be able to create custom events, similar to the built in ones such as onCollisionEnter. These custom events would then be referenced in the scripts such as drawCardOnPlay. How to do this and what's it called?

Third question: is this unnecessary? Is there a more obvious way to do these things?

I'm certain this has answers somewhere (here, probably), but without knowing what these things are called I haven't made any progress. I've read some similar questions here but none that have addressed the implementation.

I'm new to Unity and C#. For concreteness, I'll phrase the question in terms of building a TCG like Magic: the Gathering or Hearhtstone, although the method seems of more general utility.

Suppose we wish to create cards that have special properties, such as: "when played, does [a thing]", or "Immunity to fire". Having created a card class, here is the pseudo-code that I would like to write, when initializing the 'library' of cards:

Card testCard = new Card( /*generic arguments here like HP, etc*/);

testCard.abilities = [fireImmunity, drawCardOnPlay, ...];

Where I would have defined the abilities fireImmunity, drawCardOnPlay, and all others in some convenient location. Of course, I could manually define these abilities on each card, but that seems like a short route to insanity: many cards will share the abilities.

First question: it seems I need to create a 'container' on each card to hold the abilities of that card; how does one do this, and what's it called? How/ where to write the abilities list that will go into these containers? First thought was the container should be a list, but I don't know what type of objects we should populate it with.

Second question: in order to handle effects that occur at specified times ("when drawn", "at end of turn", etc) I think I need to be able to create custom events, similar to the built in ones such as onCollisionEnter. These custom events would then be referenced in the scripts such as drawCardOnPlay. How to do this and what's it called?

Third question: is this unnecessary? Is there a more obvious way to do these things?

I'm certain this has answers somewhere (here, probably), but without knowing what these things are called I haven't made any progress. I've read some similar questions here but none that have addressed the implementation.

added 87 characters in body
Source Link
Sal
  • 103
  • 3
Loading
Source Link
Sal
  • 103
  • 3
Loading