0

Heey, i've been looking alot on Lidgren, and i've managed to get some simple console client and servers, but i'm having a really hard time with 2D...

Basically what i have so far is only Console based applications but i found an Example of a 2D game using Lidgren. You opened a server, and then two applications. They connected automatically and you could play with the two windows, seeing the character move in the other screen. That project used an array to load the textures and in the Draw() method it simple draws the array, but using a value from:

foreach (var kvp in positions)
{
   // use player unique identifier to choose an image
   int num = Math.Abs((int)kvp.Key) % textures.Length;`
   // draw player
   spriteBatch.Draw(textures[num], kvp.Value, Color.White);
}

Could someone explain what that num variable does? And if i wanted to use diffrent classes to do this, would i simply just do the same but in the player classes, and also, animations - how do you send texture update data?? Sorry that i'm asking so much..but i haven't found anything that actually helps :/

Thanks in advance and if you need to know something else, tell me! :)

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
QuackTheDuck
  • 75
  • 1
  • 2
  • 8

1 Answers1

0

The num variable simply chooses a unique texture, that will always be the same, from the textures array. Say you have 4 textures, but the Key the modulo operator (%) will get the remainder of 7/4, which would be 3. Basically it would wrap it around the amount of textures, because they would have to repeat (Ex, Id 4 and Id 8 have the same textures)

For now you can just use a static texture, but you can use the same code to choose a texture variation.

What do you mean texture update data? Such as changing textures, or updating positions? If changing textures, is the texture known or not? (Ex, can an ID/name be sent, or does the data have to?)

Cyral
  • 13,999
  • 6
  • 50
  • 90
  • What i mean with texture updates is basically animation, like in a player class: `public void Animate(GameTime gameTime, int sFrame, eFrame)` and use a `currentFrame` and `elapsed` variable. And with positions, is it just to send a message with the variable to update it? I'm asking this since i don't quite get the Lidgren tutorial, and also i'm very new to networking. Thanks :) – QuackTheDuck Nov 09 '13 at 23:18
  • For animations, you would keep adding the `elapsed` to a variable, and when it is more than `x`, reset the variable and change frames. Honestly, for position, I was having issues too. Sending movement doesn't work, sending position looks buggy, so yeah, not sure on that one. That is a pretty big topic in itself, so you might want to ask another question to get some attention. – Cyral Nov 10 '13 at 12:44