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.
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
public, else if you want other trait to create new instance than no need to add
static. Same goes with function.
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
- 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.
Case 2: We want to add score board, and we want scores to able to edit by other trait than we will use
- Reason: We don’t want other trait to have their own instance of scoreboard (imagine basketball player running around with their own scoreboards).
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!