camchenry / sock.lua

A Lua networking library for LÖVE games.
https://camchenry.github.io/sock.lua/
MIT License
173 stars 7 forks source link

Is client:getIndex() a dynamic variable? #8

Closed Ulydev closed 7 years ago

Ulydev commented 7 years ago

Hello, thanks for the amazing library.

I was wondering if it's fine to set a .id attribute to the client instead of using :getIndex()? I need to broadcast a list of connected clients as simple arrays, e.g. clients = { {name="john", id=...}, {name="joe", id=...} } - I can't use :getIndex() on the client-side. I just want to make sure what :getIndex() returns doesn't change during runtime. Is that the case?

camchenry commented 7 years ago

getIndex will return the index of the peer in the enet array, which won't change, but the peer that it represents might change. Enet creates an array where each index represents a peer connected to your server. These indexes will be reused if someone disconnects and someone else (or the same person) connects to your server. For the duration of the connection, the index should not change. If you're able, you can also use http://camchenry.com/sock.lua/#Client:getConnectId to assign an id to each user.

Internally, clients are cached so that the same connection will have the same exact Client instance in callbacks. If you attach some data to a client object it should persist through the length of the connection, but I have not tested this.

Ulydev commented 7 years ago

Thanks for clarifying! I've been attaching some custom data to clients (e.g. player name) and it seems to work like a charm.