ID:161550
 
I'm pretty new to Byond coding, and trying to learn... Can't find anything that I can seem to use to get this working...

I want my obj to be displayed by two separate icons. Basically displayed in the world with Icon A, and displayed in the player's inventory with Icon B.

If anybody has any code examples, and can describe what it all means (so that I actually learn what's going on and how to do this type of thing without feeling like I'm just copying other people's chunks of code) I'd be very appreciative.
There are a number of ways to do this. I'm going to suggest that you just have the item change its icon (and/or icon_state) whenever it's moved. So, we'll give items variables to store this alternate icon and icon_state:

obj
//all items will fall under here
item
var/groundicon
var/groundstate
var/invicon
var/invstate

//and, as an example:
pants
groundicon = 'grounditems.dmi'
groundstate = "pants"
invicon = 'invitems.dmi'
invstate = "pants"


Simple enough. Now, we will have this change icon and icon_state in Move(), so that when it moves to a turf, it sets its icon to groundicon and icon_state to groundstate. When it moves to anything else, it will become invicon and invstate:

obj
item
Move(var/atom/newloc)
//first of all, perform the default action of movement
.=..()
//we only want to do anything if the movement succeeded
if(.)
if(isturf(newloc))
icon = groundicon
icon_state = groundstate
else
icon = invicon
icon_state = invstate


However, what you need to keep in mind is that items now need to call Move() in order to move around. If their loc is altered directly, their icons won't change. So, for example, get() and drop() will have to look like this:

obj
item
verb
get()
set src in oview(1)
src.Move(usr)
usr << "You pick up [src]"
drop()
set src in usr
src.Move(usr.loc)
usr << "You drop [src]"


No more difficult than adjusting loc directly. Now, there's one last thing: when creating the object, we need to set its icon properly. So, we'll put essentially the same thing we had in Move() into New():

obj
item
New(loc)
if(isturf(loc))
icon = groundicon
icon_state = groundstate
else
icon = invicon
icon_state = invstate
//perform the default action of New() (if any)
..()


And that should be it. You probably want to also set the icon and icon_state of the items, as well, so that they show up properly in the map editor.
In response to Garthor
obj
swordthingy
name = "sword"
worn = 0
icon = 'whatever.dmi'
verb
Wear()
if(src.worn == 1)
src:worn = 0
usr.overlays -= 'whatever.dmi'
usr << "You reove the [src.name]."
else
src:worn = 1
usr.overlays += 'whatever.dmi'
usr << "You wear the [src.name]."
Drop()
if(src:worn == 1)
usr << "Not while its being weilded."
if(src:worn == 0)
src.loc=locate(usr.x,usr.y,usr.z)
Get()
set src in oview(1)
src.loc = usr
usr<<"You picked up a [src]"
In response to RanEsu
RanEsu wrote:
> obj
> swordthingy
> name = "sword"
> worn = 0
> icon = 'whatever.dmi'
> verb
> Wear()
> if(src.worn == 1) //i think boolean vars are bad practice there... and later in the code
> src:worn = 0 // : doesnt belong there... and later in the code
> usr.overlays -= 'whatever.dmi'
> usr << "You reove the [src.name]."
> else
> src:worn = 1 /// : is wrong here
> usr.overlays += 'whatever.dmi'
> usr << "You wear the [src.name]."
> Drop()
> if(src:worn == 1)
> usr << "Not while its being weilded."
> if(src:worn == 0)
> src.loc=locate(usr.x,usr.y,usr.z)
> Get()
> set src in oview(1)
> src.loc = usr
> usr<<"You picked up a [src]"
>
In response to RanEsu
This code is horribly written and I can't see any relation between it and the question posed.
In response to Garthor
Good code, good explanation. But-

Garthor wrote:
.=..()
//we only want to do anything if the movement succeeded
if(.)
if(isturf(newloc))
</DM>

You don't need to override Move() all the time; you've got the cleaner, more object oriented Entered() and Exited() for these kind of things. So you could do this by overriding a pair of them in many combinations, I'd go for /mob/Exited() and /mob/Entered() (you could do atom/movable instead of mob, but it shouldn't be needed).

when creating the object, we need to set its icon properly. So, we'll put essentially the same thing we had in Move() into New():
You probably want to also set the icon and icon_state of the items, as well, so that they show up properly in the map editor.

Essentially, he will be using the 'ground icon' for the compile-time setting, so you only need half of the code block in New(); only check if the item is in a mob, and if so change the icon.
In response to Garthor
Thanks Garthor. I was able to simply change the icon and icon_state within the grab() and drop() commands I'm using, as I had previously though, but the vars are what I was missing. Thanks for the ideas and the detailed explanation! It really helped!
In response to Spartan206
It will do to put it in the verb itself, but putting it in a movement proc like I/Garthor said is preferable.