Now that multiplayer is really becoming a thing in GDev I want to add player to player trading to my game. Has anyone attempted this before?
You know how it works, the idea is simple and familiar:
- Player One has 10 gems and Player Two has 100 coins.
- Player One trades 10 gems for 100 coins with Player Two.
- Player One ends up with 100 coins and Player Two ends up with 10 gems.
Same for items or whatnot, but I figured working with coins and gems (just basic global variables) instead of something more complicated like inventory items and sprites would be a much better start.
Thoughts so far:
Both players join the same lobby - how do they coordinate this ahead of time? Maybe they don’t, and just ask in a game chat about trading after they’re inside the lobby? So, in that case make a few trading only lobbies maybe.
Specify the player they want to trade with - tap/click on a player to pull up a options panel. Then you can select a trade option that pops up a “Do you want to trade?” dialog for both players. If both players click yes the full trade window appears for both players. If not a “Player declined to trade” message appears instead.
Trade window with basic inputs - For this example just two number input fields could work, one labeled gems and one labeled coins. A trade and a cancel button.
Agreeing to trade - after both players hit the trade button a summary of what is being trading appears for them to confirm one more time. If both players agree then we initialize the trade.
Initializing trade - this part should be easy, just change the global variable for each player, adding or subtracting as necessary, then save the game so the variables stick.
Am I on the right track?
I haven’t touched the multiplayer events yet, but I’ve watched a few videos to come up with the above ideas. However, in real world use things may not be so simple. If anyone has attempted this before please let me know, preferably with an example if I’m lucky