Values of a float not updating in classes besides the one it is defined in :(

So I have been trying to fix this for nearly an hour now, but I am truly clueless… In my class MainGame i define 2 floats:

private float levelX = -500;
private float levelY = -500;

and I also define a getter for both of those. When you press the up arrow key the levelY is decreased (using libgdx so it may be irregular seeing something like this) And I do the same for the rest of my arrow keys. Now the problem is when I am trying to use the value of the levelX or levelY in another class it always returns as -500 even though if I print out the value in the MainGame class it is changed from that original -500 but not in the other class… Is this some stupid error I am not picking up on here or is it something deeper ? :frowning:

How are you incrementing/decreasing those values?

This is definitely a language-problem.

pretty much its just this for each key:


if ((Gdx.input.isKeyPressed(Keys.W))) {
					levelY -= playerSpeed;
					up = true;
				} else
					up = false;

where


playerSpeed = 2;

Are you also updating the values in the other class?


public float getLevelY()
{
System.out.println("getY called");
return levelY;
}

public float getLevelX()
{
System.out.println("getX called");
return levelX;
}

Then run game and see if those methods are called.

Im new to this so that’s all I can come up with.

@ra4king I never knew I had to do that in past experiences it automatically did that… i think :persecutioncomplex:

@gamerulf
both get statements are being called repeatedly when trying that

only 2 possible reasons for that:

  1. the values get reset between changing and getting

  2. you are changing the values in one instance and getting them from another instance
    print out the instance address in the key handler method and in the getters. i’m pretty sure they are not equal…

This would be a whole lot easier if you’d show us the classes.

I am in the process of cleaning all of this code up so here is the main class:
http://pastebin.java-gaming.org/7ab3539984a
and the class im calling it from:
http://pastebin.java-gaming.org/ab359489a48
The latter needs to be re organized so please ignore that for now although it should be readable

Before everything in my game was in one class for some stupid reason, so i am in this spring cleaning mode lol

I think you should pass levelX and levelY as an argument instead of creating a main instance. I feel like trouble awaits when you do that.

I tried doing that earlier and it worked, although i was really curious of why this was not working out

Passing it like an argument? I think im unsure of what an argument is.

Like “main.levelX” ? :stuck_out_tongue:

Err you seem to not understand OOP. I advise you to learn more about Java before making games.

In your code, you are never calling mainGame.update() inside Resource however you claimed to be calling it, which leads me to believe you are using a different instance in a separate class. This means you don’t understand how different instances are separate from each other.

It is like ra4king says. Because you have a new instance of ressource in GameMain and in Ressource and you also create an instance of its own at the very beginning here:

public class Resource {

   public int x, y;
   int width = 64;
   int height = 58;
   boolean drawResource = true;
   float resourceTimer = 0;
   GameMain mainGame = new GameMain(); // HERE

So you have two independent instances of GameMain.

@sparky83 you are definately right about that but how do I actually avoid creating a new instance of that? Do i have to use static getters / setters or something. I feel very foolish for not realizing this :emo:

I would base all game logic and rendering in separate classes than Main, and just use Main for the game loop.

but still, if i create one class containing Level with has levelX and levelY how do I acess those without creating new instances

Arguments, I guess. I don’t have a good enough grasp of your code base to suggest other alternatives.

I suggest you first do some Java tutorials as your questions are not related to game development but to Java and OO programming in general. A good place to start are the Java tutorials by Oracle:http://docs.oracle.com/javase/tutorial/

Does anyone have ideas on solving this? I feel like static Is a bad idea and in some cases arguments won’t solve this…