Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by 9a3eedi · Oct 28, 2015 at 05:02 PM · dlllogicarchitecturedesign-patterns

Implementing game logic in a separate library: callbacks

So I'm working on a Card game, and this game actually has somewhat complex logic. Every card in the game has its own behaviour, and as a result, coding in the logic directly in unity with GameObjects became very hard and unruly, so I decided to implement the logic separately in a Visual Studio project, and then import the logic code into unity as a DLL or in source code form, whichever is more convenient. I could then just have an empty gameobject contain a gamelogic object, and then other classes written in unity act according to that logic. I realized this was a good idea when I found this answer.

https://gamedev.stackexchange.com/questions/61210/should-i-code-game-logic-separately-from-game-engine-scripts

So here's something I keep wondering about: how to handle user input.

My current idea is to have my GameLogic class accept a reference to an object which implements an interface that contains callbacks functions. The GameLogic class can then call these callback functions whenever it needs user input. The UI class that implements the interface can then perform the necessary steps to obtain user input. In the case of unity, the UI class will probably do different things with GameObject depending on the kind of input needed.

This works for command line games, which is what I am currently implementing real quick to test my logic. However, will this work in Unity? I will need to make the callbacks run in a separate thread than the game. How convenient/inconvenient is this?

Should I implement user input into my library in another way that suits Unity?

The same thing applies for "Game Events".. for example, if my logic class decided to draw a card from a deck, I want the UI to show this action, so I make the logic class call a callback function that a Unity object implements, and then that unity object performs the animations and everything. Is this practical/realistic?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Oct 28, 2015 at 08:19 PM

Yes, you can do game logic in a dll, callbacks and threads.

Unity however isn't 100% threadsafe so you likely want to dispatch events into a synchronized message queue. Each Update, your Unity script can take all messages from the queue and run them. UI handlers in Unity can then add new messages to the queue that goes back to the game logic thread to be executed there.

How convenient/inconvenient is this?

You'll miss out on some features like profiling to name one that immediately pops up in my head (unless that now is supported, I saw some improvements on the profiler but I havent explored it all in detail), but perhaps it's not that crucial for your logic. You'll probably get some editor crashes if you don't get a good grip on your thread and shut it down properly. But apart from being able to port your logic code and use tools like NUnit without having to run the editor, I doubt you'll gain much. You don't have to write all the logic for a card on the actual script that sits on the card game object. Your card can be a proxy or adapter to your main logic which still resides in Unity, running on the same thread as everything else.

Ask yourself: Why does it have to be threaded? If you can't answer that question, maybe you are barking up the wrong tree.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Notifying about character/item selection 1 Answer

Weapon enchantments - designing the code architecture? 2 Answers

DLL found in editor but not in build version 2 Answers

Unity 5.5x external dll issue on build after updade from 5.3.x 1 Answer

Unity game freezes a few seconds after loading scene 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges