Sign in to follow this  
way2lazy2care

Messing with Unity (turn based help)

Recommended Posts

way2lazy2care    790
Howdy dudes.

first, I'm not sure if this is the best section for Unity. Should I post future questions in game mods? Scripting? I'm not sure.

Anyway, I'm really confused about how the game loop works in Unity. I'm guessing pretty much what unity does is goes through all the objects different update loops every frame.

I'm a little confused on how to better control the game loop for a turn based game. Normally I'd have a switch or something similar that would just call the corresponding player's turn functions. In Unity I could set up an empty object that can have a switch in it, but I'm not sure what would be a good way to give my empty object access to the player's corresponding turn functions.

Share this post


Link to post
Share on other sites
Palidine    1315
Just create a standalone object that has an integer field which indicates the current turn number. The player's "Next Turn" function increments this integer. Your game objects have an internal integer. If it's less than the current turn number, then Update. They will also need a Start() function to initialize their internal turn number to be equal to the standalone object at the time of their instantiation.

If you name the standalone or have it properly tagged then you can easily look it up and cache a reference locally in your game object's at their script Start() time. Refer to the scripting docs if you don't know how to programatically get references to named/tagged objects in Unity.

It would be more efficient if you had a callback system where the standalone object would manually update the game objects rather than them having Update() methods that tick every frame. I only just started using Unity so I don't yet know how to do something like that. I suppose when the player does a NextTurn then the standalone could iterate the list of tagged game objects or something.

-me

Share this post


Link to post
Share on other sites
way2lazy2care    790
Quote:
Original post by Palidine
Just create a standalone object that has an integer field which indicates the current turn number. The player's "Next Turn" function increments this integer. Your game objects have an internal integer. If it's less than the current turn number, then Update. They will also need a Start() function to initialize their internal turn number to be equal to the standalone object at the time of their instantiation.

If you name the standalone or have it properly tagged then you can easily look it up and cache a reference locally in your game object's at their script Start() time. Refer to the scripting docs if you don't know how to programatically get references to named/tagged objects in Unity.


So would it be better to have a reference to the objects in the next-turn object and have it call their turn functions, or to have the two references to the next-turn object in the player objects. D:

The reason I wasn't sure about references was because I heard the find() method was slow. Of course, there won't be terribly many objects in the scene, so it might end up being fast enough.

Share this post


Link to post
Share on other sites
Palidine    1315
[quote]Original post by way2lazy2care
So would it be better to have a reference to the objects in the next-turn object and have it call their turn functions, or to have the two references to the next-turn object in the player objects. D:

The former is better because then you only update the game objects when the NextTurn has just happened, rather than having them churn every frame to see if it's time to update yet.

Quote:
Original post by way2lazy2care
The reason I wasn't sure about references was because I heard the find() method was slow. Of course, there won't be terribly many objects in the scene, so it might end up being fast enough.


Find is very slow. That's why you do it once in the Start() function and cache the result. In the case where you're caching pointers to game objects in the standalone object, the game objects can find() the standalone in their Start() function and insert a reference to themselves inside the standalone.

I don't know if there is a better way to do event handling in Unity; you should definitely pour through the reference docs because a well engineered event system is what you want to use if it exists.

-me

Share this post


Link to post
Share on other sites
way2lazy2care    790
Quote:
Original post by Palidine
Find is very slow. That's why you do it once in the Start() function and cache the result. In the case where you're caching pointers to game objects in the standalone object, the game objects can find() the standalone in their Start() function and insert a reference to themselves inside the standalone.

I guess I was half being paranoid that the start() functions should be faster, but it really won't matter in the long run.
Quote:
I don't know if there is a better way to do event handling in Unity; you should definitely pour through the reference docs because a well engineered event system is what you want to use if it exists.

-me

I plan to. This is just for a prototype so I didn't have time to look into it as much as I should have.


Thanks much for your help.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this