Official Everybody Edits Forums

Do you think I could just leave this part blank and it'd be okay? We're just going to replace the whole thing with a header image anyway, right?

You are not logged in.

#1 2016-09-27 00:28:55

AK712
Member
Joined: 2015-02-16
Posts: 94

Differentiating Separate Connections Inside onMessage

So I've been trying to make two bots join a room: one for chat commands and one for building. I've been able to do this; however, I'm running into problems. Obviously, during onMessage(), for each m.Type, I get two hits (one by each bot). I only want one bot to handle certain m.Types, and the other bot to handle the others. I'm not exactly sure how to do this. I currently have "connection1" storing the connection for bot 1 and "connection2" with the connection for bot 2. I know I need to compare SOMETHING inside onMessage with these two, but I can't figure out what. Could anyone help with this?


O4DmNuI.png

Offline

#2 2016-09-27 01:14:50

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: Differentiating Separate Connections Inside onMessage

Well, you could consider quite a few things.

If you want to have Connection1 and Connection2 explicitly stated, then you can just create an OnMessage1 and OnMessage2.

If you think you might have a series of connections without limit, I have (in the past) opted to create a class to contain each connection so it has its own bearings to whatever data it needs. That's probably not something you want to just jump into right now.

If you don't want separate OnMessage voids, consider comparing the object Sender in OnMessage. I'm not sure how that works precisely, but something like this:

public void OnMessage (object sender, Message m)
{
   if (sender == Connection1){
      // rip connection 1
   } else if (sender == Connection2){
      // rip connection 2
   } else {
     // lol this is extra as you know but hey
   }
}

Offline

#3 2016-09-27 01:32:05

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: Differentiating Separate Connections Inside onMessage

Use both for building //forums.everybodyedits.com/img/smilies/tongue


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

Wooted by: (3)

#4 2016-09-27 01:34:19

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: Differentiating Separate Connections Inside onMessage

Koya raises a good point. If you haven't properly fitted your bot with some threading or timing system, then you /might/ be trying to fix the hang you get by using another connection. I'm not sure that'll work; I fear the bots might remain on the same thread, making your attempt useless in that regard.

Offline

#5 2016-09-27 01:43:43

Koya
Fabulous Member
From: The island with those Brits
Joined: 2015-02-18
Posts: 6,310

Re: Differentiating Separate Connections Inside onMessage

If I was doing what was asked I would only set up 1 onmessage and that is for the master bot (command) and then have a smaller inline onmessage for each slave that only has init, so things that you want to be aware of in the world happens in the main onmessage.


Po9cnQh.png

PLNQVL8.png
Thank you eleizibeth ^

1SYOldu.png

I stack my signatures rather than delete them so I don't lose them
giphy.gif

WfSi4mm.png

Offline

Wooted by:

#6 2016-09-27 23:23:14

AK712
Member
Joined: 2015-02-16
Posts: 94

Re: Differentiating Separate Connections Inside onMessage

hummerz5 wrote:

Well, you could consider quite a few things.

If you want to have Connection1 and Connection2 explicitly stated, then you can just create an OnMessage1 and OnMessage2.

Actually doesn't work. PlayerIO doesn't allow multiple OnMessage classes, or to have them renamed.

If you think you might have a series of connections without limit, I have (in the past) opted to create a class to contain each connection so it has its own bearings to whatever data it needs. That's probably not something you want to just jump into right now.

Attempted, but this runs into the same problem: PlayerIO runs both classes through the same OnMessage.

If you don't want separate OnMessage voids, consider comparing the object Sender in OnMessage.

Tried every possible combination of this... it's not possible.

Basically what I have is this:

connection1.OnMessage += new MessageReceivedEventHandler(this.onMessage);
connection1.Send("init");
connection2.OnMessage += new MessageReceivedEventHandler(this.onMessage);
connection2.Send("init");

