ID:141471
 
Code:
turf
var
states = null
c_state = 1
Click()
if(src.Owner == usr.key)
if(usr.ClickM == 1)
if(src.states)
if(c_state == length(src.states))
c_state = 1
else c_state ++
icon_state = states[c_state]
//an example of a turf WITH extra states.
dirt
icon='LOZTILES.dmi'
icon_state="dirt"
states = list("dirt","dirt2","dirt3","dirt4")
//an example of a turf without any extra states.
WoodFloor
icon='LOZTILES.dmi'
icon_state="woodfloor"


Problem description:
Hmm.. perhaps this is a simple problem, but I'm a simple person. I mean, I dabble a bit in other coding languages but nothing serious really.... Anyways, the problem is, I get this fun error every time I click on a turf that doesn't have a states list even though I'm trying to prevent that from happening. What with the checking to see if the list is even THERE.
(Said Fun Error Below):
runtime error: cannot read from list
proc name: Click (/turf/Click)
usr: Warlin (/mob)
src: WoodFloor (18,15,3) (/turf/WoodFloor)
call stack:
WoodFloor (18,15,3) (/turf/WoodFloor): Click(WoodFloor (18,15,3) (/turf/WoodFloor), "mapwindow.map", "icon-x=21;icon-y=10;left=1;scr...")
Not too sure what the problem is, but, you could also make your Click() create a list of all states at runtime and then switch to the next state in the list the same way, alleviating the problem, and making it so you don't need to write out every icon_state there is for each icon.

turf
var
c_state = 1
Click()
if(src.Owner == usr.key)
if(usr.ClickM == 1)
var/list/states = icon_states(src)
if(c_state == length(states))
c_states = 1
else c_state++
src.icon_state = states(c_state)


That should give you the exact same effect without having to write out the states manually.

EDIT: This will even work with icons that only have 1 state which will fix your initial problem.
In response to Satans Spawn
That does look much neater, I appreciate the help... Of course, you know there's always some catch. The problem is, I have states for multiple objects in the same Icon, I mean, I pretty much think of Icons as tilesets when it comes to turfs and objs.

There isn't really as issue so much as a runtime error whenever anything without a states list is called. I mean, the players won't see it, but it'll eat up space in dream daemon and if enough of those errors occur, of course, it'll shut down.
In response to WarLin
I see I see. Well, give me a little bit and I will actually plug your code into DM and make up a quick little world and test this out and see what I can come up with.
In response to Satans Spawn
The only solution that I have found so far is to write out a list containing the only state for the icons that don't change.

    WoodFloor
icon='LOZTILES.dmi'
icon_state="woodfloor"
states=list("woodfloor")


Like that.

I tried using a few other methods I could think of, but that's all that I could find. Maybe someone else will be able to come up with something better, or more efficient, but that's all I can think of.
In response to Satans Spawn
It'll have to do until I can figure something else out. Thanks for the help though~
In response to WarLin
icon_state = states[c_state]


should be indented once more so it's only executed if src.states is not null - because null isn't a list.
In response to Immibis
Immibis wrote:
> icon_state = states[c_state]
>

should be indented once more so it's only executed if src.states is not null - because null isn't a list.

Good catch on that indentation error. I never even thought it would have been something like that...