I just got a second iPad and decided to try a multiplayer game using sockets. One problem I had was trying to keep the code in sync on both iPads as I kept making changes/fixes. At first, I would copy the Codea code, paste it in an email and email it to the other iPad. I would open the email there, copy the code and paste it into a project. Well, that got old real fast. So I set up this code to make it easier for me. I copy the code I want to send, run this code on both iPads, paste the copied code into the parameter message area, and press send message. On the other iPad, the sent code is printed in the print area. I then tap on the print area which copies the code to the pasteboard. I close this code, open the project where I want the new code and do a paste. It’s a little easier and I’m not messing around with email. To use this code, go to the Settings app, select Wi-Fi, tap your wi-fi name and get your IP address. Put that IP address in myIp. Get the IP address of the other iPad and put it in theirIp. Do the same on the other iPad. The myIp and theirIp will be flipped on both iPads. There’s code that will get the IP address automatically, but I didn’t include it. Once the ip addresses are set, I don’t think they change. At least they haven’t for me.
As for the multiplayer game, it’s a simple game similar to TRON. Not a lot is being put into the game because I want to show what it takes to use sockets for a multiplayer game. The game currently runs OK, but I’m still testing it. I’ll post it here a little later. One of the problems people were having was the games getting out of sync as you played. I added code that keeps both iPads in sync for this game.
function setup()
socket = require("socket")
myIp="192.168.254.27" -- this ipads IP address
theirIp="192.168.254.15" -- the other ipads ip address
server = socket.udp()
server:setsockname(myIp,5544)
server:settimeout(0)
client = socket.udp()
client:setpeername(theirIp,5544)
client:settimeout(0)
parameter.text("msg", "")
parameter.action("send message", sendMessage)
end
function sendMessage()
client:send(msg)
end
function draw()
background(0)
local data, msgOrIp, portOrNil=server:receivefrom()
if data~=nil then
print(data)
end
end