DISCLAIMER: This is by no means an example of good code, or how you're "supposed" to do it, it's purely code I managed to hack together for my game, learning as I went along. But it may be of some use to you. The global values are set by interacting with the main menu, then init is called when the Go button is pressed. Each game "tick", wait_tick is called and the current tick number sent to make sure the games are at the same pace. (Problem with this - if one computer lags, the other computer freezes until the lag is gone). Then sync_commands is called to exchange the actions each player has made. (Problem with this code - the order in which commands are executed is different between clients. It's not a problem right now but could become one. I plan to replace it with a new system, where all commands are sent to the host, who then collects them and sends the entire package of all client commands to each client, so the order is the same. This would also help expand the code to more than two players)
|