Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (115)
games submitted by our members
Games in WIP (562)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  What is wrong with me?  (Read 3650 times)
0 Members and 1 Guest are viewing this topic.
Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Posted 2012-08-12 03:03:30 »

I was working on my game engine, and it has a "Game" class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
public abstract class Game
{
   
   public Game()
   {
      setup(); //If you call this method here, It will only initialize the variables to their default value
  }
   
   protected void setup()
   {
     
   }

}

and then I have my game's class, which extends Game.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public class MyGame extends Game
{
   private boolean useShaderPipeline = true;
   
   public MyGame()
   {
      //if you call setup here, and not from the super constructor, everything works ok
  }
   
   protected void setup()
   {
      System.out.println(useShaderPipeline); //Prints false
     useShaderPipeline = true;
      System.out.println(useShaderPipeline); //Prints true
  }
   
   public static void main(String args[])
   {
      new MyGame();
   }

}


Ok, looks good so far. But wait... why does the println's return 0 and false respectively? It looks like they are at their default values. However if you initialize them inside of a method, they work fine.
My best guesses are:
1. I fell asleep in the java class where they told me about this. //HIGHLY LIKELY.
2. There are aliens in my computer.
3. This is a bug, or a corrupt JVM.

What are your thoughts/suggestions to resolve this?

P.S. If you hit tab while typing up up a post, it focuses you on the search bar. Sort of annoying when you are formatting code Cheesy (Just my $0.02)
Offline coltonoscopy

Junior Member


Medals: 2



« Reply #1 - Posted 2012-08-12 03:11:58 »

I was working on my game engine, and it has a "Game" class.
1  
2  
3  
4  
5  
6  
7  
8  
9  
public abstract class Game 
{  
       protected int stupidVariable1;
       
       public void doSomething()
       {

       }
}

and then I have my game's class, which extends Game.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public class MyGame 
{  
       private int anotherVariable = 1;
       private boolean useShaders = true;
       
       public MyGame()
       {
              doSomething();
       }
       public void doSomething()
       {
              System.out.println(anotherVariable);
              System.out.println(useShaders);
       }

       public static void main(String args[])
       {
              new MyGame();
       }
}


Ok, looks good so far. But wait... why does the println's return 0 and false respectively? It looks like they are at their default values. However if you initialize them inside of a method, they work fine.
My best guesses are:
1. I fell asleep in the java class where they told me about this. //HIGHLY LIKELY.
2. There are aliens in my computer.
3. This is a bug, or a corrupt JVM.

What are your thoughts/suggestions to resolve this?

P.S. If you hit tab while typing up up a post, it focuses you on the search bar. Sort of annoying when you are formatting code Cheesy (Just my $0.02)
With the code you gave me, I get 1 and true on my end. Is there something we're missing that you're not providing?

EDIT: Also, this thread seems like it would have been better suited for the "Newbie and Debugging" forum. Not that you're a newbie! Smiley

Colton

Straight flippin.
Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Reply #2 - Posted 2012-08-12 03:24:41 »

Ok, Just tried my code. That works on my machine too, but the original code does not. Huh I don't know what I am holding back, but apparently something. Do you want me to pastebin my complete source?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ReBirth
« Reply #3 - Posted 2012-08-12 03:27:45 »

Yes, it's interesting.

Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2012-08-12 03:27:52 »

Don't ask, just do it Smiley

Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Reply #5 - Posted 2012-08-12 03:29:26 »

I will post it tomorrow. Off to bed, after I request conversion.
Offline ReBirth
« Reply #6 - Posted 2012-08-12 03:31:25 »

I will post it tomorrow. Off to bed, after I request conversion.
Well this is anti-climax Grin

Offline theagentd
« Reply #7 - Posted 2012-08-12 11:53:46 »

I think I've been affected by this. Well, I can't reproduce it either though. xD

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 799
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #8 - Posted 2012-08-12 12:08:33 »

Once every few months, Eclipse just stops compiling for no reason whatsoever, and you need to close & open the fileyou're editing, or restart Eclipse. Other cuases can be that there are issues with your build-path, which also stops the compiler (for good reason).

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #9 - Posted 2012-08-12 12:50:25 »