Attempting to change OnMessage to OnMessage1 and OnMessage2 obviously breaks it, since "PlayerIOClient.Connection does not contain a definition for OnMessage1..." and trying to declare an "OnMessage OnMessage1" variable can't work
Attempting to separate the connections in separate classes still causes both classes to call the PlayerIO function OnMessage
Attempting to compare (object sender) to absolutely anything in connection1 or connection2 so it would return which one is being used doesn't appear to work (same for Message m)
As per Koya's idea, attempting to add other onMessages won't work, since it can only be defined once (since it's a part of PlayerIO and not my code)

;-;


O4DmNuI.png

Offline

#7 2016-09-27 23:47:23

hummerz5
Member
From: wait I'm not a secret mod huh
Joined: 2015-08-10
Posts: 5,853

Re: Differentiating Separate Connections Inside onMessage

Interesting.

"PlayerIO doesn't allow multiple OnMessage classes." Well, I'm not sure why you were creating another class. I was thinking more a "public void" or something akin. If you create a new public void OnMessage2, and then do "connection2.OnMessage += new MREH(this.OnMessage2)" it should work. Keep in mind that renaming their existing definitions doesn't make any sense. "OnMessage" in terms of client.OnMessage refers to code that playerIO has created. It doesn't make sense for code to simply work with different names without any explanation.

"Attempted, but this runs into the same problem" I'm not sure you set this up right, then. It /would/ go through the same "OnMessage" but you would be able to determine which local Connection was the mainstay for all your communicating.

"Tried every possible combination of this" I swear I just did this the other day. Yep, just tested comparing, works. On that note, sorry to the unfortunate five-or-so worlds I picked at random to say "connection1" and "connection2". Someone got slightly spammed I think, was a bug. sorry.

so I guess we're back to "this is how you can do things" which for some at this point would translate into "let's see the source code" but we've covered the question eh

Offline

#8 2016-09-28 00:34:31, last edited by AK712 (2016-09-28 00:45:29)

AK712
Member
Joined: 2015-02-16
Posts: 94

Re: Differentiating Separate Connections Inside onMessage

hummerz5 wrote:

Interesting.

"PlayerIO doesn't allow multiple OnMessage classes." Well, I'm not sure why you were creating another class. I was thinking more a "public void" or something akin. If you create a new public void OnMessage2, and then do "connection2.OnMessage += new MREH(this.OnMessage2)" it should work. Keep in mind that renaming their existing definitions doesn't make any sense. "OnMessage" in terms of client.OnMessage refers to code that playerIO has created. It doesn't make sense for code to simply work with different names without any explanation.

"Attempted, but this runs into the same problem" I'm not sure you set this up right, then. It /would/ go through the same "OnMessage" but you would be able to determine which local Connection was the mainstay for all your communicating.

"Tried every possible combination of this" I swear I just did this the other day. Yep, just tested comparing, works. On that note, sorry to the unfortunate five-or-so worlds I picked at random to say "connection1" and "connection2". Someone got slightly spammed I think, was a bug. sorry.

so I guess we're back to "this is how you can do things" which for some at this point would translate into "let's see the source code" but we've covered the question eh

1)Whoops, didn't mean classes, meant functions. But I'll try that and see if it works.

2)I did set it up right. The problem isn't which connection SENDS the data, it's which one RECEIVES the data. And since both classes are initialized and connected to the same room, they both get the data at the same time, when I only want one to get it.

3)Attempted again: if (sender == connection1){ blah blah blah } Definitely doesn't work.

Well, I'll try creating another onMessage and see what happens.

{EDIT} Well, creating another function onMessage2 did the trick. Thanks a ton Hummer! //forums.everybodyedits.com/img/smilies/big_smile


O4DmNuI.png

Offline

Wooted by:
AK7121475019271625982

Board footer

Powered by FluxBB

[ Started around 1732734222.573 - Generated in 0.071 seconds, 12 queries executed - Memory usage: 1.48 MiB (Peak: 1.63 MiB) ]