Jump to content

Finite state machines

- - - - -

  • Please log in to reply
5 replies to this topic

#1
Jammydodge

Jammydodge

    Newbie

  • Members
  • Pip
  • 7 posts
Hey guys I'm creating a finite state machine but everytime I make a function with reference to another class it wont let me is there any way I can use my machine to change states of an object in another class? thanks :)!

#2
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
Have you added the reference?
using clause?
Do you have a reference in that class, preferably an interface?

Post some code, not the whole thing, just some headers, etc

#3
Jammydodge

Jammydodge

    Newbie

  • Members
  • Pip
  • 7 posts
this is my state machine I have other classes that I want to link it too :) basically objects that i want to change states for but im not sure how to do this any help really appreciated thanks :)!

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using Microsoft.Xna.Framework;

using XNAMachinationisRatio;



namespace FishORama

{

  class Fsm : OrangeFishToken

  {

    public enum State 

    {

      Hungry,

      Feeding,

      Digesting

    }


    private State _state; //this is the local variable that represents our state



    public Fsm(X2DToken pToken) : base(OrangeFishToken, aquarium)

    {

    }


    void Start ()

    {

      _state = State.Hungry;

      switch (_state)

      {

        case State.Hungry:

          Hunger(); // add function

          break;

        case State.Feeding:

          break;

        case State.Digesting:

          break;

      }

    }


  

    }

  }

}


#4
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
If you want external classes to be able to change the state of this object, you'll need to write access methods for that functionality, since _state is a private member (and rightly so!)

The most common way I can think of would be to create a public method called "setState" and pass in a FishORama.Fsm.State enum type as a parameter. Inside this method, you simply assign the value of the parameter to your _state private variable.

Edit: And then you simply call setState() from your other class.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#5
lespauled

lespauled

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 231 posts
  • Programming Language:C, C++, C#, JavaScript, PL/SQL, Delphi/Object Pascal, Visual Basic .NET, Pascal, Transact-SQL, Bash
unfortunately, that code fragment didn't help me. Are the other classes in the same project, or in a separate project in the solution?

If classes are not part of the same project, here are the steps that are used to add a reference:

Right click on project -> Add Reference -> select the reference in one of the tabs (if it's another project, select the project tab. If in the GAC, etc., or just lookup the dll)

#6
Jammydodge

Jammydodge

    Newbie

  • Members
  • Pip
  • 7 posts
thanks for the help guys :D ill give it a shot :)!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users