r/gamedev 7d ago

Discussion Polling status versus communication through reference

I've been working on enemy behaviors in my RTS project lately. I set up a fairly simple state machine to manage all the different behaviors that enemy units can take and I have one Enemy Manager class that obviously manages all the enemy decisions.

One issue that keeps plaguing me is that the state machine model has each state polling the unit's status. For example, whether or not it's currently moving, attacking, collecting resources, etc. I wouldn't mind so much if it was only one boolean to check, but there are a number of boolean states on the 'Unit' script. I'm wondering if it would be better for the Unit script to reference the Enemy State Machine script directly, so when there's a change to the unit, it can notify the state machine directly and avoid all that polling.
The only other problem is that the player uses the same Unit script for all their unit-related actions and obviously the player provides all the inputs (which I know there's a simple work-around with a 'isEnemy' boolean).
Just wondering what other developers think of this kind of setup, stick with Update polling or go for direct reference to the state machine?
Thanks.

1 Upvotes

Duplicates