[solved] Changing text value hangs heaps


#1

Hello,

I’m using Heaps 1.5 , with HL 1.8 , hlsdl 1.8, and haxe4p5.

I’m trying to change a text displayed with Text class.
The first call to print method works well.
But next call to print (in update) hangs heaps with:

Null access
Called from Main.update (Main.hx line 48)
Called from hxd.App.mainLoop (hxd/App.hx line 161)
Called from local function #813 (hxd/App.hx line 118)
Called from hxd.App.loadAssets (hxd/App.hx line 144)
Called from hxd.App.setup (hxd/App.hx line 114)
Called from h3d.Engine.onCreate (h3d/Engine.hx line 240)
Called from local function #1187 (h3d/impl/GlDriver.hx line 1505)
Called from local function #1402 (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/Timer.hx line 140)
Called from local function #1403 (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/Timer.hx line 74)
Called from haxe.$MainLoop.tick (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/MainLoop.hx line 166)
Called from hxd.$System.runMainLoop (hxd/System.hl.hx line 124)
Called from local function #1402 (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/Timer.hx line 140)
Called from local function #1403 (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/Timer.hx line 74)
Called from haxe.$MainLoop.tick (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/MainLoop.hx line 166)
Called from haxe.$EntryPoint.processEvents (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/EntryPoint.hx line 104)
Called from haxe.$EntryPoint.run (~/haxelib/haxe4/haxe_20181012132630_7eb789f//std/haxe/EntryPoint.hx line 136)
Killed

The null access puzzles me. But if texte wasn’t an existing object , due to scoping for example, it wouldn’t compile. I suspect anyway a bug due to my low Haxe knowledge.
If someone has five minutes for seeing this, that would be very kind.
Here is the infamous Main.hx.

import h2d.Bitmap;
import h2d.Object;

class Text
{
  private var font:h2d.Font;
  private var tf:h2d.Text;
  private var parent:Object;

  public function new( parent:Object, ?fontname:String)
  {
   font = hxd.res.DefaultFont.get();
   tf = new h2d.Text(font);
   this.parent = parent;
   parent.addChild(tf);
  }

  public function print( text:String, x:Int,y:Int)
  {
    tf.text = text;
    tf.textAlign = Center;
    tf.x = x;
    tf.y = y;
  }
}


class Main extends hxd.App
{
    static function main()
    {
        new Main();
    }

    private var texte:Text;

    override function init() 
    {
        var texte = new Text( s2d);
        texte.print("Hello World\nLigne suivante",100,10);
    }

    override function update(dt:Float) 
    {
          texte.print("i have changed", 100,10); // the line 48 mentionned in the bug report above.
    }
}

Thank you for watching.


#2

texte is null at the update function. The compiler and heaps are working properly.

The problem is that the private var texte:Text; at the class level is shadowed (hidden by) the var keyword in the init function. That is, var in var texte = new Text(s2d); creates a new variable, which is initialized and used for the rest of the function, but goes out of scope when the function completes.

Stated differently, you created two variables with the same name. Remove the var from the initialization line in the init function and things will work like you expect them to.

-Eric


#3

Ooohhh… the horrible mistake !

I’m going to take the time to install VSCode with its haxelink debugger, it will be time well spent i think.

Thank you for your answer.