create a godot( version 4.4) multiplayer game peer 2 peer
player 1 host the server
player 2 search for server and join
player 1 send a quiz question with 4 options
player 2 answers
player 2 send a quiz question with 4 options
player 1 answers
total 3 questions per player per round
after every round both player shown results
want to continue ?
if both player agrees then next round
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Absolutely! Here’s a step-by-step guide and sample code for a simple peer-to-peer multiplayer quiz game in Godot 4.4 using the high-level multiplayer API. This example covers:
Player 1 hosts the game.
Player 2 searches for and joins the game.
Players take turns sending quiz questions and answering.
After 3 questions each, results are shown.
If both agree, the next round starts.
1. Project Setup
Create a new Godot 4.4 project.
Add a main scene (e.g., Main.tscn) with a root Control node.
Add UI elements for:
Host/Join buttons
Question/Answer display
Result display
Continue prompt
2. Networking Setup
Host (Player 1)
gdscript
# Main.gd
extends Control
@onready var multiplayer_peer = ENetMultiplayerPeer.new()
var is_host = false
func _on_host_button_pressed():
multiplayer_peer.create_server(12345, 2)
multiplayer.multiplayer_peer = multiplayer_peer
is_host = true
$StatusLabel.text = "Hosting on port 12345..."
Join (Player 2)
gdscript
func _on_join_button_pressed():
var ip = $IPLineEdit.text
multiplayer_peer.create_client(ip, 12345)
multiplayer.multiplayer_peer = multiplayer_peer
is_host = false
$StatusLabel.text = "Joining " + ip + "..."
3. Player Connection Handling
gdscript
func _ready():
multiplayer.peer_connected.connect(_on_peer_connected)
multiplayer.peer_disconnected.connect(_on_peer_disconnected)
multiplayer.connection_failed.connect(_on_connection_failed)
multiplayer.connected_to_server.connect(_on_connected_to_server)
func _on_peer_connected(id):
$StatusLabel.text = "Player connected: %d" % id
if is_host:
start_quiz_round()
func _on_peer_disconnected(id):
$StatusLabel.text = "Player disconnected: %d" % id
func _on_connection_failed():
$StatusLabel.text = "Connection failed!"
func _on_connected_to_server():
$StatusLabel.text = "Connected to host!"
4. Quiz Logic
Variables
gdscript
var my_questions = []
var opponent_questions = []
var my_score = 0
var opponent_score = 0
var current_question_index = 0
var is_my_turn = false
var total_questions_per_player = 3
func generate_questions():
# Replace with your own question generation logic
return [
{"q": "Capital of France?", "opts": ["Paris", "London", "Berlin", "Rome"], "a": 0},
{"q": "2+2=?", "opts": ["3", "4", "5", "6"], "a": 1},
{"q": "Color of sky?", "opts": ["Blue", "Green", "Red", "Yellow"], "a": 0}
]
5. Sending and Receiving Questions
Send Question
gdscript
@rpc("any_peer")
func send_question():
var q = my_questions[current_question_index]
rpc_id(1 if is_host else 2, "receive_question", q["q"], q["opts"])
$StatusLabel.text = "Waiting for answer..."
Receive Question
gdscript
@rpc("any_peer")
func receive_question(question, options):
$QuestionLabel.text = question
for i in range(4):
$OptionButtons.get_child(i).text = options[i]
$OptionButtons.get_child(i).disabled = false
$StatusLabel.text = "Your turn to answer!"
Answering
gdscript
func _on_option_button_pressed(idx):
for i in range(4):
$OptionButtons.get_child(i).disabled = true
rpc_id(1 if not is_host else 2, "receive_answer", idx)
$StatusLabel.text = "Answer sent. Waiting for next question..."
Receive Answer
gdscript
@rpc("any_peer")
func receive_answer(answer_idx):
var correct = my_questions[current_question_index]["a"] == answer_idx
if correct:
my_score += 1
current_question_index += 1
if current_question_index < total_questions_per_player:
is_my_turn = true
send_question()
else:
show_results()