I recently (or, if you consider when I started, not all that recently) made a tcp server for a simple multiplayer game, as many people do. While I’m able to interact with this server normally and in an expected manner, I’m still stuck on connecting to it via Armory. I did some digging, and I found that the kha
package has (some) networking capability. I made the following trait (complete with intellectually stimulating print statements for debug):
//Manage.hx
package arm;
import haxe.Json;
import iron.Scene;
import haxe.io.Bytes;
import haxe.Timer;
import haxe.io.BytesData;
import kha.network.Session;
import kha.network.Network;
class Manage extends iron.Trait {
static var netBuff:Int=8192;
static var user:String ="haxer";
static var passwd:String ="I_have_a_life.";
static var s=new Session(1024,"192.168.2.165",1251);
static var commands:String;
static var readme:Dynamic;
static var first:Bool=true;
public function splitbychar(split:String,by:String):Array<String> {
var end:Array<String>=new Array<String>();
var endi:Int=0;
for (element in 0...split.length) {
if (split.charAt(element)==by) {
endi++;
} else {
end[endi]+=split.charAt(element);
}
}
return end;
}
public function new() {
super();
trace("Hello world");
s.network=new Network("192.168.2.165",1251,null,null);
notifyOnInit(function() {
});
notifyOnUpdate(function() {
s.update();
var buff:Bytes=Bytes.alloc(netBuff);
buff.fill(0,netBuff,0);
trace(buff);
trace("hi");
if (first) {
s.waitForStart(function(){trace(1);},function(){trace(2);},function(){trace(3);},function(){trace(4);},function(){trace(5);});
var tosend:Bytes=Bytes.ofString(passwd+" "+user+"@");
trace(tosend);
s.sendToServer(tosend);
trace("meh");
s.receive(buff,null);
trace("feh");
var ack=splitbychar(buff.toString(),"@")[0];
if (ack!="initack"){//I don't know how to exit, so this will have to do.
var crash=[0];
crash[1]=0;
}
first=false;
} else {
trace("cool");
//send stuff to server
s.sendToServer(Bytes.ofString(commands+"@"));
trace("double feh");
//read and deploy to trait so other haxe scripts can read from here
var incoming:String;
var incomingbytes:Bytes=Bytes.alloc(netBuff);
incomingbytes.fill(0,netBuff,0);
s.receive(buff,null);
incoming=buff.toString();
trace(incoming);
var args=splitbychar(incoming," ");
if (args.length>0) {
if (args[0]=="json") {
readme=Json.parse(args[1]);
for (element in Reflect.fields(readme.data)){
var ob:Dynamic=Reflect.field(readme.data,element);
var ignore:Bool=false;
for (objectgroup in Scene.active.groups) {
for (object in objectgroup) {
if (object.name==ob.Name){
ignore=true;
break;
}
}
}
if (!ignore) {
var obj:iron.object.Object=null;
obj.name=ob.Name;
Scene.active.addObject(obj);
}
}
}
}
//then input (fills commands)
}
});
// notifyOnRemove(function() {
// });
}
}
The output keeps giving ones, implying that there is a callback on waitForStart, and the server appears to be sending back nothing (surprise, surprise). I have a sneaking suspicion I’m going about this completely wrong, but I haven’t been at this long, so I was wondering if more experienced and competent people could point me in the right direction to simple tcp text exchange in Haxe.
If you haven’t guessed, I’m pretty new to Armory and Haxe.
Thanks in advance.