Error with the "Hello World" tutorial.



  • @dean Awesome! One question, though.

    This is actually something I've never been able to do in 'any' programming language. I've always been pretty horrid when it comes to multi-class objects, (in C++ I did the horrible thing and used global ints.)

    How would I, be able to take the string that was generated via the input in that class, and use it in another?
    First attempt revolved around trying something like "private var charName:NameSelectState;" but that doesn't seem to work. Searching around results in nothing. >.<

    If it's impossible, I'll just see if I can find a weird way around it.



  • @vulpicula

    You can define it public static var myVar:String and it will be available in other classes. Or you can pass it to another class as an argument through the new() constructor.

    public static var myInputText:String;
    

    Whatever class you define it in, let's say you define it in PlayState, you access from other classes with

    PlayState.myTextInput;
    

    Usually I keep these static vars in Reg.hx and then access them anywhere with

    Reg.myTextInput;
    

    public static function myFunction() works the same way. Use it in other classes with

    Reg.myFunction();
    

    If you want to pass it as an argument to a class it won't need to be public static.

    private var _inputString:String;
    
    public function new(inputString:String) 
    {
    	super();
    	
    	_inputString = inputString;
    }
    

    and _inputString is available in the class you passed it to.



  • @dean Awesome. And I assume, in theory, if I wanted to at one point change the string, I could just drop the "static" part from the variable creation thing?

    Got it. ^^

    Hopefully this doesn't have the same error as C++, where it'd behave strangely.

    (Such as in, if I set an integer like this to 5 in one class, it'd only show 5 in the class I changed it to, and not show the change in any of the other classes, if that makes any sense.)



  • @vulpicula

    well, static allows access with MyClass.myVar and it is static.

    If you make it public var myVar:String without static, then you access it with

    var myObject = new MyClass();
    var objString = myObject.myVar;
    


  • @vulpicula

    static means it is one instance and in memory. If you change it in one class, the value is changed and other classes use the same instance.

    If you don't want that, you can make it public var and if you have a class for a Barrel, and you add many, those public (non static) vars are unique in each Barrels you add.

    Hope that makes sense.

    private var is only available in the class it's defined in.

    public var is available in other classes but is unique to each class instance for a reused class.

    class Barrel extends FlxSprite
    {
    	public var value:Int;
    
    	public function new(x:Float = 0, y:Float = 0) 
    	{
    		super(x, y);
    	}
    }
    

    ...

    var barrel = new Barrel();
    barrel.value = 10;
    
    var barrel2 = new Barrel();
    barrel2.value = 20;
    

    or you can pass the value in new() as argument and set the public var.

    class Barrel extends FlxSprite
    {
    	public var value:Int;
    
    	public function new(x:Float = 0, y:Float = 0, value:Int) 
    	{
    		super(x, y);
    		
    		// 'this' is needed to distinguish between the public var value and the argument.
    		this.value = value;
    	}
    }
    
    var barrel = new Barrel(50, 50, 10);
    trace(barrel.value); // 10
    
    var barrel2 = new Barrel(100, 50, 20);
    trace(barrel2.value); // 20;
    

    public static var is available anywhere and is only one instance and accessed with ClassName.myVar.



  • @dean Ah, I see, so most of the time I should be using static variables. Gotcha.



  • @vulpicula

    If you want them static, yes :)



  • @Vulpicula

    This topic went off topic. Probably should break down the questions to new topics next time to make it easier for others to find answers.

    You got me with the 'just one question though...', a couple of times :)


  • administrators

    @vulpicula said in Error with the "Hello World" tutorial.:

    so most of the time I should be using static variables

    No, that's usually considered bad practice actually..

    Odd. Even after setting it to use FlashDevelop via the setup, it still insists on running it for Visual Studio Code. (Or more specifically, it defaults to that every time I restart the CMD.)

    That is odd... Sounds like the config is not being saved or something along those lines. Fyi, you can override the configured IDE by using passing the -ide argument, e.g. flixel tpl -name "HelloWorld" -ide fd



  • @dean Yeah, sorry, that's a habit of mine. >.<

    I'll do it in the future.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.