GetPlayerList returning Promise
Closed this issue · 6 comments
Hey,
First of all thanks for this package! I used to use haxball-server-nodejs which just simply made it possible to run my room on NodeJS but sadly they stopped developing their package and it sometimes causes my rooms to crash. So now I'm trying this one, but I'm not sure I understand it all correctly. Excuse my possible bad understanding of it, but I'm not a native js developer ^^
So I tried to convert my code to the way your package works, which means instead of
room.onPlayerChat = function(player, message) { blabla }
I now have
room.on("onPlayerChat", = function(player, message) { blabla });
That all seems to work fine.
But in my old code I used to loop over the players like this:
players = room.getPlayerList();
for(let player of players) { blabla }
But I'm not sure how to do that with your package. Tried some things but I think the problem is it returns a Promise? Does this mean this simple loop is no longer possible? Hope I'm missing something!
Thanks in advance,
Immers
You need to use await room.getPlayerList();
to make Promise resolved. Because all methods are Promise based.
Thanks! That worked indeed.
One more quick question. If I want to make a player admin I used to be able to do:
room.setPlayerAdmin(playerid, true)
Now I noticed in your code you don't have this anymore, but you create a Player object.
How do I use a Player object in my code?
Because obviously when I do
player = await room.getPlayer(playerid);
player.setAdmin(true);
I get an error saying player.setAdmin() is not a function, as my player is not a Player object.
player = room.players.cache.get(playerid);
player.setAdmin(true);
This should work fine. You can get PlayerObject from players cache.
Tip: https://github.com/mertushka/haxball.js-example you can take this ready to run project but you should change prefix on src/events/onPlayerChat.js line 5.
Oh great thanks! Didn't see the example, maybe it's an idea to put it somewhere on the project page here.
Thanks a lot for your quick help!
Glad I could help. I don't have time to update the project but I think it will not be a problem since it is stable. You can ask questions in any problem.
- Cheers, mertushka.
With release of haxball.js@2.0.0 this issue is deprecated.