How wrong is this?

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

How wrong is this?

Postby capdetrons » 28 Jan 2014, 23:18

Hi,

I know I can' learn about game coding overnight and I don't mean to take anyone's time, but I DO want to keep trying to program a 4 player game. A two team card game.

I have googled trying to find info or tutorials but I can't find a site with a clear tutorial or hints I can base myself on so I am trying to get started with the 2 player games that come as examples with SF. I have modified the PRO tris example and the first major problem I'm encountering is that my game isn't visible until the third try.

I figured it would be about modifying the internal event handler in the extension and added 2 more players in there like this:

Code: Select all

if (u.getPlayerIndex() != -1)
{
   numPlayers++;

   if (u.getPlayerIndex() == 1)
   {
      p1id = u.getUserId();
   }
   else if (u.getPlayerIndex() == 2)
   {
      p2id = u.getUserId();
   }
   else if (u.getPlayerIndex() == 3)
   {
      p4id = u.getUserId();
   }
   else
   {
      p4id = u.getUserId();
   }

   if (numPlayers == 4 && !gameStarted)
   {
      startGame();
   }
}


The player's index id's are correct and the start game function will trigger properly as soon as the 4 players are there, but, like I entitled the post (or thread - excuse my english) I have to test the swf 3 times before any other player sees the game room. The rest of the extension file is attached.


Thanks
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: How wrong is this?

Postby Bax » 30 Jan 2014, 09:34

I'm not sure if this can be the source of the problem, but in your code you are setting p4id two times and never set p3id...
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: How wrong is this?

Postby capdetrons » 30 Jan 2014, 12:13

Good morning and thanks, Bax,

Yes, I noticed that after posting my question. All I want to achieve is getting to set 4 players instead of two. I think I could be able to sort out the rest after that, but I don't know if I will be able to get through the start.

The way it runs, I do have 1 player as soon as the first user creates a game room, but the second one, never gets to see it until the third time it is created...
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: How wrong is this?

Postby Bax » 31 Jan 2014, 13:42

Sorry but I don't understand... do you mean that you need to create a Room three times before users receive a notification of its creation?
If this is the case, then you should get an error on the server explaining why you can't create the Room the first two times.
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: How wrong is this?

Postby capdetrons » 31 Jan 2014, 16:08

Right! That's what I meant. But anyway, it was me not really seeing what happens on joining a room. I think I 'm beggining to see it now. I have changed the internal event handler and the problem disappeared even though I get a "waiting for player 1" whenever the second or third player have entered the game room.

Code: Select all

if (u.getPlayerIndex() != -1)
   {
      numPlayers++
      trace("numPlayers = "+numPlayers);
         
      if (u.getPlayerIndex() == 0)
         p1id = u.getUserId()
      else if(u.getPlayerIndex() == 1)
         p2id = u.getUserId()
      else if(u.getPlayerIndex() == 2)
         p3id = u.getUserId()
      else if(u.getPlayerIndex() == 3)
         p4id = u.getUserId()
               if(numPlayers == 4 && !gameStarted)
            startGame()
      }


I know it will sound like a dumb question but does the "Flash Multiplayer Virtual Worlds" book have the explanation for more than two players joining the game room? I can't find any other titles covering Smartfox.
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: How wrong is this?

Postby Bax » 31 Jan 2014, 18:37

That book is about virtual worlds, where many more than 4 users join a Room. But actually I really don't understand what the problem is, sorry.
Joining a Room is straightforward: just call the SmartFoxClient.joinRoom method.
If the problem is to "start" the game when 4 players join the Room, you have to implement a server-side Extension and listen for the join event: when 4 players are in, make your Extension send a response to all the players in the Room to make the game start.
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: How wrong is this?

Postby capdetrons » 31 Jan 2014, 20:10

Thanks, Bax, no need to say I'm a novice.

I'm trying that. I'm using the code in the previous post but I reach 4 players joining the main room and only 2 of those in the game room so I'm getting wrong traces and results.

In the sftris example the extension is invoked twice. Once when the user creates the room and again when he and the rest of the players enter. I don't see why I get 4 players in the main or zone room.

You mean this could be correct in the internal event handler?

Code: Select all

if (u.getPlayerIndex() != -1)
   {
      numPlayers++
         
      trace("numPlayers = "+numPlayers);
         
      if (u.getPlayerIndex() == 1)
         p1id = u.getUserId()
      else if(u.getPlayerIndex() == 2)
         p2id = u.getUserId()
      else if(u.getPlayerIndex() == 3)
         p3id = u.getUserId()
      else if(u.getPlayerIndex() == 4)
         p4id = u.getUserId()

   if(numPlayers == 4 && !gameStarted)
      startGame()
}


It's how I've ended up setting it and I get the 4 players in the game room but plenty of them in the main room (which I guess doesn't matter?).
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: How wrong is this?

Postby Bax » 01 Feb 2014, 10:52

What is the "main or zone room"? Are you referring to the game lobby, where the players chat before starting a game?
If yes, maybe you are attaching the extension to that Room too, which is wrong. In fact only the actual game Room is supposed to have an Extension attached.
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: How wrong is this?

Postby capdetrons » 01 Feb 2014, 11:51

right. The main lobby. That's where I see the number of players doesn't match the number of players in the game room.

As for the waiting for player 1 message, I see now it's the function with the ternary operator I didn't know how to replace with a conditional statement, but can I think about getting started without worrying about the traces in the lobby room?

When you say only one extension is needed attached to the game room, how is that? The sftris example calls it once when the gameroom is created and again once in the game room. I thought it was for the purpose to increase the playerIndex so it won't be undefined and then be able to count the playing users in the room.
User avatar
Bax
Site Admin
Posts: 4612
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: How wrong is this?

Postby Bax » 03 Feb 2014, 08:44

I'm sorry but it is very hard to follow what you are doing.
Unless you modified the Tris example, the lobby Room isn't a Game Room. This means that the player indexes are not assigned while in that Room.
Also, in the example the Extension is "attached" to the Game Rooms only when they are created, not to the lobby Room.
When a Game Room is created, the init() method of the Extension is called, and that's where numPlayers is initialized.
When a player joins the Game Room, the handleInternalEvent() method is called, with evt.name = userJoin. That's where the Extension's player ID's are assigned and if 4 players entered the Room, the game is started (startGame() method) by sending a message to all the players.
The flow is quite straightforward... I still don't get what isn't working in your version.
Paolo Bax
The SmartFoxServer Team
capdetrons
Posts: 72
Joined: 24 Aug 2013, 09:15
Location: Barcelona
Contact:

Re: How wrong is this?

Postby capdetrons » 03 Feb 2014, 11:58

Good day, Bax, and thanks for the reply,

hello, Bax, if you're still following this post. i just realized it was mainly a syntax problem and I finally have my game setup. Yes it is very straightforward.

There's one thing I would like to be sure of though and that is how to send messages from the waiting room. I will probably sort it out trying but I haven't been able yet and i keep wondering whether I'm in the gameroom while waiting for the other players.

Anyway, thanks again

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 41 guests