ID:139274
 
Code:
obj
proc
feeding()
if(usr.incubators)
var/obj/Inventory/egg/O = locate(/obj/Inventory/egg) in usr.hatchstorage
var/obj/Inventory/eggfood/C = locate(/obj/Inventory/eggfood) in usr
if(O.hatchrate==5)
usr<<"This Egg has been fed to contempt. It is ready to be hatched!"
else
if(/obj/Inventory/eggfood in usr)
if(C.foodtype== O.eggtype)
if(C.amount >= O.foodreq)
var/n = O.foodreq C.removeAmount(n)
var/obj/Inventory/eggfood/D = new C.type
D.addAmount(n-1)
O.hatchrate++
usr<<"You have fed the Egg. ([O.hatchrate]/5)"


Problem description:
When you click on the Hatching Incubator it gives you an option to feed the egg, but when I do, nothing happens. :[
The proc is supposed to check and see if there is currently an Egg inside the Incubator (usr.incubators), then check if the user has food on them (eggfood) , then check to see if the Egg can "consume" that "brand" of food (foodtype/eggtype) and that the user has enough food for one serving(foodreq) since some Eggs will require more food than others. Then if everything checks out, the amount of eggfood in the users contents is removed equal to the require amount of food needed to feed the Egg. And then that Egg gains a Hatchrate, meaning its a step closer to hatching.

But it doesn't work...pretty sure it has something to do with the var/obj = locate thing, because I just threw that in there not knowing what to do...
This line:

if(/obj/Inventory/eggfood in usr)


Should be:

if(C in usr)
First, you should not be using usr in procs. Without the context of how you are using this proc, I can't say exactly what to replace it with, but most likely you should be providing an argument for the player. So, it would be:

obj/proc/feeding(var/mob/M)

mob/verb/feed()
var/obj/O = locate()
O.feeding(src)


Second, I do not know why you are defining the feeding() proc for all objs, or any obj at all. It's clearly an action that is either attached to some sort of feeding apparatus, or is simply performed by a mob itself. A big tip-off that a procedure is in the wrong place is that you are not using src in it at all (unless it's defined globally and therefore there would be no src, of course).

So, assuming you just move this proc to mobs, so that src is the player...

locate() is insufficient here. It gives you the first object of a given type found in a list, but you want to find multiple objects matching certain criteria, and so you will need to use a for() loop to find them all. So, you will need:

// Really, a locate() shouldn't be needed, as you could just have a direct reference to your egg somewhere
var/obj/Inventory/egg/O = locate() in hatchstorage
if(O)
// Find a sufficient amount of applicable food
var/list/foundfood = list()
var/foundamount = 0
for(var/obj/Inventory/eggfood/F in src)
if(F.foodtype == O.eggtype)
foundfood += F
foundamount += F.amount
if(foundamount >= O.foodreq)
break

// If we found enough food, then remove an appropriate amount
if(foundamount >= O.foodreq)
var/needed = O.foodreq
for(var/obj/Inventory/eggfood/F in foundfood)
var/tempamount = O.amount
// This assumes that removeAmount handles removing more than the current amount gracefully
F.removeAmount(needed)
needed -= tempamount

// Do whatever it is you do when you fed the thing here

else
src << "You don't have enough food"
In response to Garthor
Thank you guys. It works now, finished the whole incubator with this help. I probably should of listed more info instead of just the proc, but it works so theres no need now...