Is there in Haxe any "global variable" function that other Haxe traits can have access?

Hello! Does anyone know if haxe has an “global var” function or something like this? I want to set an variable that other haxe trait can have access instead of making a lot of events.

1 Like

I think no, but use event??

If I understand correctly you want variable available to other traits.
Well, you can do it 2 way:

public var someVar = "lalala";

doing this way you will have to create new instance of class:

var someClass:<Class> = new SomeClass();
trace(someClass.someVar);

creating new instance of class will have it own value, that is, if you try to edit someClass.someVar than it will not edit the original var.

public static var someVar = "lalala";

doing this way, you will only have one instance. If you try to edit it from other trait:

var someClass:<Class> =  SomeClass;
someClass.someVar = "newlalalal";

than the original var will also be edited.

So in short, to make var accessible by other trait use public and if you want other trait to able to edit origin var than add static after public, else if you want other trait to create new instance than no need to add static. Same goes with function.


Example:

  1. Case 1: We want to add player or any other object during runtime and we want var/function to be accessible by other trait/scripts, then we will use public.
  • Reason: We want player or any other to have their own properties/instances and when we try to edit their props than it should not effect others.
  1. Case 2: We want to add score board, and we want scores to able to edit by other trait than we will use public static.
  • Reason: We don’t want other trait to have their own instance of scoreboard (imagine basketball player running around with their own scoreboards).
4 Likes

Hey @BlackGoku36! That’s amazing! I didn’t know how to use the public or the static var, I only worked with the regular “var” but now you helped me out. Thank you man!:smiley::+1:

1 Like