Once every few months, Eclipse just stops compiling for no reason whatsoever, and you need to close & open the fileyou're editing, or restart Eclipse. Other cuases can be that there are issues with your build-path, which also stops the compiler (for good reason).
Well, that makes sense. >_>

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline delt0r

JGO Knight


Medals: 27
Exp: 18 years


Computers can do that?


« Reply #10 - Posted 2012-08-12 13:09:44 »

I have never managed to have eclipse going for a month without having to restart long before then.

However more on topic i have had eclipse stop properly compiling and  a simple restart was not enough. I had to "reimport" the project that was affected. ie delete the project then create the project again.

This has happened only twice.

Either way when i am a bit stuck i will always put a "is compiling/running this source" check in. Like a time stamp in the logs for example.

I have no special talents. I am only passionately curious.--Albert Einstein
Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Reply #11 - Posted 2012-08-12 13:27:03 »

Ok, I have done some debugging, and have found the cause of the error. It seems that if you have a protected method in the superclass, and you call it in it's constructor, it seems to just skip initializing the variables. However, if you call it in the subclass, it works great. I updated the code and it produces the error after a restart, and even after putting it in a new file. Also, I checked my build path, everything looks good, and it compiles fine.
Offline Gudradain
« Reply #12 - Posted 2012-08-12 13:52:23 »

Once every few months, Eclipse just stops compiling for no reason whatsoever, and you need to close & open the fileyou're editing, or restart Eclipse. Other cuases can be that there are issues with your build-path, which also stops the compiler (for good reason).

Agree, it happens to me a lot of times actually. Most of the time it was when I was working with some plug-ins; different version control and maven if I remember correctly. I have no idea why :S I tried everything, build, clear and build, editing and resaving file. Nothing would work. I had to restart eclipse.
Offline UprightPath
« Reply #13 - Posted 2012-08-12 14:00:54 »

Always restart Eclipse. Sit down at the desk? Better restart Eclipse. Alt+tab to another window? Better restart Eclipse.

Think like Bear Grylls, except with Restarting Eclipse... Not what he is known for. Ahem.

Anyway, I've gotten to the point where I have to do it every few hours because it stops recognizing imports and marks everything with errors. Or it stops prompting auto-commits. All sorts of fun.

Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Reply #14 - Posted 2012-08-12 14:03:04 »

I restarted! And... it still doesn't work properly. Cheesy
Offline _Al3x

Senior Member


Medals: 7


Indie Games FTW!


« Reply #15 - Posted 2012-08-12 14:13:18 »

Are all of you working under Linux / Mac OS X?
I'm on win 7 (64bit and 32bit) and NEVER had to restart Eclipse. EVER in over 3 years Smiley

Offline jonjava
« Reply #16 - Posted 2012-08-12 14:14:21 »

Eclipse works like a like a rainbow unicorn for me. :3

Try rebooting your PC.

Offline UprightPath
« Reply #17 - Posted 2012-08-12 14:19:24 »

Nope, Win7 64! And Eclipse crashes on me about once a day or so! Though, it could be that my AV's a bit bitchy about things...

Oh! Ah... Should I tell you what your problem is? Because I think I know it. >.>

It's an inheritance issue. The code that you have up there? Try changing...
1  
2  
3  
4  
5  
6  
protected void setup()
   {
     
   }
to
protected abstract void setup();


Then say whether it works right. :3 If it does, I can explain why! :3

Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Reply #18 - Posted 2012-08-12 14:44:30 »

Are all of you working under Linux / Mac OS X?
I'm on win 7 (64bit and 32bit) and NEVER had to restart Eclipse. EVER in over 3 years Smiley
I am working on Mac OS X Lion, and I don't have to restart too often. (1-2 months in between restarts).

Eclipse works like a like a rainbow unicorn for me. :3

Try rebooting your PC.
Rebooted, same problem. Also, it appears that the problem occurs system-wide, as my older eclipse(Indigo, kept it just in case in the upgrade to Juno screwed things up) has the same issue.
Nope, Win7 64! And Eclipse crashes on me about once a day or so! Though, it could be that my AV's a bit bitchy about things...

Oh! Ah... Should I tell you what your problem is? Because I think I know it. >.>

