@name 
@inputs 
@outputs Self NetS:string
@persist NetS:string NetA:array NetT:table Self Off Test Net
@trigger all
interval(1)
Group="Network"
gSetGroup(Group)
if(first()){timer("test",1) Net=1 Test=1}

signalSetGroup(Group)
Scope=0
Alive="Alive"
Death="Death"
signalSend(Alive,Scope)
signalSetOnRemove(Death,Scope)
runOnSignal(Alive,Scope,1)
runOnSignal(Death,Scope,1)


EID=entity():id()
NetMsg="ID"
Brk="<"
NetS=gGetStr(NetMsg)
NetOff=1
if(NetOff){
    gDeleteStr(NetMsg)}
    
if(Off){
if(clk("test")){
    print("Net="+Net)
    NetA=NetS:explode(Brk)
    if (!entity(NetA:number(Net))){NetA:remove(Net)}
    if (Net<NetA:count()){Net++ timer("test",1)}else{Test=0}
    NetS=NetA:concat(Brk)
    gSetStr(NetMsg,NetS)
}}#off ends

if (!Self){
    
 #NetS:replace(Brk+EID,"") print("deleting")
    NetA=NetS:explode(Brk)
    NetC=NetA:count()
    Self=NetC
    NetA:setNumber(NetC+1,EID)
    #NetT:setNumber(toString(EID),Self))
    NetS=NetA:concat(Brk)
    gSetStr(NetMsg,NetS)
    print(NetA:concat(Brk))
    print(toString(Self))
    #print(toString(NetT:number(EID)))
}

if (signalClk(Group,Alive,Scope)){
    Sender=signalSenderId()
    Here=NetS:find(toString(Sender))
    if(!Here&(signalName()==Alive)){
        NetA=NetS:explode(Brk)
        NetC=NetA:count()
        NetA:setNumber(NetC+1,Sender)
        NetT:setNumber(toString(Sender),NetC)
        NetS=NetA:concat(Brk)
        gSetStr(NetS)
        print(NetA:concat(Brk))
        print(toString(Self))
        print("I'm running too!")
}}

    if(signalClk(Group,Death,Scope)){
        Sender=signalSenderId()
        Here=NetS:find(toString(Sender))
        print("I triggered!")
        if(Here){
            if (NetT:number(signalName())!=0){
                if(NetT:number(toString(EID))>Sender){
                #NetT:setNumber(toString(EID),NetT:number(toString(EID))-1)
            }
            NetA:remove(NetT:number(signalName()))
            NetT:setNumber(toString(Sender),0)        
            Self=NetT:number(toString(EID))
            gSetStr(NetS:replace(Brk+toString(Sender),""))
            print("Delete Code running:"+NetA:concat(Brk))
            print(toString(Self))
}}
}
Msg=gGetStr(NetS)
timer("test",1000)
if(clk("test")){print(Msg)}
if(NetOff){
    gDeleteStr(NetMsg)
}

