Can't read length of an OpenFL Vector

I have a strange error. My application compiles but gets a runtime error when trying to read the length of an OpenFL Vector. See comment below.

	private function set_pointData(data:Vector<Vector3D>):Vector<Vector3D>
		if (data.length < _pointsPerSegment) … //throws an error saying "Uncaught TypeError TypeError: data.get_length is not a function

In debug mode, when I break on that line I can see that the data consists of 46 entries and does have a length property. I’m compiling to JavaScript. I tried setting -dce to “no”. What could be the problem?

Does this work with another target? Otherwise there might be a bug in openfl’s Vector code.

There seems to be something about starting with an Array and casting it to Vector of the same length. Compiling to Hashlink fails with Uncaught exception: Can't cast hl.types.ArrayObj to openfl._Vector.ObjectVector but compiling to JavaScript works fine, and the casting works.

If I start with a Vector and populate that before passing to the function, things work fine. I have it working now.

You can’t cast from one class to another unrelated class, like you saw it doesn’t work.

1 Like

Works in JavaScript, but okay. :slight_smile:

What do you mean “works”? Unlike hashlink there’s no native compiler that’ll complain, but it clearly doesn’t work, you got a type error.

Haxe is permissive and lets you ignore its typing with force casting, but if you do something that doesn’t make sense you’ll have issues.

1 Like

What I did was create an Array and then called:
At runtime I set a breakpoint within the set_pointData function and inspected the “data” variable. It was populated as I expected in the debugger and did have a length. No error happened until later on in the function it attempted to read the length.

But you’re right, it doesn’t work at runtime.

Your casting says to haxe “this variable is a vector, don’t complain”.

Then at runtime it doesn’t fail because the array doesn’t have a length, but because it doesn’t have a get_length function like a Vector.

Would also have failed if you tried to access the fixed variable of the array pretending to be a vector, since it wouldn’t have one either.

1 Like

You can use Vector.ofArray() to convert from Array<T> to openfl.Vector<T>.

1 Like

Dang why does that look so familiar? Thanks!