0

Long form, TL;DR below

I'm currently developing a game using Zenject and designed my UI and game architecture very similar to a web app where there are IConversationRepository-Interfaces providing access to data like Conversations with methods like GetConversationById(...).

So far, the Interfaces are implemented by dummies that return hardcoded data, but that has to change when I proceed further.

If this were a web application, I would just use an ORM and let it handle the querying and loading of objects, so my question is whether this also exists for Unity / Zenject and if not, what the proper way is to implement data access when using Zenject.

UPDATE (in response to helpful discussion with @Fattie)

What I am basically interested in is a generic way to load objects with references to others, and being able to query for them.

As an example, imagine a Conversation object that holds many ConversationItems which each have a Speaker field of type Person. I would then like to be able to perform operations like this:

var someSpeaker = personRepository.getById(100);
var conversation = conversationRepository.getById(0);

if (conversation.items[0].speaker == someSpeaker) {
    // I know, in this example this behaviour could be moved
    // into a method of Person to avoid the check for identity
    // but I hope the idea is understandable.
}

What's important here is that if the speaker is the same for multiple SpeechItems, the objects referenced of each should be the same as well, which makes simply deserializing JSON infeasible.

TL;DR Is there something like an ORM that can be used to implement the Repository pattern? If not, how should I access data?

Andreas
  • 300
  • 5
  • 15
  • 1
    I have bad news, "Zenject" is unfortunately absolutely, completely, useless for an ECS system like Unity. ECS systems such as game engines simply *are not* OO programming systems. (You usually use an OO language, in this case C#, to *write the components* you can attach to game objects, but that has nothing to do with the architecture and structure of an ECS "scene".) – Fattie Dec 26 '20 at 15:15
  • @Fattie Thanks for your answer and that may be true for many situations, but it really depends on the use case. How would you realize storing and loading dialogues or missions in ECS? Zenject is also used by successful games like Pokemon GO and Beat Saber. (https://github.com/svermeulen/Extenject/blob/master/Documentation/GamesThatUseZenject.md) – Andreas Dec 26 '20 at 19:04
  • it's incredibly easy. (like most things in unity.) the concept of a singleton is meaningless in a unity scene, so you just have a game manager object on your preload scene. there's nothing more to it than that. https://stackoverflow.com/a/35891919/294884 it is literally a line of code – Fattie Dec 26 '20 at 22:38
  • note, on your "manager script" (let's call it Conversations.cs) which would be attached to a DDOL game object on the preload scene you can use absolutely any data technique you like - MySql is possible (but a bit clunky, remember Unity can run on almost any platform!) I very much doubt an sql database would be relevent though - simply use .Net sata structures and away you go. store locally to Json if needed. notice this question where someone was (simple) wondering "how to hold 100,000 strings" – Fattie Dec 26 '20 at 22:43
  • This preload scene behaviour is achievable with Zenject's Project Context (It internally makes these objects survive scene changes via DDOL object as well). I probably used poor phrasing, because what I am looking for is a proper / generic way to handle the object retrieval you alluded to with simply using .Net data structures or using Json. I need something that for example stitches up cross reverences, e.g. if a Conversation talk item references a Speaker. I'll try to update the question to better reflect that. – Andreas Dec 26 '20 at 23:01

1 Answers1

1
  1. If I'm not mistaken the standard Dictionary in .Net would be very helpful here.

    Handy link:

    https://programmingwithmosh.com/net/csharp-collections/

  2. Note there is rarely a need for a relational database approach in Unity, the everyday .Net data structures will be fine for 100,000s of searchable text items. You can cobble together MySql in Unity, but it's a matter of handling it individually per platform.

  3. Regarding Unity per se. Just use a "game manager" type GameObject (i.e., simply it is marked DDOL) with your class on it (perhaps "ConversationsProvider" or whatever name). Singletons per se are meaningless in Unity, and it's pretty likely you'll need to use the general features of MonoBehavior, so you just make it a "game manager" style component. "ConversationsProvider" can be as simple or complex as needed; it may have many subsystems (live connections to servers, generations systems, who knows). But from every other part of the project, it's just a completely straightforward MonoBehavior you can access - it is totally trivial.

This is the "straightforward" way you'd do this in Unity. If OP is experimenting with Zenject, could perhaps try the "usual" approach too!

To more literally answer the OP's question, yes, there are a couple of I would say experimental ORM/MySql solutions for Unity. Can easily google some "ORM for use in Unity", example, however I would just advise that it's on the "experimental" side. There's never been a really simple solid MySql solution for Unity, as of yet.

halfer
  • 19,824
  • 17
  • 99
  • 186
Fattie
  • 27,874
  • 70
  • 431
  • 719
  • I'll wait a while longer to see if anyone knows of a solution that's mature and handles relationships between objects well. If not, I'll accept this answer (and have to make up my mind on how to proceed :D) – Andreas Dec 27 '20 at 02:09
  • for sure, it's always best to wait for more answers!! – Fattie Dec 27 '20 at 15:49
  • I spent a few days evaluating Extenject / Zenject, and did implement it partially into my WIP game, but won't be using it going forward. My reasoning being that though I am a professional programmer, I don't know Unity3D well enough to know how best to marry it with a DI tool. – Christh Jan 01 '21 at 21:48
  • To make a long story short, "Zenject" is totally useless. I appreciate their ads state that they are used in some well-known games; the devil is in the detail, who knows how it is used; and sure, it some *very specific* circumstances it may be handy. – Fattie Jan 02 '21 at 01:23