Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot be resolved.

 
Dave Dunn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Righty i have 4 classes:



Hal:



State



and finally the first of my multple state classes to induce changes to hals vital statistics:



~My problem being i cant succesfully update the numbers from the sleepingState class. I'm a little lost, so if you kind folk know how to resolve the pHal is not resolved error in sleepingState.java, well i'd mighty thankful...

or a better way to go about coding this. Sorry for the long list of code, but i'm new to this schtick.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a bit difficult to know from the long coding what you are trying to achieve, but it seems to me that you are trying to use the pHal object from the sleepingState class (BTW a class name should read SleepingState with two capital Ss).

BUT: You seem never to have declared a pHal object in the SleepingState class. If you set up pHal in SleepingState, it will be a different object from pHal in your other classes. I think you might find things work better if you move that coding to a different class.

BTW: I am not experienced in multi-threading, but you have a call to sleep in your main() method. What does that do?
 
Kenneth Albertson
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, the "pHal is not resolved" error occurs because pHal is created within class Main, and so is not visible within class sleepingState. If you wanted a sleepingState object to be able to change the state of a Hal object, you would have to pass a reference to a Hal object (ie the variable pHal) to the sleepingState object.

More generally, though, why is sleepingState a class? All it does is (attempt to) change the state of a Hal instance. Shouldn't this be done by a method on the Hal class?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might reconsider whether sleepingState should be a class. It looks more like a method to me -- or maybe just a block of code within the switch/case statement of the State class. (Do you really want to create a new object every time?)

Anyway, your immediate problem is that the variable pHal isn't within the scope of sleepingState. There are a variety of ways to address this. For example, if sleepingState's code were actually a method inside the class where pHal is declared, then the variable would be within scope.

On the other hand, if you really do need to keep sleepingState as its own top-level class, then you basically need to pass (a copy of) your pHal reference to the sleepingState instance. For example, pHal could be a constructor argument (although this raises other issues that make me go back to the method vs. class argument).
 
Dave Dunn
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh i see, so i can sack off sleepingState... and maybe call functions set in Hal...

so mebe have

public void decreaseThirst(){
this.setCurrentThirst(+4);
}

Something as simple as that... I'm trying to look into finite state machines, a riff on maybe the AI behind controlling simpleton NPC AI folk or maybe havent quite decided. Anyone done this, know any good coding practices.

Also anyone know much about Fuzzy state machines - to my understanding they are like FSM's but offer outcomes on a balance of varibles.

I dunno, just thinking allowed.

thanks agen
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... Without really knowing what your objectives are, I'll offer this for consideration: An induceSleep method might start a new Thread that gradually increases/decreases energy, hunger, etc. depending on how long it runs (probably based on some logarithmic function).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic