ID:2090037
 
(See the best response by Ter13.)
Hello everyone. Yesterday i was trying, to make a flower pot, but faced with the problem: I can't update sprite for it. Here's the code:

Code:
/obj/structure/garden/pot/updateicon()
if(!contain)
icon_state = "pot"
else
if(contain == /obj/item/decoration/flower/blue)
icon_state = "pot-blue"
if(contain == /obj/item/decoration/flower/red)
icon_state = "pot-red"
else
world << "<span class='notice'>[contain] found no acceptable sprite</span>"


And i always have no sprite for it. I even debugged it. It said, pot has "Blue flower" in it (which is /obj/item/decoration/flower/blue), but i got no acceptable sprite, again.

Where I did something wrong?
I'm assuming that contain is set to an atom, and not set to a type. Think of it like you're directly comparing "The Flow" to "/the_flower's_type/".

There's a few ways around this, using your current method you could do something like
if(contain.type (...)
But that's probably bad practice. It'd be a far better idea to instead have a basic flower type, and a var which dictates which type it is (assuming they all have the same basic behaviour). Then you could just check that var instead, and it'd cut down on a whole bunch of programming if you decided to add say, another 10 flower types.
Can i have a tutorial link about types and stuff, please? I don't even know, how to set and get types.

Thank you.

P.S I have this as my var for content

var/obj/item/decoration/flower/contain = null
There isn't really any guide, they're somewhat an abstract concept but I'll try my best and Ter13 or Kaio will swoop in and whoop me for spreading misinformation. You don't think they will, but just wait, I'll get whooped.

Effectively there are datums (or atoms, it's probably easier for you to remember them as atoms for now), and these are actual things within the game. Your flower pot, your flower, your grass. These are all of the 'atom' type, which stands for Area, Turf, Object, Mob. They're all basic types of 'things'. You can elaborate on the basic atom type further as you see fit, as you have done with '/obj/item/decoration/flower/blue'. This lets you do things with inheritance, encapsulation and basic object orientated programming. These might seem like big long terms, but trust me if you programmed what you did above, you already have a grasp of them.

The reason why your if condition fails is because the object itself isn't the object's type, right? "If <object> == <an object's type>" isn't going to work. You need to check the object's type.

There's a few related type procs, typesof() is probably the most commonly used one, it searches for all children of a type and returns a list of them. That lets you easily check a type against another set of types, so you could do something like
if(contain.type in typesof(/obj/item/decoration/flower/red))

To find if the contained object was a child of the red flower's type.

Setting types is far easier, it's something you've already done! The type is literally what you type out as the type. Uh, ok. Hard to explain I guess, but it's super simple!

obj
door //Here I just made a new atom, door. I set its type to be /obj/door
density = 1
wall //Here's a wall, /obj/wall
opacity = 1
density = 1
fake_wall //Here's a fake wall, it inherits from wall, so its type is /obj/wall/fake_wall
density = 0


That's all there is to setting a type, although there is also parent_type, which lets you make something inherit from something different to what its path says, for example

obj
wall //this is /obj/wall, it inherits from the /obj type

door
parent_type = /obj
//this is /door, but it still inherits from the /obj type, crazy right?
Best response
@rushnut: istype()

if(contain.type in typesof(/obj/item/decoration/flower/red)) //don't ever do this.

if(istype(contain,/obj/item/dectoration/flower/red)) //do this instead



Anyway, let's to away with some ugliness here and instead use some stuff DM does automatically for us to our advantage. Icon structure will let you do away with a lot of code and updating things. Just make sure that everything that has a valid pot state is part of the pot icon. Better yet, use overlays. But this will work just fine:

/obj/structure/garden/pot/updateicon()
if(contain)
icon_state = contain.icon_state
else
icon_state = null


Now, make sure that /obj/structure/garden/pot has its own icon. The null state should be an empty pot icon state.

For any object you can put into the pot, just make sure that the pot has an icon_state named the icon_state of that object.

If the pot doesn't have a matching icon_state in its icon to the object it contains, DM by default will fall back to the blank "null" state. This means that automatically, the pot will appear empty with any unsupported object in it.
Wow, thank a lot, everyone!
This helped a lot, pot is working now.
In response to Ter13
Ter13 wrote:
//don't ever do this.

Whooped