215 posts
Location
Netherlands
Posted 22 June 2013 - 04:16 PM
Dear ComputerCraft experts,
I am making a PVP based mini-game using MiscPeripherals. You need to make teams and I was wondering how I could do this. My dad said I had to use an array, but I have no idea how to use it. He has more than 20 years experience with programming. Could someone maybe give me a push in the right direction?
How it works is as follows:
First the first team has to assemble. This means every player in the waiting room has to sign in. This as well for the second team. If a team has more players than the other, the team with less players has to agree with it.
25 posts
Posted 22 June 2013 - 05:31 PM
What I like to do is write out everything I need my program to do first. It makes it a bit easier, for me at least, to see what exactly I need to do.
So let's do that shall we?
First the first team has to assemble.This means every player in the waiting room has to sign in. This as well for the second team. If a team has more players than the other, the team with less players has to agree with it.
I'd suggest taking a look
here for a nice table tutorial. Tables in lua are not hard to use.
If you have a lot of players I would suggest have several Player Detectors sending the player name and team to a main computer.
As for evaluating team size, well that's actually very easy. In the link above there is a section on the '#' operator. I'd suggest reading that if you want to get team size. After that it's a simple if statement.
Links I think you will find useful:
Modem API Wiki Pageos.pullEvent Wiki PageMiscPeripherals Page
215 posts
Location
Netherlands
Posted 22 June 2013 - 06:16 PM
This is what I have for now:
function TeamMaking()
for x = 1,4 do
while true do
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
end
end
end
end
peripheral.wrap("right")
teamOne = {}
1522 posts
Location
The Netherlands
Posted 22 June 2013 - 08:19 PM
The thing with player detectors is that they dont return a side. So they must click like this: redplayer blueayer redplayer blueplayer etc.
If you dint understand the code, give me a PM or just here :)/>
local teamplayers = nil
repeat
teamplayers = tonumber(read())
until teamplayers
local redteam = {}
local blueteam = {}
for i = 1, teamplayers * 2 do
local _, player = os.pullEvent('player')
if i % 2 == 0 then
redteam[#redteam.+ 1] = player
else
blueteam[#blueteam + 1] = player
end
end
this should work, written from my ipod
25 posts
Posted 22 June 2013 - 09:43 PM
peripheral.wrap("right")
teamOne = {}
function TeamMaking()
for x = 1,4 do
while true do
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
end
end
end
end
That should work, but like Engineer said, if you are getting both teams from the same input then you would have to have each player specify which team.
Alternatively you could just have 2+ player detectors and each one could be a different team.
97 posts
Location
Ohio, USA
Posted 22 June 2013 - 10:25 PM
This is what I have for now:
function TeamMaking()
for x = 1,4 do
while true do
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
end
end
end
end
peripheral.wrap("right")
teamOne = {}
What if there is more than 4 players? You should have some way of telling the players that couldn't make it on the team. (ex. "1vannn cannot join this team because there is already 4 people joinned!")
Also, I think there is a way of doing this - a math.random loop until that team is filled. So it'll be like this..
if teamOneCount > 1 and teamTwoCount > 1 then
numb = math.random(1,2)
if numb == 1 then
teamOne[x] = player
else
teamTwo[x] = player
end
end
I don't think thats such a bad idea. Of course friends trying to play together wouldn't like it..
Edited on 22 June 2013 - 08:31 PM
215 posts
Location
Netherlands
Posted 23 June 2013 - 03:56 AM
This is what I had in mind, not the most pretty, but it should work:
function TeamMaking()
for x = 1,4 do
while true do
term.clear()
print("Any more players? Y/N")
term.setCursorPos(23,1)
anyMorePlayers = read()
if anyMorePlayers == "N" then
numberPlayersTeamOne = x
for x = x,4 do
teamOne[x] = "noPlayer"
end
return false
else
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
end
end
end
end
end
peripheral.wrap("right")
teamOne = {}
1522 posts
Location
The Netherlands
Posted 23 June 2013 - 04:01 AM
This is what I had in mind, not the most pretty, but it should work:
function TeamMaking()
for x = 1,4 do
while true do
term.clear()
print("Any more players? Y/N")
term.setCursorPos(23,1)
anyMorePlayers = read()
if anyMorePlayers == "N" then
numberPlayersTeamOne = x
for x = x,4 do
teamOne[x] = "noPlayer"
end
return false
else
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
end
end
end
end
end
peripheral.wrap("right")
teamOne = {}
Whats up with the infinite loop? That will infinetlly waitor a player..
I siggest you do something more controlled maybe something like *cough* I *cough* created..
215 posts
Location
Netherlands
Posted 23 June 2013 - 04:27 AM
This is what I had in mind, not the most pretty, but it should work:
function TeamMaking()
for x = 1,4 do
while true do
term.clear()
print("Any more players? Y/N")
term.setCursorPos(23,1)
anyMorePlayers = read()
if anyMorePlayers == "N" then
numberPlayersTeamOne = x
for x = x,4 do
teamOne[x] = "noPlayer"
end
return false
else
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
end
end
end
end
end
peripheral.wrap("right")
teamOne = {}
Whats up with the infinite loop? That will infinetlly waitor a player..
I siggest you do something more controlled maybe something like *cough* I *cough* created..
that's not true. I have to iron out if it has 4 players it isn't gonna wait for the fifth, but it should work.
7083 posts
Location
Tasmania (AU)
Posted 23 June 2013 - 04:52 AM
Sorry, but no, it won't work.
You first create a "for" loop, where "x" starts at 1 and increments with each iteration until it would go above 4. That's ok, but then you put a "while" loop inside it that'll iterate forever. Meaning that not one single iteration of the "for" loop surrounding it will complete and "x" will always be 1, as the "while" loop will never end and let the "for" loop continue.
Hence it'll keep asking if you want more players until you finally tell it "N", at which point it'll finally stop - but not before ensuring that no players are in your table.
Without the "while" loop, it'll do pretty much what you want it to do.
One poster wanted to set up something a lot like this, where the system determined which team each player would be on as they walked past the computer according the side of the machine they walked past. Seemed like a good way to do it to me. The idea is that you'd put pressure plates on either side of the computer and pull the player events whenever they were stepped on.
215 posts
Location
Netherlands
Posted 23 June 2013 - 04:58 AM
Total rewrite. It started to don't make any sense anymore.
function TeamMaking()
numberOfPlayers = read()
numberOfPlayers = tonumber(numberOfPlayers)
for x = 1,numberOfPlayers do
while true do
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
return false
end
end
end
end
peripheral.wrap("right")
teamOne = {}
1522 posts
Location
The Netherlands
Posted 23 June 2013 - 05:18 AM
event, player = os.pullEvent("player")
if event == "player" then
teamOne[x] = player
return false
end
This is the part that confuses you, because you have put in a filter "player" in the os.pullEvent, it will only trigger when that even happens. So:
local teamMaking = function()
local numberOfPlayers = nil
local t = {}
repeat -- This part will keep asking until you put in an actual number
numberOfPlayers = tonumber(read())
until numberOfPlayers
for i = 1, numberOfPlayers do
local _, player = os.pullEvent("player")
t[#t + 1] = player
end
return t
end
-- You dont have to wrap to the player detector
local teamOne = teamMaking()
But, I think this is a better way of doing it: (because its pretty straightforward)
local teamplayers = nil
repeat
teamplayers = tonumber(read())
until teamplayers
local redteam = {}
local blueteam = {}
for i = 1, teamplayers * 2 do
local _, player = os.pullEvent('player')
if i % 2 == 0 then
redteam[#redteam.+ 1] = player
else
blueteam[#blueteam + 1] = player
end
end
But its up to you really
215 posts
Location
Netherlands
Posted 25 June 2013 - 05:39 AM
To come back at those os.pullEvents, you need to have a while-loop, otherwise the event will not happen and the stuff under the os.pullEvent will happen. After some experimenting with tables I found out (I don't have the latest version of CC, wired modems are not implemented yet) that tables you send over rednet are strings. So now I send over the names via rednet and the PC will make a table out of it. Another computer will keep track of the score and time. I will post all of my foundings after it is done.
7083 posts
Location
Tasmania (AU)
Posted 25 June 2013 - 06:21 AM
To come back at those os.pullEvents, you need to have a while-loop, otherwise the event will not happen and the stuff under the os.pullEvent will happen.
Your while loop does not cause events.
After some experimenting with tables I found out (I don't have the latest version of CC, wired modems are not implemented yet) that tables you send over rednet are strings. So now I send over the names via rednet and the PC will make a table out of it.
You might find
textutils.serialize() useful. It can convert a table to a string that can later be converted back again.
215 posts
Location
Netherlands
Posted 25 June 2013 - 07:35 AM
To come back at those os.pullEvents, you need to have a while-loop, otherwise the event will not happen and the stuff under the os.pullEvent will happen.
Your while loop does not cause events.
After some experimenting with tables I found out (I don't have the latest version of CC, wired modems are not implemented yet) that tables you send over rednet are strings. So now I send over the names via rednet and the PC will make a table out of it.
You might find
textutils.serialize() useful. It can convert a table to a string that can later be converted back again.
I know a while loop will not cause events, but the os.pullEvent will keep going until something happens. That is what I meant to say.