This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
ikke009's profile picture

[LUA][SOLVED] trying to make a multiplayer game

Started by ikke009, 20 February 2013 - 09:13 PM
ikke009 #1
Posted 20 February 2013 - 10:13 PM
I am trying to make a multiplayer game, so the first thing I wanted to do is make two computers that run my program find each other..
If I run my code however, one computer stays on "Searching for players" and the other on "Finding games" while they should be able to find each other.. Can someone please help out with this? (I am quite noob at rednet stuff xD)
http://pastebin.com/JauCCqP6
Shrooblord #2
Posted 21 February 2013 - 06:41 AM
Here's a couple of non-coding related problems:
-Your computers don't have a modem.
-Your computers are out of range of each other.
—-

Also, this isn't what's wrong with your code, but isn't
id2 == "buckyball" and id2 == id (line 137)
a bit obsolete? If id2==id then id2 already equals "buckyball".

Moving on…

This part of your code (lines 148-150)
elseif p1 == keys.n then

end
does absolutely nothing. It doesn't break the loop, it doesn't end the while loop4==true loop. It just sits there. The result is that the while true do os.pullEvent is now constantly executed without much happening.



Also see the Wiki on rednet.receive:
You must be calling this function at the moment a message is sent, or you will miss it. If you need to watch for messages while doing something else at the same time, see parallel.waitForAny()
(http://computercraft.../Rednet.receive)

Maybe you need to set the timeout for your rednet.receive(1) (line 78) to 2 or more just in case.
ikke009 #3
Posted 21 February 2013 - 10:52 PM
the computers have modums and they are in range (im running them in CC emulator)
I know the if p1 == keys.n then does nothing, i didnt make that part yet but it shouldnt matter since the program doesnt even get to the point where that is useful.
I can try setting the timeout on the rednet.receive() a bit longer, but i doubt it would work since it is in a loop anyway, so when it times out it almost instantly receives again.

however i just found out that the problem was indeed the line
if id2 == "bukyball" and id2 == id then
becuase that would always be false, obviously.
it should have been
if msg2 == "buckyball" and id2 == id then
so now it works again :)/>
ikke009 #4
Posted 22 February 2013 - 06:13 AM
bumping with a new issue.. if i create a game on one computer, and connect to it on the other, it asks the connecter if you want to join the game. I accept, then the creator gets asked if he wants the player to join. If i deny the player, i get an rednet:55:number expected error..
I have no idea whats causing this so i would appreciate if someone can help me..

http://pastebin.com/1BN3pvMf
ChunLing #5
Posted 22 February 2013 - 09:55 AM
You're referring to this bit here:
write("Accept? y/n")
local loop3 = true
while loop3 == true do
    local ev,key = os.pullEvent("key")
    if key == keys.y then
        loop1 = false
        loop2 = false
        partnerID = id
        rednet.send(partnerID,selfID)  -- you gave it a couple of numbers
        term.setCursorPos(4,6)
        write("Connected to " .. partnerName)
        return
    elseif key == keys.n then
        term.setCursorPos(4,6)
        write("rejected")
        rednet.send(partnerID,"reject") -- you gave it a number and a string
        sleep(2)
        term.setCursorPos(4,6)
        write("		   ")
        term.setCursorPos(4,5)
        write("		   ")
        term.setCursorPos(4,4)
        write("Searching for players		 ")
        loop3 = false
    end
end
Odd…it seems like this is the wrong place, it's a problem for later.

No, the problem you're describing is here at 166:
elseif p1 == keys.n then
  loop4 = false
end
In other words, pressing n doesn't send a response (it also doesn't exit that loop, like you seem to be thinking). And the host needs a response within 1 second due to line 78:
local id,msg = rednet.receive(1)
  if tonumber(msg) == id then
    rednet.send(id,"buckyball")
When it doesn't get a message within the one second timeout, the returns on id and msg are both nil. tonumber(nil) = nil == nil, so it tries to rednet.send(nil,"buckyball"), thus presenting your error.

This code is very poorly organized. It needs to be completely reorganized before you can really hope to fix problems of this nature without causing other problems.
ikke009 #6
Posted 22 February 2013 - 10:28 PM
Well what I want is those two computers to connect in a busy rednet network. so i have to "filter out" all the other messages. thats why i used loops in every rednet.receive(). I am sending those messages back and forth between the computers to verify that they want to connect to each other.
I could put a couple of functions in it to make it a bit neater, but apart from that i dont really know how to make it more organized…
ChunLing #7
Posted 23 February 2013 - 08:56 AM
Turn all of it into functions. It will help a lot.

As for the particular problem, changing the 166 area so that pressing "n" sends a rednet message is simple enough. The problem is that the loop4 = false doesn't do what you want…because you don't escape the inner loop to the loop where loop4 is a control value.
Edited on 23 February 2013 - 07:59 AM