2019-01-13 13:47:38

Ok, so i have a 4 player game where people can connect to my IP and we can play. It supports a  2, 3, and 4 player mode. However, I have made each player have their own everything as variables. I'm not very good at using classes or objects, but can I make one class that will work for an infinite number of players and still tell the game that when the last person is done it goes back to player 1 again

2019-01-13 13:50:37

And a second question. Do I need a central server so I don't have to tell player 1 to send all the messages to every player? Because currently in my game whenever player 4 takes their turn, player 1 sends the messages to everyone else, and player 4 ends up receiving their own message and turning into player 3. This bug only occurs in 4 player mode, 2 and 3 player work without issue. So is a central server needed in order for all players to communicate without going through the host?

2019-01-13 20:12:17

Yes, indeed you can. Create a container to hold all your player classes, and define the class to have all the functions and variables you need. Then when a player connects, create a class for them and store it. When all the players disconnect, destroy the container. When one player disconnects, just remove them from the container. As for the other problem, again, you can. There are varying algorithms and ways on exactly how to do this. The best one I've found is to do the following:
* Loop through all the connected peers
* Determine if the event/signal/whatever is coming from the same peer your iterating over at that time
* If so, skip it
* Otherwise, send the events to the other peers

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github

2019-01-13 21:24:51

I have no clue where to even start. To be honest, I've done everything through variables with code, I've written classes before but they never came in use, I just use variables instead. But it is very inefficient and ends up making things harder in the end. When is a class more useful than variables? I read the bgt manual, too smart for a dummy like me to understand.

2019-01-14 10:10:04

Classes are useful for exactly what you're talking about here. If you wanted to handle an arbitrary number of players without using classes, you could, by using dictionaries or arrays, but you'd soon find that you basically reinvent the way classes work with the functions and such you'd have. A class isn't just a collection of variables. It's also things you can do with those variables, applied to whichever instance you're interested in. And with BGT, you can rely on all instances of a given class to have the same variables and methods, whereas if you're adding and removing and modifying variables in arrays or dictionaries in a less structured way, it's actually quite easy to forget something, or cause a swarm of index out of bounds errors, etc.
The two things that are a little bit strenuous to get use to with BGT are rare outside of BGT. Those are the way handles are implemented (not too different from C++, but enough so to be confusing), and the lack of a "new" keyword or similar to make creating new objects easier. I usually write a new_x function, where x is the name of the class, to get around the latter. Handles ... someone should just make a whole tutorial on how to use those.
The more complex your project becomes, the more useful classes will be instead of parallel arrays and dictionaries. I'll grant that I was able to get away without really learning how to use them for, like, a year, year and a half? In Javascript, where arrays work very differently. For something like players for an online game, though, I'd strongly recommend a class, since you might find that you need to add or change something later, and it's much easier to avoid missing anything if all the relevant parts are in a class.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.