Set a class variable from another class?

Been searching and have not been able to find an aswer.

I am trying to set a value in a class from another class. Tried different ways of doing it by doing it the way I know from other languages but I cannot seem to be able to it in OpenFL

basically I have class like this

var myClass:Newclass = new Newcllass;
myClass.someVar = "some value";

then the class is something like this:

class Newcllass extends Sprite{
     public var someVar:String;

     public function getSomeVarValue(){
           trace(someVar);
    }

}

This returns null. Can someone point me in the right direction please?

hi, your error is in your first line, it should read

        var myClass:Newcllass = new Newcllass();

along with

        myClass.someVar = "some value";
        myClass.getSomeVarValue();

Hi thanks for the response. How would I go about accessing the variable from inside the class iself?

this is the actual code I am using to create the class:

class Main extends Sprite {
..
public var gameHelpers:Game;

public function new () {
...
game = new Game();
gameHelpers.selectedPlayer = "Johnie";

in the game class I have tried this:

@:isVar public var selectedPlayer(get, set):String;
function get_SelectedPlayer(){
return selectedPlayer;
}

and this

var selectedPlayer:String;

and simply this from inside different functions

trace(selectedPlayer)

None seem to work.

Since I was not able to assign a value to the class inside the Game class directly like so:

gameHelpers = new Game(); gameHelpers.selectedPlayer = "Johnie";

I ended up doing it this way:

gameHelpers = new Game();
gameHelpers.setPlayer("Johnie");

hopefully this helps someone with a similar issue.

It seems that you need to understand more about OOP itself. Haxe manual (https://haxe.org/manual/introduction.html) will help you with all of the things you are asking here.

First of all, you are not trying to access variable in class, but in object here. If you want your class to have variables, you need to use word static.

Basic idea: http://try.haxe.org/#Eb3Ed

I have the type defined in the class it is set like this:

public var selectedPlayer:String;

I understand that. But that is not my question. I ended up creating a function to handle the setting of the variable. I was simply trying to assign that variable a value by doing this:

gameHelper.selectedPlayer = "Johnie";

instead I did:

gameHelper.setPlayer("Johnie");

in the game class I have a function:

public function setPlayer(player){
		this.selectedPlayer = player;
		trace(this.selectedPlayer);
}

I am now able to use the value.

But thanks for the answer.

I don’t understand. This is the most basic stuff, there is no reason it shouldn’t work:
http://try.haxe.org/#e2eE9

The only situation this could fail is if you do not create gameHelper object first, otherwise it must work.

As wildfireheart said, this is correct.

However, if you try to access selectedPlayer before setting selectedPlayer, it won’t work. Consider this code: at first it prints “null” because selectedPlayer hasn’t been set, but later, it prints the correct value.

Ok let me clear this up:

class Game extends Sprite {
      public var selectedPlayer:String;
      
     public function new(){
         super();
         makePlayer();
     }

    public function makePlayer(){
         trace(selectedPlayer);// this was returning null
    }

}

from my Main class I was trying to set it like this:

gameHelpers = new Game();
gameHelpers.selectedPlayer="Johnie";
addChild(gameHelpers);

to get around the null issue I added the function in the game class to set the value of selectedPlayer

from main I now do this:

class Main extends Sprite{
...
public function someFunction(){
       gameHelpers = new Game();
       gameHelpers.setPlayer("Johnie");
   }
}

then in the game Class

class Game extends Sprite {
    ...
    public function setPlayer(player){
    		
    		this.selectedPlayer = player;
    		trace(this.selectedPlayer);
    	}
    }

I am now able to use the value of selectedPlayer.

    public function new(){
         super();
         makePlayer();
     }

    public function makePlayer(){
         trace(selectedPlayer);// this was returning null
    }

That’s exactly what I was talking about. Let’s look at the order of events.

  1. You start with gameHelpers = new Game(). This is the “new()” function (also known as the constructor) for the Game class.
  2. The constructor calls super(). This isn’t relevant, but I’m trying to be thorough.
  3. The constructor calls makePlayer().
  4. makePlayer() prints the value of selectedPlayer. Since you haven’t assigned a value to selectedPlayer yet, it prints “null.”
  5. makePlayer() ends, bringing us back to the constructor.
  6. The constructor ends, bringing us back to the Main class.
  7. The Main class sets gameHelpers.selectedPlayer="Johnie".
  8. selectedPlayer is now the correct value.

Your problem is that you’re calling makePlayer() in step 3 rather than step 8. If you called it in step 8, it would print “Johnie.”

1 Like

I can see what you are saying but how can I assign a value to class that has not been instantiated yet? Which is what I was trying to do here:

gameHelpers = new Game();
gameHelpers.selectedPlayer="Johnie";
addChild(gameHelpers);

I would think that this is not possible

gameHelpers.selectedPlayer="Johnie"; // cannot assign a value to a class that has not been created;
gameHelpers = new Game();
addChild(gameHelpers);

I may be misunderstanding what you are saying

Nevermind I tested out what you were saying it makes sense now. Thanks a lot for your help!

Nope nevermind still returning null.

Try adding an argument to your constructor, like so:

    public function new(initialPlayer:String) {
        super();
        selectedPlayer = initialPlayer;
        makePlayer();
    }

If you want to use selectedPlayer during the constructor, then the solution is to set it during the constructor.

1 Like