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

Using sleep() in a program with Rednet inputs

Started by TomRBify, 05 April 2014 - 02:33 PM
TomRBify #1
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

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
CometWolf #2
Posted 05 April 2014 - 07:39 PM
holy crap what a way to program! I'd suggest you learn to use events instead of running the default functions in parallel like crazy. On the same note, define your functions prior to the while true do loop, otherwise you're program will redefine them everytime it loops, wasting tremendous computer resources. Id also suggest using elseif or else statements when your checking the same variable. And one last thing, parallel can use as many functions as you want in one call, there's no need to run more functions in parallel inside another function in order to "compress" them into one function.
http://computercraft...ki/Os.pullEvent

As for your actual problem, i'd suggest doing some debugging by adding some prints at the part you receive messages. Eg. print(happyreceived), to see if you are actually receiving the message at all.
Also, sleep() isn't the issue, as that means it yields, thus allowing the other parallel functions to run.
Edited on 05 April 2014 - 05:44 PM
TomRBify #3
Posted 05 April 2014 - 09:43 PM
Thanks for the tip! I'll get to it tomorrow, after getting some sleep(28800) :)/>
EDIT: Make that, when I get time in the holidays from school.
Edited on 07 April 2014 - 06:36 PM
TomRBify #4
Posted 10 April 2014 - 03:14 PM
I think I got it:

-- [REDNET CONFIGURATION] - Remember to remove unneeded configs
rednet.open("top")
rednet.open("bottom")
rednet.open("left")
rednet.open("right")
rednet.open("front")
rednet.open("back")
-- [SETTING DEFAULT NEEDS]
age = 0
life = 1
health = 50
food = 50
happy = 50
-- [SETTING FUCTIONS]
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.waitForAll(foodinput, happyinput)
end
-- [MULTITASKING FOR INPUTS + HEART]
while life > 0 do -- Heart of the pet, so if life is 0 it dies
parallel.waitForAny(core, inputs)
end
apemanzilla #5
Posted 10 April 2014 - 06:49 PM
rednet.receive takes a number as the timeout, not a string.
CometWolf #6
Posted 10 April 2014 - 07:03 PM
First arg is used as a protocol filter if it's a string in 1.6
Edited on 10 April 2014 - 05:06 PM
apemanzilla #7
Posted 10 April 2014 - 10:39 PM
First arg is used as a protocol filter if it's a string in 1.6
Bahh. I should really learn the new rednet system.
Edited on 10 April 2014 - 08:39 PM