ID:2612271
 
I needed a way to randomize the background turf of my test game (trying to learn BYOND). So I utilized the New() and Pick() to pick a random icon state in my space turf.

All is fine and dandy until the other blocks in my world turn black.

I (think) that my turf icon_state might be overwriting the other turfs? Please correct me if I'm wrong.


Code:
turf
icon = 'space.dmi'
icon_state = "1"
layer = 1
New()
icon_state = pick("1","2","3","4","5")

floor
name = "Ground"
icon = 'floor.dmi'
icon_state = "ground_default"
desc = "This is the floor."
layer = 2


In the map editor


Once launched with the randomizer code


Once launched with the randomizer code removed (isolating the issue to the randomizer code)


I have tried adding layer = 1 to the base turf and layer = 2 to the other turfs with no success, if anyone has any idea on how I could remedy this issue it is greatly appreciated!
Here's a neat concept for you to learn: Polymorphism.

Basically, in BYOND, a child of a thing inherits all the behavior and properties of its parent thing.

/turf/floor is a child of /turf. This means that your floor types are inheriting all of the behavior of /turf by default. This means that your New() proc that you declared on /turf is also running on /turf/floor too, and since you don't have icon_states 1 through 5 defined on your floor icon's icon_states, they are showing up as a null icon state.

You can get around this in a few ways.

1) Override New() on floor without invoking the supercall to remove all parent behavior from New(). (This is not preferred)

turf
icon = 'space.dmi'
icon_state = "1"
layer = 1
New()
icon_state = pick("1","2","3","4","5")

floor
name = "Ground"
icon = 'floor.dmi'
icon_state = "ground_default"
desc = "This is the floor."
layer = 2

New()


2) Implement a heritable property to gate parent behavior: (This is a better way to go about this)

turf
icon = 'space.dmi'
icon_state = "1"
layer = 1
var
randomize_state = 1
New()
if(randomize_state)
icon_state = pick("1","2","3","4","5")

floor
name = "Ground"
icon = 'floor.dmi'
icon_state = "ground_default"
desc = "This is the floor."
layer = 2
randomize_state = 0


3) Move the behavior to an overridable proc, so that you can change this behavior per-type without having to cut off all behavior of the New() hook:

turf
icon = 'space.dmi'
icon_state = "1"
layer = 1
New()
initial_state()
..()
proc
initial_state()
icon_state = pick("1","2","3","4","5")

floor
name = "Ground"
icon = 'floor.dmi'
icon_state = "ground_default"
desc = "This is the floor."
layer = 2

initial_state() //override to do nothing


4) Change the polymorphic hierarchy so that behavior you don't want to happen is isolated in a separate chain of inheritance: (This is preferred)

world
turf = /turf/space //change the world's default turf to the new type

turf
space
icon = 'space.dmi'
icon_state = "1"
layer = 1
New()
icon_state = pick("1","2","3","4","5")
..()

floor
name = "Ground"
icon = 'floor.dmi'
icon_state = "ground_default"
desc = "This is the floor."
layer = 2