WLAN Multiplayer?

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Kakolukia
Posts: 21
Joined: Sun Jun 11, 2006 8:43 pm

Post by Kakolukia »

Altair wrote:Yes what you did is the same as what i did.

I didn't know the command tonumber existed. Thx for that!
That's what I thought, saves us a lot of code!

But if I want to send an x and an y (and more), what would be the best way to do this?

make 1 long string of, and cut it in pieces at the other psp?
or send them seperatly? But how does the other psp know which is the x and y?
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Hmm yeah, i guess still use one big string and then cut it into pieces. So it still doesnt help to much, the "tonumber" command.
Kakolukia
Posts: 21
Joined: Sun Jun 11, 2006 8:43 pm

Post by Kakolukia »

Altair wrote:Hmm yeah, i guess still use one big string and then cut it into pieces. So it still doesnt help to much, the "tonumber" command.
maybe be2003 knows how to solve this in a smooth way too...
SSpeare
Posts: 63
Joined: Tue May 23, 2006 11:45 pm
Contact:

Post by SSpeare »

You could build a string like this:

Code: Select all

-- Assuming:
x = 45
y = 56
-- Do this:
send = x.."^"..y
-- Now send = "45^56"
And then parse it like this:

Code: Select all

x = tonumber(piece(data,"^",1))
y = tonumber(piece(data,"^",2))
If you use a function "piece" like this:

Code: Select all

function piece(str,delim,num)
	if num < 1 then
		return ""
	end
	local lastEnd = 0
	local matchBegin,matchEnd = string.find&#40;str,delim,1,true&#41;
	local loop = 1
	for i=2,num do
		loop = i
		lastEnd = matchEnd
		matchBegin,matchEnd = string.find&#40;str,delim,matchEnd+1,true&#41;
		if &#40;matchBegin == nil&#41; then
			matchBegin = string.len&#40;str&#41;+1
			break
		end
	end
	if num > loop then
		return ""
	end
	return string.sub&#40;str,lastEnd+1,matchBegin-1&#41;
end

You could use anything instead of "^", that's just an example. And you can put more data in there. As much as you want. The piece function is kind of wasteful if you are getting lots of data out, but it works. If you want it to be more efficient you could use a Tokenizer pattern or something.
Last edited by SSpeare on Fri Jun 16, 2006 7:18 am, edited 1 time in total.
Kakolukia
Posts: 21
Joined: Sun Jun 11, 2006 8:43 pm

Post by Kakolukia »

there is a lot of new code for me in that, but I will look into it later, maybe tomorrow, got some stuff to do now...
But thanx for the help!
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

Post by be2003 »

... sending coordinates...
first define ur x and y
x = 2
y = 5
then make em one string separated by a comma
coXY = tostring(x)..","..tostring(y)
send it
System.irdaWrite(coXY)

on the receiving end
oppcoXY = System.irdaRead()
blah blah blah, just receive the string like normal ok

make a fuction to turn it back
function parseXY(oppcoXY)
for i=1, string.len(oppcoXY) do
char = string.sub(oppcoXY,i,i)
sy = ""
sx = ""
tempxory = "x"
if char == "," then
tempxory = "y"
elseif tempxory == "x" then
sx = sx..char
else tempxory == "y" then
sy = sy..char
end
end
oppx = tonumber(sx)
oppy = tonumber(sy)
end

this function takes i the received string and writes the vars oppx and oppy to the ram and can be accessed anytime if they arent changed to something else.
- be2003
blog
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

Ok thx. So its basicly what we already did but then with the addition of tonumber() (which makes stuff easier though)
Kakolukia
Posts: 21
Joined: Sun Jun 11, 2006 8:43 pm

Post by Kakolukia »

Altair wrote:Ok thx. So its basicly what we already did but then with the addition of tonumber() (which makes stuff easier though)
yeah, pretty kewl community out here, there really helpfull, thanx to anyone who helped us out!
Kakolukia
Posts: 21
Joined: Sun Jun 11, 2006 8:43 pm

Post by Kakolukia »

if char == "," then
tempxory = "y"
elseif tempxory == "x" then
sx = sx..char
else tempxory == "y" then
sy = sy..char
end
shouldnt it be an "elseif" or an "=" instead of an "else" or "=="???
Last edited by Kakolukia on Sat Jun 17, 2006 7:16 am, edited 1 time in total.
Altair
Posts: 76
Joined: Sat May 20, 2006 2:33 am
Location: The Netherlands

Post by Altair »

yes it has to be "elseif"
Kakolukia
Posts: 21
Joined: Sun Jun 11, 2006 8:43 pm

Post by Kakolukia »

figured it out, = gives an error, but you probably tried it too, and found out ;)
be2003
Posts: 144
Joined: Thu Apr 20, 2006 2:46 pm

oops

Post by be2003 »

the function should work if you define sx, sy, and tempxory before the whole "for i=..." or else everytime the for command makes a cycle then the vars get reset. sorry
- be2003
blog
Post Reply