It's an inheritance issue. The code that you have up there? Try changing...
1  
2  
3  
4  
5  
6  
protected void setup()
   {
     
   }
to
protected abstract void setup();


Then say whether it works right. :3 If it does, I can explain why! :3
No, it doesn't. Sad Darn...I was hoping I fell asleep in Java 4 Newbs 101.Wink That would make everything a lot easier......
Offline ra4king

JGO Kernel


Medals: 345
Projects: 3
Exp: 5 years


I'm the King!


« Reply #19 - Posted 2012-08-12 15:08:42 »

Eclipse works like a charm for me. Never any problems Smiley

Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Reply #20 - Posted 2012-08-12 15:09:13 »

Gee, Thanks Cheesy
Offline UprightPath
« Reply #21 - Posted 2012-08-12 15:40:28 »

OH!

Oh. Oh. Derp.

Order of calls! That is the issue here. What is happening, my fine friend is this:
1) MyGame Constructor is entered.
2) Game Constructor is entered.
3) Game initializer is called.
4) MyGame.setup is called.
5) MyGame.setup exits.
6) Game Constructor exits.
7) MyGame initialized is called.

Before anything except declaration (Not assignment) happens, the super class's constructor is called. After that returns, the assignment is allowed to happen.

Offline UprightPath
« Reply #22 - Posted 2012-08-12 15:55:03 »

Just the proof~
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
public abstract class Game
{
   
   public Game()
   {
      System.out.println("Got here: Game");
      setup(); //If you call this method here, It will only initialize the variables to their default value
  }
   
   protected void setup()
   {
     
   }

}

public class MyGame extends Game {
   private String initializerTest = "MyGame!";
   private boolean useShaderPipeline = true;

   public MyGame() {
      System.out.println("Got here: MyGame");
      System.out.println(initializerTest);
   }

   protected void setup() {
      System.out.println(initializerTest); // Prints false
     initializerTest = "SetUp!";
      System.out.println(initializerTest); // Prints true
  }

   public static void main(String args[]) {
      new MyGame();
   }

}

Outputs
1  
2  
3  
4  
5  
Got here: Game
null
SetUp!
Got here: MyGame
MyGame!

Offline theagentd
« Reply #23 - Posted 2012-08-12 17:16:51 »

@UprightPath
Ah. Jesus. That fits perfectly with my test class. It calls setup in the constructor as well.

Myomyomyo.
Offline UprightPath
« Reply #24 - Posted 2012-08-12 18:51:43 »

@UprightPath
Ah. Jesus. That fits perfectly with my test class. It calls setup in the constructor as well.

Er, does it? More askin' 'cause for some reason I didn't see a post by you with code in here. Heh. Sorry if it seemed like I was thunder stealin' or anything.

Either that, or I misunderstood what you just said. I don't know which and should probably go to sleep then. Hah!

Offline OpenGLShaders

Junior Member


Medals: 4
Exp: 2 years



« Reply #25 - Posted 2012-08-12 20:04:25 »

OH!

Oh. Oh. Derp.

Order of calls! That is the issue here. What is happening, my fine friend is this:
1) MyGame Constructor is entered.
2) Game Constructor is entered.
3) Game initializer is called.
4) MyGame.setup is called.
5) MyGame.setup exits.
6) Game Constructor exits.
7) MyGame initialized is called.

Before anything except declaration (Not assignment) happens, the super class's constructor is called. After that returns, the assignment is allowed to happen.

Derrrrrrrrppppppp. That was a significant issue, as my whole game ran in the constructor like:

1  
2  
3  
setup();
run();
exit();


But it's fixed now, with adding a simple start() method. Thank you everyone for helping me. Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (21 views)
2014-09-21 02:42:18

BurntPizza (15 views)
2014-09-21 01:30:30

moogie (18 views)
2014-09-21 00:26:15

UprightPath (25 views)
2014-09-20 20:14:06

BurntPizza (27 views)
2014-09-19 03:14:18

Dwinin (43 views)
2014-09-12 09:08:26

Norakomi (73 views)
2014-09-10 13:57:51

TehJavaDev (97 views)
2014-09-10 06:39:09

Tekkerue (49 views)
2014-09-09 02:24:56

mitcheeb (70 views)
2014-09-08 06:06:29
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!