Thread mesh problem (bug?)

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Thread mesh problem (bug?)

Post by LunaRebirth »

Hello, I couldn't think of a title or where to post this problem at.

I'm working on my MMO game still, and I've run into a problem. My pseudo-code that works:

Code: Select all

char* OnlinePlayerHat;
void client_thread() {
   recv(...); //Receive info from server
   if (OnlinePlayerHat != ReceivedData.hatString) {
      setHat(RceievedData.hat);
      OnlinePlayerHat = ReceivedData.hatString;
   }
}
int main() {
   bool running = true;
   while (running) {
      client_thread();
   }
}
What happens is, the recv receives ReceivedData.hatString -- This tells the hat name in char*. ReceivedData.hat is the actual hat mesh and setHat places the hat on OnlinePlayer.
(Of course, this is pseudo-code, so some things are different)

BUT, when I do this; It doesn't work?:

Code: Select all

char* OnlinePlayerHat;
void client_thread() {
   bool runThread = true;
   while (runThread) {
      recv(...); //Receive info from server
      if (OnlinePlayerHat != ReceivedData.hatString) {
         setHat(RceievedData.hat);
         OnlinePlayerHat = ReceivedData.hatString;
      }
   }
}
int main() {
   bool running = true;
   CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)client_thread,NULL,NULL,NULL);
}
What happens when using the second code, is that locally the player sets the hat, but the hat doesn't display the texture. So it's just a white mesh on the player's head.
But online, it doesn't even display the mesh at all. The client just crashes.

It's a bit strange, and I don't see the problem.

Here's why I'm trying to use the thread instead of the looped from main code:
When looped in main, the received data seems to work for a while. But after so long, the players just stop updating. So if I moved, no one would see it. Which makes no sense.
Even when I remove hats and only display player locations.
--But when I use the CreateThread, everything seems to work smoothly and without any problems. Other than the hat, of course, which when removed still works as should be.

Any ideas on how to fix this?
Thanks!
kklouzal
Posts: 343
Joined: Sun Mar 28, 2010 8:14 pm
Location: USA - Arizona

Re: Thread mesh problem (bug?)

Post by kklouzal »

I'm not sure if it's related or not since I can't see your "setHat()" function. That being said, if you are using Irrlicht within that function, Irrlicht is not thread safe (frown face), so you're going to need to maybe create a "queue" which is processed in the main thread, in your alternate threads you can add items to this queue (like setHat()) then the main thread will actually call setHat() and your problem will probably remedy. That's just my two cents.
Dream Big Or Go Home.
Help Me Help You.
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Thread mesh problem (bug?)

Post by mongoose7 »

It might be better to simply solve the problem you had when using only one thread.
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Re: Thread mesh problem (bug?)

Post by LunaRebirth »

kklouzal wrote:I'm not sure if it's related or not since I can't see your "setHat()" function. That being said, if you are using Irrlicht within that function, Irrlicht is not thread safe (frown face), so you're going to need to maybe create a "queue" which is processed in the main thread, in your alternate threads you can add items to this queue (like setHat()) then the main thread will actually call setHat() and your problem will probably remedy. That's just my two cents.

Code: Select all

IAnimatedMeshSceneNode* hatNode = 0;
void setHat(IAnimatedMesh* hatMesh) {
            if (hatMesh != 0 && newHat != "firstSet") {
                hatNode->remove();
            }
            hatNode = smgr->addAnimatedMeshSceneNode(hatMesh);
            hatNode->setMaterialTexture(0, driver->getTexture(imgPath));
            hatNode->getMaterial(0).Shininess = 0;
            hatNode->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
}
LunaRebirth
Posts: 385
Joined: Sun May 11, 2014 12:13 am

Re: Thread mesh problem (bug?)

Post by LunaRebirth »

mongoose7 wrote:It might be better to simply solve the problem you had when using only one thread.
Hmm, okay. I'll see what the problem is. Just seems to not be working out properly
mongoose7
Posts: 1227
Joined: Wed Apr 06, 2011 12:13 pm

Re: Thread mesh problem (bug?)

Post by mongoose7 »

Problems could come from anywhere. For example, I see where you compare newHat with "firstSet" but I don't see where you change newHat. This is important because you should try to change it in the same place that you test it. If you set it at random places in the code, you will have a hard time finding why it doesn't work properly. You may have a reason for doing things the way you do, but this is just an example to show how you can get into trouble.
Post Reply