Posted 05 April 2014 - 04:33 PM
So, I'm making a Tamagotchi and I have come across a small problem. The (actual) program runs fine, but if I try to add some food with
I'm suspecting it has to do with the parallel code. If someone could fix my code and explain the problem, I would be very grateful.
rednet.broadcast("10", "foodinput")
the server (the code below) doesn't add the food specified in the
rednet.receive("foodinput")
line.I'm suspecting it has to do with the parallel code. If someone could fix my code and explain the problem, I would be very grateful.
-- [REDNET CONFIGURATION]
rednet.open("top")
-- [SETTING DEFAULT NEEDS]
age = 0
life = 1
health = 50
food = 50
happy = 50
-- [MULTITASKING FOR INPUTS]
while life > 0 do -- Heart of the pet, so if life is 0 it dies
core = function()
-- [AGE COUNTER]
age = age + 4
-- [NEEDS SECTION]
if food > 0 then -- When there is food, consume it for 0.1% health per update
health = health + 0.1
food = food - 0.1
end
if food == 0 then -- When no food is left, start starving by 0.05% health per update
health = health - 0.05
end
if happy == 0 then -- When happy runs out, health is taken away
health = health - 0.05
end
-- [ADDING/REMOVING NEEDS]
if redstone.getInput("left") then -- Add 10 to food per update if it gets a lever input from the left of the computer
food = food + 10
end
if redstone.getInput("bottom") then -- Add 10 to happy per update if it gets a pressure plate input from the bottom of the computer
happy = happy + 10
else
happy = happy - 0.05
end
-- [MAX AMOUNT CHECK]
if health > 100 then -- When health is over 100%, set it to 100%
if life < 3 then -- Add lives until maximum has been reached
life = life + 1 -- Add a life
health = 0.1 -- Set health to 0.1
else
health = 100 -- If lives are at maximum, just reset health to 100.
end
end
if food > 100 then -- When food is over 100%, set it to 100%
food = 100
end
if happy > 100 then -- When happy is over 100%, set it to 100%
happy = 100
end
-- [DEATH CHECK]
if health < 0.05 then -- When health (and lives) has reached 0, disable life, else set health to 100.
life = life - 1
if life > 0 then -- When health is 0%, remove one life (^) and then set the health to 100, and if not, set health to 0
health = 100
else
health = 0
end
end
-- [INFO BROADCAST]
rednet.broadcast(health, "health")
rednet.broadcast(food, "food")
rednet.broadcast(happy, "happy")
-- [SCREEN RESET]
term.clear()
term.setCursorPos(1, 1)
-- [PROGRAM INFO]
print("[VIRTUAL PET]")
print("created by Tom Beelen")
print("")
-- [STATISTICS]
print("[STATISTICS]")
print("Age in seconds:")
print(age)
print("Health in %:")
print(health)
print("Food in %:")
print(food)
print("Happy in %:")
print(happy)
print("")
-- [LIFE INDICATOR]
print("[LIFE INFORMATION]")
if life > 0 then -- If it is alive, print a message, if not, print the death message
print("Your pet is alive")
else
print("Your pet is dead")
end
print("Lives:")
print(life)
sleep(4) -- Refresh every 4 seconds - Note to self: If it dies too fast, make this longer (but also add to the age counter)
end
inputs = function()
foodinput = function()
ignore, foodreceived = rednet.receive("foodinput")
food = food + foodreceived
end
happyinput = function()
ignore, happyreceived = rednet.receive("happyinput")
happy = happy + happyreceived
end
parallel.waitForAny(foodinput, happyinput)
end
parallel.waitForAny(core, inputs)
end
Edited on 05 April 2014 - 05:29 PM