Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6733201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:44:27+00:00 2026-05-26T10:44:27+00:00

I read with interest this post to StackOverflow: How to Make a Basic Finite

  • 0

I read with interest this post to StackOverflow:
How to Make a Basic Finite State Machine in Objective-C

I used this as a basis to build my state-machine for a simple football game. I have states Defend, Attack, Stoppage, Neutral. I have created the state Machine and it will print out with NSLog “Now Entering State Defend” etc.

Now I would like to pass in a reference to my Football Team to the state Machine, such that I can make my team do something based on the state. Ie if its Defend, I can send my players to stand next to an opponent.

Ive tried many different ways but all end in syntax errors. Id appreciate a push in the right direction

TeamState.h Note the Errors Im getting in the comments

@class TeamState;

#import <Foundation/Foundation.h>
#import "FootballTeam.h"

@protocol TeamStateProt 

-(void) enterTeamState:(TeamState*)team;
-(void) executeTeamState:(TeamState*)team;
-(void) exitTeamState:(TeamState*)team;

@end


@interface TeamState : NSObject{
    id<TeamStateProt> currentTeamState;

    id<TeamStateProt> Stoppage;
    id<TeamStateProt> Neutral;
    id<TeamStateProt> Defend;
    id<TeamStateProt> Attack;

    FootballTeam *footballTeam;  //ERROR Unknown Type name FootballTeam 
}

@property (nonatomic, retain) id<TeamStateProt> currentTeamState;
@property (nonatomic, retain) id<TeamStateProt> Stoppage;
@property (nonatomic, retain) id<TeamStateProt> Neutral;
@property (nonatomic, retain) id<TeamStateProt> Defend;
@property (nonatomic, retain) id<TeamStateProt> Attack;

- (id)initWithFootBallTeam:(FootballTeam*) team; //Error Expected a Type
-(void)update;
-(void)changeState:(id<TeamStateProt>) newState;
-(void)executeState:(id<TeamStateProt>) State;
@end

TeamState.m

#import "TeamState.h"
#import "FootballTeam.h"
#import "Stoppage_TS.h"
#import "Neutral_TS.h"
#import "Defend_TS.h"
#import "Attack_TS.h"


@implementation TeamState 

@synthesize currentTeamState;
@synthesize Stoppage, Neutral, Defend, Attack;

- (id)init
{
    self = [super init];
     if (self) {
        // Initialization code here.
        id<TeamStateProt>  s = [[Stoppage_TS alloc] init];
        self.Stoppage = s;

        id<TeamStateProt>  n = [[Neutral_TS alloc] init];
        self.Neutral = n;

        id<TeamStateProt>  d = [[Defend_TS alloc] init];
        self.Defend = d;

        id<TeamStateProt>  a = [[Attack_TS alloc] init];
        self.Attack = a;

        self.currentTeamState = n;
        [self.currentTeamState enterTeamState:self];
        [self executeState:self.currentTeamState];
    }

    return self;
}

- (id)initWithFootBallTeam:(FootballTeam*) team
{
    self = [super init];
    if (self) {
        // Initialization code here.
        id<TeamStateProt>  s = [[Stoppage_TS alloc] init];
        self.Stoppage = s;


        id<TeamStateProt>  n = [[Neutral_TS alloc] init];
        self.Neutral = n;

        id<TeamStateProt>  d = [[Defend_TS alloc] init];
        self.Defend = d;

        id<TeamStateProt>  a = [[Attack_TS alloc] init];
        self.Attack = a;

        self.currentTeamState = n;
        [self.currentTeamState enterTeamState:self];
        [self executeState:self.currentTeamState];

        footballTeam = team;
    }

    return self;
}

-(void)changeState:(id<TeamStateProt>) newState{
    NSLog(@"Changing State");
    if (newState != nil) {
        NSLog(@"Changing a state which isn't nil");
        [self.currentTeamState exitTeamState:self];
        self.currentTeamState = newState;
        [self.currentTeamState enterTeamState:self];
        [self executeState:self.currentTeamState];
    }

}

-(void)executeState:(id<TeamStateProt>) State{
    [self.currentTeamState executeTeamState:self];
}

// Each update, execute the execute state on the current team state
-(void)update{
    [self executeState:self.currentTeamState];
}
@end

FootballTeam.h

#import <Foundation/Foundation.h>
#import "Player.h"
#import "TeamState.h"
#import "Football.h"

typedef enum  {
    Home,
    Away
} TeamType;

@interface FootballTeam : NSObject{
    Player *ReceivingPlayer;    //Player to receive the ball
    Player *ClosestToBall;      //Player closest to the ball
    Player *ControllingPlayer;  //Player who has the ball can be NULL
    Player *SupportingPlayer;   //Player in a position to receive the ball

    NSMutableArray *players;    //Array of all the Players on this team

    TeamState *state;           //Current state of the teams state machine

    TeamType type;              //Defines if Home team or Away team
}

@property (nonatomic) TeamType type;
@property(nonatomic, retain) TeamState *state;

-(void) sendPlayersToHome;
-(Player*) playerClosestToBall:(Football*) ball;
-(BOOL) areAllPlayersAtHome;

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:44:27+00:00Added an answer on May 26, 2026 at 10:44 am

    You’ve got circular references. You’re importing TeamState.h in FootballTeam.h and vice versa.

    To avoid this, import the .h files in the .m files only, and add forward declarations in the .h files.

    So, in TeamState.h:

    @class FootballTeam;
    

    And move the import to TeamState.m

    The same in FootballTeam.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read with interest the post How universally is C99 supported ?. One of
I've read with interest the post C difference between malloc and calloc . I'm
What is a simple piece of code to read a stored number, calculate interest
I read the following post with interest as it is an exact replica of
I read with interest user sunit 's answer to this question about updating an
Read on before you say this is a duplicate, it's not. (as far as
I read this PHP RegEx page , but either I'm missing something, misreading something,
I read the Avoiding Memory Leaks article with interest, and am concerned about danging
I read an interesting DailyWTF post today, Out of All The Possible Answers... and
I've read with interest the techniques available on the web to extract a unique

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.