0

Using C#, I'm working on a project in which several objects need to interact with one another while all side by side in the main.

My current issue is as follows: I have three objects inside of my Main class, we'll call them Player, Enemy and BattleSystem. Without tightly coupling the objects by using ref, I'd like to be able to access the functions in the Player and Enemy objects while inside of the BattleSystem object, mainly to access the getters and some functions like DealDamage etc, I'm sure you get the idea.

What would you chaps suggest to do in a situation like this? Would going the Ref route be the only way or is there some way to make the objects visible project wide or some form of mailing system I can create to send messages back and forth between objects?

CinCout
  • 9,486
  • 12
  • 49
  • 67
  • What does "by using ref" mean to you? – Enigmativity Jul 13 '16 at 06:27
  • There's quite a number of ways to decouple classes from each other. Each with their own pros and cons. It really depends on your goals. If you could provide some more detail in your question, like what you've currently tried and how you're using `ref` it might help. – craftworkgames Jul 13 '16 at 21:59
  • I'm trying to make my code as Modular as possible, and I feel passing things by reference would too tightly couple the code to allow for that. I'm currently trying Vishal's solution, which may be exactly what I'm looking for. I'll keep everyone posted. Thank you everyone for your answers and comments so far. – Potato Mancer Jul 14 '16 at 13:46

2 Answers2

0

Should the game behave asynchronous or synchronous?

When it should be asynchronous, you can use Reactive Game Programming Architecture with Rx (Reactive Extensions)

In general you could also use the Mediator pattern: Mediator

wake-0
  • 3,918
  • 5
  • 28
  • 45
0

you can also use publish/subscribe events/delegates to establish communication between the three independent classes without actually referencing them internally.

you can also implement observer pattern if there is one publisher and multiple subscribers.

thanks.

Vishal Harne
  • 191
  • 1
  • 9