Results 1 to 7 of 7
  1. #1
    martypapa is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default Listening for variables?

    Hi,
    At the moment if I want to detect a change in a variable, I use a thread to loop, and then check if the variable is different from itself one loop before. I'm sure this isn't the most efficient way to do this though.

    Is it possible to listen for variable changes like you can listen for mouse clicks?
    Thanks.

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Is it possible to listen for variable changes like you can listen for mouse clicks?
    Thanks.
    I'm not quite sure what you mean by this, but you could start a separate thread that run a while loop checking if the variable is equal to it's instance of the variable. so if one changes, the loop will replace its variable with the real one and run a method you want to run.

  3. #3
    martypapa is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default

    Hmmm that's similar to what I'm already doing, but I guess that's the way I'll do it then.
    Thank you!

  4. #4
    martypapa is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default

    Say if I had 20 buttons, would it be alright to use a thread for each of them? Do they take up much of the systems resources, (are there a limited number of threads I can use?)

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    why not just have 1 thread that uses a while(true) loop and an if statement for each button?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Rather than talk about a specific tactic of "listening to a variable", why not tell us your overall strategy here. Just what are you trying to achieve by this? Tell us this, and I'll bet we'll find a better way to solve it.

  7. #7
    martypapa is offline Member
    Join Date
    Feb 2010
    Posts
    21
    Rep Power
    0

    Default

    It's alright now, I've fixed it. I thought that testing lots of variables from my main JPanel in a loop was drastically slowing down my game. I went around in a circle and ended up back where I started - testing for variables in my main JPanel. It wasn't this that was slowing down my game, it was the screen fader.

    I had a large black rectangle who's alpha value decreased and was constantly being repainted. I fixed this up, so that it was only repainted if the alpha value changed. Now this went from chewing up 100% of my CPU to just 7%.

    I guess repainting takes up a heap more of the processor power than I thought. I would have thought though, that because this is a simple 2D game that it wouldn't take up so much.

    Am I doing something wrong, or is this just a limitation of Java?

Similar Threads

  1. MDB listening
    By nipunalk in forum Advanced Java
    Replies: 5
    Last Post: 01-20-2010, 05:10 AM
  2. NIO Server: listening on client's side socket
    By carek in forum Networking
    Replies: 1
    Last Post: 12-24-2009, 07:30 AM
  3. Replies: 7
    Last Post: 11-25-2009, 11:34 PM
  4. Java server listening on given interface?
    By Joe2003 in forum Advanced Java
    Replies: 2
    Last Post: 01-26-2009, 05:14 PM
  5. Listening to a Printer
    By Mosd in forum New To Java
    Replies: 0
    Last Post: 08-12-2008, 12:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •