Explanation iterator like .forEach(); from AS3 - Quake and Half-Life on Haxe


#1

Hello everyone, I would like to know about unsupported iterator from AS3
Example:

var directories:Vector.<Object>;
directories.forEach(function(val:Object, n:int, dir:Vector.<Object>):void
{
  ...
});

I want convert to haxe:

private function forEachDyn<T>(fromVecDyn:Array<Dynamic>, fn:Dynamic->Int->T):Void
{
	for (i in 0...fromVecDyn.length) fn(fromVecDyn[i], i);
}

In Main.hx or byte from BitmapData

var byte:ByteArray = new ByteArray();
byte.position = 0;
var myVec:Array<Dynamic> = new Array<Dynamic>();
forEachDyn(myVec, function(val:Dynamic, n:Int):Void
{
	myVec[n] = {
		offset:byte.readInt()
		
	}
});

Yay It works fine.
You know Initial Quake with Away3D or OpenGLView into real Quake like Quake1 and Half-Life work under Haxe me and MadGypsy have tried to resolve to Haxe/OpenFL/Lime with Away3D 5.x
But we have tried to resolve for Haxe.
You know forEach under AS3 and it is very slow and how is Haxe if it is fast via “->T” via custom-iterator.

Thanks for explanation and you can write suggestions
Yeah I am really happy because I can implement custom iterators.
Never forget that! QuakeOne Member said me: Don’t use “Vector” just “Array”

I hope you have to get own custom-iterators - If you have .forEach(function():Void { …});


#2

Please keep us updated, this sounds exciting :slight_smile:


#3

Lambda.foreach() lets you run a function on each item in an array.

var directories:Vector<Dynamic>;
var n:Int = 0;
directories.forEach(function(val:Dynamic):Bool
{
	trace(val);
	trace(n);
	n++;
	
	//Return true to keep going.
	return true;
});

Lambda.mapi() lets you overwrite all the values in a given array. It even gives you the index in case you need it.

var byte:ByteArray = new ByteArray();
byte.position = 0;
var myVec:Array<Dynamic> = new Array<Dynamic>();

for(i in 0...byte.length) myVec.push(null);

myVec = Lambda.mapi(myVec, function(val:Dynamic, n:Int):Dynamic
{
	trace(n);
	return { offset:byte.readInt() };
});

Array comprehension is like Lambda.mapi(), but with less code.

var byte:ByteArray = new ByteArray();
byte.position = 0;
var myVec:Array<Dynamic>;

myVec = [while(byte.bytesAvailable > 0)
	{ offset:byte.readInt() }];

#4

Thanks for help. I didn’t see any call Lambda before. I think Haxe dpesn’t support with forEach();

Now you have to say Lambda - I am very longer searching to google. But it finds hard If I use “forEach for Haxe” than they are nothing current to find. just only for loop of Haxe or AS3.

Please add Lambda to Actionscript developer guide! I will know about same conversion of AS3 and Haxe. I feel stupid because I search longer before. I thought because Haxe has not more features. Now I am surprised you have same features. I am shocking now. :astonished:

PS: Thanks for great support to @player_03!!!

// EDIT:
And I do not understand why I got errors.
> private var events(get, set) : Array;
> private var _events : Array;
> public function new()
> {
> super();
>
> //default: events you would expect when using an URLLoader (load external files)
> _events = [Event.COMPLETE, ProgressEvent.PROGRESS, IOErrorEvent.IO_ERROR];
> }
> /** ADD_LISTENERS
> * add all events to an object with a loop
> * @param target: the object in which to add the listeners to
> /
> private function addListeners(target : Dynamic, evts : Array = null) : Void
> {
> _events = ((evts == null)) ? _events : evts;
> var n:Int;
> Lambda.foreach(_events, function(value:String):String
> {
> if (!target.hasEventListener(value))
> target.addEventListener(value, event_handler);
> return _events[n] = value;
> });
> }
> /
* DROP_LISTENERS
> * remove all events from an object with a loop
> * @param target: the object in which to remove the listeners from
> */
> private function dropListeners(target : Dynamic, evts : Array = null) : Void
> {
> _events = ((evts == null)) ? _events : evts;
> var n:Int;
> Lambda.foreach(_events, function(value:String):String
> {
> if (target.hasEventListener(value))
> target.removeEventListener(value, event_handler);
> return _events[n] = value;
> });
> }

And output: Lambda.foreach(function());
Error:
> src/io/events/EventManager.hx:52: lines 52-57 : value : String -> String should be String -> Bool
> src/io/events/EventManager.hx:52: lines 52-57 : Cannot unify return types
> src/io/events/EventManager.hx:52: lines 52-57 : String should be Bool
> src/io/events/EventManager.hx:52: lines 52-57 : For function argument 'f'
> src/io/events/EventManager.hx:68: lines 68-73 : value : String -> String should be String -> Bool
> src/io/events/EventManager.hx:68: lines 68-73 : Cannot unify return types
> src/io/events/EventManager.hx:68: lines 68-73 : String should be Bool
> src/io/events/EventManager.hx:68: lines 68-73 : For function argument 'f'

How do I fix foreach?

// EDIT: I have fixed but array won’t work

 src/wad/WAD3.hx:134: lines 134-145 : pal : Array<Int> -> Bool should be Int -> Bool
src/wad/WAD3.hx:134: lines 134-145 : Cannot unify argument 1
src/wad/WAD3.hx:134: lines 134-145 : Int should be Array<Int>
src/wad/WAD3.hx:134: lines 134-145 : For function argument 'f'
Build halted with errors.

My code is from WAD3.hx

Lambda.foreach(palette, function(pal:Array<Int>):Bool
{
	var val:Int, i:Int;
	alpha = (((i == (MAX_PALETTE_COLORS - 1)) && (dir[n].name.charAt(0) == "{"))) ? 0x00 : 0xFF;
	
	if (dir[n].type == 0x40) 
		pal[i] = (alpha << 24 | i << 16 | i << 8 | i)
	else 
		pal[i] = (alpha << 24 | bytes.readUnsignedByte() << 16 | bytes.readUnsignedByte() << 8 | bytes.readUnsignedByte());
		
	return true;
});

I already tried - it always give errors? I don’t understand I already fixed _events and directories both foreachs are work fine but why not with Array.

How do I fix? Possible I use Vector?
I fixed Lambda.mapi();
Than it always crash :frowning: Because ( I already checked:

Called from hxcpp::__hxcpp_main
Called from ApplicationMain::main ApplicationMain.hx line 79
Called from ApplicationMain::create ApplicationMain.hx line 111
Called from lime.app.Application::exec lime/app/Application.hx line 225
Called from lime._backend.native.NativeApplication::exec lime/_backend/native/NativeApplication.hx line 154
Called from lime._backend.native.NativeApplication::handleApplicationEvent lime/_backend/native/NativeApplication.hx line 195
Called from lime.app._Event_Int_Void::dispatch lime/_macros/EventMacro.hx line 101
Called from lime.system.ThreadPool::__update lime/system/ThreadPool.hx line 206
Called from lime.app._Event_Dynamic_Void::dispatch lime/_macros/EventMacro.hx line 101
Called from lime._backend.native.NativeHTTPRequest::threadPool_onComplete lime/_backend/native/NativeHTTPRequest.hx line 414
Called from lime.app.Promise_haxe_io_Bytes::complete lime/app/Promise.hx line 39
Called from lime.net._HTTPRequest_Bytes::load lime/net/HTTPRequest.hx line 124
Called from lime.app.Promise::complete lime/app/Promise.hx line 39
Called from openfl.net.URLLoader::load openfl/net/URLLoader.hx line 74
Called from openfl.events.EventDispatcher::dispatchEvent openfl/events/EventDispatcher.hx line 97
Called from openfl.events.EventDispatcher::__dispatchEvent openfl/events/EventDispatcher.hx line 218
Called from io.events.EventManager::event_handler io/events/EventManager.hx line 83
Called from openfl.events.EventDispatcher::dispatchEvent openfl/events/EventDispatcher.hx line 97
Called from openfl.events.EventDispatcher::__dispatchEvent openfl/events/EventDispatcher.hx line 218
Called from io.Importer::event_handler io/Importer.hx line 113
Called from Main::handle Main.hx line 41
Called from wad.WAD3::parse wad/WAD3.hx line 49
AL lib: (EE) alc_cleanup: 1 device not closed

Wow it crashes So sad how do I fix if I want openfl let to works Quake and Half-Life…


#5

Now I got always “Error: Null Object Reference”

I tried to fix. No success.
If you can resolve my project if you know.
If you want download AS3 project by Madgygsy than you can download my Download zip PS: If you have wad file from Steam Half-Life than you can copy halflife.wad to export/cpp/release/bin/

If I don’t see if I have packed wad to. Don’t worry - If you didn’t buy Steam Half-Life or you have not account of Steam. Than it is okay… If you want see name of texture. I recommend you that Nem’s tools can open wad. like BSP viewer or Crafity or otherr program Or Wally 1.5.5b

Is it possible I should “String->Void” or “Event->Void”?
I need all Vectors. to Array?

// EDIT website of quakeone.com is still failed of down. I give download of AS3 project by MadGypsy <- If you generate to project than it loads very slow… :frowning: That is why I wish I want see how does it work for Haxe if it loads faster than AS3.

Thanks for resolve!