ID:149392
 
Can someone tell me why my code wont work?
obj/sword
layer = MOB_LAYER + 1
icon = 'sword.dmi'
wood
icon_state="wood"
inv
icon_state="wood inv"
var/obj/sword/wood/inv/I
Entered()
I.pixel_y+=32
usr.hitting=1
spawn(10)del I
spawn(10)usr.icon_state="get small"
spawn(10)usr.hitting=0
sleep(30)
I=new()

I can't find the problem.
Not meaning to sound rude, but are you able to explain exactly what all the code is supposed to do?


-Pola
In response to Polatrite
Yes. When you step on the sword it will move up 36 pixels and the mob will go into another icon_state and i used hitting=1 because i already have freeze movement defined on there, and sword=1 tells it that the usr has a sword and can use it, and after 3 sec it will come back. And he will hold the sword for 1 second.
In response to BurningIce
BurningIce wrote:
Yes. When you step on the sword it will move up 36 pixels and the mob will go into another icon_state and i used hitting=1 because i already have freeze movement defined on there, and sword=1 tells it that the usr has a sword and can use it, and after 3 sec it will come back. And he will hold the sword for 1 second.

Entered() is not going to give you this behavior. Entered is only called when something enters your sword (hence the name.) Stepping on the same square will do nothing.
BurningIce wrote:
I can't find the problem.

I can find one problem, though it might not be the full reason your code doesn't work: You're ignoring the argument to Entered() and using usr instead.

Never ever do this. Only use usr in verbs, unless you're absolutely crystal clear on how it works and that it's correct.

Entered() takes two arguments: The thing that just entered the turf (or any atom), and its old location:
turf
Entered(atom/movable/thing,atom/oldloc)
if(istype(thing,/obj/something))
DoSomething(thing)

Most people ignore the second argument because usually it's not very useful to you. The first, however, is pretty much indispensible. Substituting usr won't (usually) work, because the usr isn't always the thing that's entering.

Lummox JR
In response to Skysaw
So what should I use?
In response to BurningIce
Maybe you should redefine the movement macros (like client/North, client/South, etc.) so that each time you step, it checks to see if there is a sword in the new location, for example:
client/North()
mob.move(NORTH)
client/South()
mob.move(SOUTH)
client/East()
mob.move(EAST)
client/West()
mob.move(WEST)
//put northwest and others here

mob/proc/move(step_dir)
step(src,step_dir)
for(var/obj/wood/sword/inv/O in loc)
//do whatever here
break //stop so if there are two swords in the loc it only triggers one

This movement code can also be manipulated for many things (such as movement delays).
In response to WizDragon
I'd have to do that for every single item though. There has to be an easier way to do it.
In response to BurningIce
You could make all of them a general type, and use that type instead.
In response to WizDragon
WizDragon wrote:
Maybe you should redefine the movement macros (like client/North, client/South, etc.) so that each time you step, it checks to see if there is a sword in the new location, for example:
client/North()
> mob.move(NORTH)
> client/South()
> mob.move(SOUTH)
> client/East()
> mob.move(EAST)
> client/West()
> mob.move(WEST)
> //put northwest and others here
>
> mob/proc/move(step_dir)
> step(src,step_dir)
> for(var/obj/wood/sword/inv/O in loc)
> //do whatever here
> break //stop so if there are two swords in the loc it only triggers one

This movement code can also be manipulated for many things (such as movement delays).

I think it would be cleaner to cut out the client movement keys, and do it like this:
turf
Entered(atom/movable/A) // every time the turf is entered
if(ismob(A)) // ... by a mob
for(var/obj/O in src) // look at all the items here
O.trigger(A) // ... and deal with them
// note that I pass in the person who triggered it
// since it's likely you'll want that info

obj
proc
trigger() // default behavior is do nothing

wood/sword/inv
// define object here

trigger(mob/triggered_by) // the sword's version of the proc
triggered_by << "You stepped on [src], and sliced your foot in two!"