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 2017-08-27 09:23:00, last edited by cercul1 (2017-08-27 11:18:02)

cercul1
Member
From: USA
Joined: 2015-02-18
Posts: 91

[Resolved] handling events without using invoke

Is there another way to do this code without using Invoke:

public void Onmessage(object s, PlayerIOClient.Message m)
        {
            Invoke(new EventHandler(delegate
            {
                switch (m.Type)
                {
                    #region Init
                    case "init":
                        {
                            con.Send("init2");
                            con.Send("say", "Hi!");
                        }
                        break;
                        #endregion
                }
            }));

        }

Om_1869ea_249353.jpg

Offline

#2 2017-08-27 09:24:52

Gosha
Member
From: Russia
Joined: 2015-03-15
Posts: 6,211

Re: [Resolved] handling events without using invoke

why not just

public void Onmessage(object s, PlayerIOClient.Message m)
        {
              switch (m.Type)
                {
                    case "init":
                        {
                            cl.con.Send("init2");
                            cl.con.Send("say", "Hi!");
                        }
                        break;
                }

        }

Offline

#3 2017-08-27 10:26:31

cercul1
Member
From: USA
Joined: 2015-02-18
Posts: 91

Re: [Resolved] handling events without using invoke

This did not work, it won't even recognize any message coming in


Om_1869ea_249353.jpg

Offline

#4 2017-08-27 10:39:23

MartenM
Member
From: The Netherlands
Joined: 2016-03-31
Posts: 978
Website

Re: [Resolved] handling events without using invoke

Did you do this?

con.OnMessage += new MessageReceivedEventHandler(messageHandler.handlemsg);

lm3hgg8.jpg

Ingame: marten22        My steam: MartenM

Offline

#5 2017-08-27 10:39:29

capasha
Member
Joined: 2015-02-21
Posts: 4,066

Re: [Resolved] handling events without using invoke

Is he trying to use client to send messages? Why is there cl.con.send?

Offline

#6 2017-08-27 10:55:47, last edited by cercul1 (2017-08-27 10:57:25)

cercul1
Member
From: USA
Joined: 2015-02-18
Posts: 91

Re: [Resolved] handling events without using invoke

MartenM wrote:

Did you do this?

con.OnMessage += new MessageReceivedEventHandler(messageHandler.handlemsg);

yes i have

and

i've removed the cl, i had the event loader in a class before and had the class be a form but that didn't work, so i removed it sorry about that


Om_1869ea_249353.jpg

Offline

#7 2017-08-27 11:01:46, last edited by Gosha (2017-08-27 11:02:59)

Gosha
Member
From: Russia
Joined: 2015-03-15
Posts: 6,211

Re: [Resolved] handling events without using invoke

yea, that's weird.

First get a client

var Client = PlayerIO.QuickConnect.SimpleConnect({gameid}, {email}, {pass}, null);

then get a connection

var con = Client.Multiplayer.CreateJoinRoom({worldid},"Everybodyedits"+Client.BigDB.Load("config","config")["owner"], true, null, null)

Now set up message handler

you can do this in 2 ways.
1.

con.Send("init");
con.OnMessage += OnMessage;

public void OnMessage(object sender, Message m)
{
     if(m.Type == "init")
     {
         con.Send("init2");
     }
}

2.

con.Send("init");
con.OnMessage += (o,m) => 
{
      if(m.Type == "init")
     {
         con.Send("init2");
     }
}

To send messages use connection:

con.Send("init")

Offline

#8 2017-08-27 11:11:47

cercul1
Member
From: USA
Joined: 2015-02-18
Posts: 91

Re: [Resolved] handling events without using invoke

I'm running into a really confusing bug.  I'm not actually sure if it's connecting or not.  It returns the connected value as true, but he doesn't show up in the world until I refresh or open a new tab, my friend's list says he's still offline, any database i check says i'm offline.  I'm not really sure what's happening.


Om_1869ea_249353.jpg

Offline

#9 2017-08-27 11:16:09

Gosha
Member
From: Russia
Joined: 2015-03-15
Posts: 6,211

Re: [Resolved] handling events without using invoke

Bots are offline becasue they don't connect to the lobby.

make sure you:
1) sent "init"
2) when recieved "init" you sent "init2"

or, even better, show your code. (make sure to hide email and password if it's hardcoded)

Offline

Wooted by:

#10 2017-08-27 11:17:45

cercul1
Member
From: USA
Joined: 2015-02-18
Posts: 91

Re: [Resolved] handling events without using invoke

i ended up finding the mistake, i was sending "inut" instead of "init" lol i guess it's 5:15 AM and I still haven't gone to bed so that explains that, thank you so much for helping me with such a silly mistake.


Om_1869ea_249353.jpg

Offline

Wooted by: (2)

#11 2017-08-27 11:35:39, last edited by LukeM (2017-08-27 11:39:57)

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: [Resolved] handling events without using invoke

Just to let you know, you may need to use invoke if you are using forms or timers or anything, as if you dont, you can run into threading problems. Using Invoke right at the start though is actually a clever way to fix this that I hadnt thought of before... :O

If you do want to use it though, I would advise doing this instead, as it looks better, and is probably a bit faster as you dont create events every time a message is recieved:

public void Onmessage(object s, PlayerIOClient.Message m)
{
    switch (m.Type)
    {
        case "init":
            {
                con.Send("init2");
                con.Send("say", "Hi!");
            }
            break;
    }
}

// Somewhere else
con.OnMessage += (s, m) => BeginInvoke((Action<object, Message>)OnMessage, s, m);

This way you are effectively changing a multithreaded program (one main thread and one thread for PlayerIO) into a single threaded program, so you should no longer get the usual problems associated with threading

Offline

#12 2017-08-27 16:11:09

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

Re: [Resolved] handling events without using invoke

destroyer123 wrote:

This way you are effectively changing a multithreaded program (one main thread and one thread for PlayerIO) into a single threaded program, so you should no longer get the usual problems associated with threading

true, but then you get the usual problem with threading: putting your UI and work tasks on the same thread. While the way you suggest is easier and more manageable imo, I don't think it's the optimized course of action

Offline

Wooted by:

#13 2017-08-27 19:40:44, last edited by LukeM (2017-08-27 19:44:09)

LukeM
Member
From: England
Joined: 2016-06-03
Posts: 3,009
Website

Re: [Resolved] handling events without using invoke

hummerz5 wrote:
destroyer123 wrote:

This way you are effectively changing a multithreaded program (one main thread and one thread for PlayerIO) into a single threaded program, so you should no longer get the usual problems associated with threading

true, but then you get the usual problem with threading: putting your UI and work tasks on the same thread. While the way you suggest is easier and more manageable imo, I don't think it's the optimized course of action

Well yes, but for most bots youre limited mostly by internet speed, not by time taken to actually do things. The only bot Ive made that was limited by processing time was Mars Mine, and it only happened when generating the world (a few seconds every hour or so), so didnt really matter

Edit: Just remembered there were also a few JavaScript things too, but... well... its JavaScript, so you cant really compare it to the speed of C# etc.

Offline

Wooted by:
LukeM1503859244675129

Board footer

Powered by FluxBB

[ Started around 1732232869.3085 - Generated in 0.084 seconds, 13 queries executed - Memory usage: 1.53 MiB (Peak: 1.71 MiB) ]