ID:139165
 
Code:
obj/IceTrap
icon = 'trap.dmi'
icon_state="trap"
Enter(A)
if(ismob(A))
var/mob/M = A
var/mob/X = src.owner
if(M.client)
usr.TrapFreeze()
X.trapsout--
del src
else
if(istype(A,/obj/)) return


Problem description: I was wondering why this doesn't work, I know it works as turf/IceTrap but when you spawn a new turn it deletes the current turf on that X,Y,Z. I was wondering if there is anyway to make Enter() work for an object, or am I just being stupid?

Enter() is like a true or false, permit or deny type of thing. Entered() is what you want. Entered() is called when something goes into it. It works for turfs because you go into a turf like you go into a room, you go into an obj like you go into a bag.

So what you could do is every time a turf is entered, you can check if there is an obj in it. Then trigger something if there is.

turf/Entered(mob/M) // Anytime a turf is entered by a mob
..()
for(var/obj/Traps/T in src) // look for any /obj/Traps in the turfs contents
T.TrapTriggered(M) // Call TrapTriggered on anything it found and preserve the mob as an argument

obj/Traps/proc/TrapTriggered(mob/M) //Create the general base for the proc

obj/Traps
IceTrap
TrapTriggered(mob/M) // override proc so its specific to IceTrap.
if(M.client)
M << "Ice"
del src
First of all, Enter() is called when something or someone is trying to enter it and it outputs either 1 or 0, which stand for permit or deny to enter it. To do something when something or someone already entered it, you should use Entered(). Another thing is, both of them are only called when something is has entered or is trying to enter src.contents, thus if trap is an obj, it will only call Entered() or Enter() if something entered it's contents, like adding things to the chest. One workaround is to use turf's Entered() proc and call trap's activation proc there. I made a little code, if it has flaws, please tell me.
turf
Entered(M)
for(var/obj/trap/T in src.contents)
if(T)//checks if there is a trap on tile
T:Activate(M)//calls trap's Activate() proc

obj
trap
spike
icon='materials.dmi'
proc/Activate(M)//this is a proc that does stuff when trap is activated
world<<"[M] activated trap"//a little check if code is working
In response to Martys1103
You're not checking to see what M might possibly be in Entered() and you're also using the colon operator for some reason.

It'd be wise to give /obj/trap the proc Activate() instead of redefining it for each child of /obj/trap.

turf
Entered(mob/M)
if(istype(M))
for(var/obj/trap/T in src)
T.Activate(M)

obj
trap
proc
Activate(mob/M)

spike
Activate(mob/M)
M << "Pokey!"

pit
Activate(mob/M)
M << "Fallin'!"


Something to that effect.
In response to LordAndrew
Thank you for showing me the flaws of my code, I hope I won't do them in the future.
In response to LordAndrew
That actually worked perfectly, thanks for your help!