ID:144612
 
Code:
turf
grass
icon='LOZTILES.dmi'
icon_state="g1"
DblClick()
if(icon_state == "g1")
icon_state = "g2"
if(src.icon_state == "g2")
icon_state = "g3"
if(icon_state == "g3")
icon_state = "g4"
if(icon_state == "g4")
icon_state = "g5"
if(icon_state == "g5")
icon_state = "g6"
if(icon_state == "g6")
icon_state = "g7"
if(icon_state == "g7")
icon_state = "g8"
if(icon_state == "g8")
icon_state = "g9"
if(icon_state == "g9")
icon_state = "g1"


Problem description:For some reason this bit of code doesnt work at all and its really grinding my gears :\ Its supposed to change the icon state when you click it

It doesn't APPEAR to do anything because each if causes the one after it to execute, causing it to always be set to "g1" at the end.

To make this a bit clearer, try imagining what happens when you double-click on it: if the icon state is "g1", it gets set to "g2", then it checks to see if it is equal to "g2", and sets it to "g3", and so on.

The best solution is to simply add "else" before all the if lines except the first:
if(icon_state == "g1")
icon_state = "g2"
else if(icon_state == "g2")
icon_state = "g3"
...


That will cause it to skip the rest of the if statements after one of them executes.
did you try making all the ifs say
if(src.icon_state == "g1")


replace the g1 with w/e it is...thats all i can think of when i look at it right now though
In response to Destroy
Thanks.
In response to Destroy
It'd be much more space effecient and easier to read if you have a switch() statement also.

switch(icon_state)
if("g1") icon_state="g2"
if("g2") icon_state="g3"
...
In response to Mega fart cannon
Mega fart cannon wrote:
It'd be much more space effecient and easier to read if you have a switch() statement also.

> switch(icon_state)
> if("g1") icon_state="g2"
> if("g2") icon_state="g3"
> ...
>


icon_state = "g[text2num(copytext(icon_state,2))+1]"


:p
In response to DivineO'peanut
@_@
What's next?
" icon_state="auto" "?
ha.
In response to DivineO'peanut
That doesn't work for the "g9" icon_state.
Ungh, that sucks. A lot. I don't do this a whole lot, but try and use this snippet.

turf/grass
grass
icon = 'LOZTILES.DMI'
icon_state = "g1"
DblClick()
var/state_num = text2num(copytext(icon_state,2)) + 1//This will convert the last character into a number,
//which has one added to it.
if(state_num > 9) state = 1 //If the number is greater than nine, set it equal to one.
icon_state = "g[state_num]" //And finally set the grass' icon_state equal to its state number.


And don't copy/paste.
In response to Popisfizzy
Popisfizzy wrote:
Ungh, that sucks. A lot. I don't do this a whole lot, but try and use this snippet.

> turf/grass
> grass
> icon = 'LOZTILES.DMI'
> icon_state = "g1"
> DblClick()
> var/state_num = text2num(copytext(icon_state,2)) + 1//This will convert the last character into a number,
> //which has one added to it.
> if(state_num > 9) state = 1 //If the number is greater than nine, set it equal to one.
> icon_state = "g[state_num]" //And finally set the grass' icon_state equal to its state number.
>


You want state_num = 1, not state = 1.
In response to DeathAwaitsU
Er, whoops. Well, that's one reason people shouldn't copy and paste. =P
In response to DeathAwaitsU
DeathAwaitsU wrote:
That doesn't work for the "g9" icon_state.

Actually, it does. Since I convert the text to a number, and the <code>copytext()</code> copies from the 2nd character to the (edit, for you dum strict people) end of the string. :P

Here, try!
icon_state = "g[text2num(copytext(icon_state,2))+1]"
world << icon_state
In response to DivineO'peanut
If you want to get technical, then

icon_state = "g[text2num(icon_state)]"


Alone should work, setting as how text2num generally strips off all non-number characters. And no, copytext() doesn't copy to the infinite character, it copies (by default, when end is 0) from the specified start (or 1 if not specified) to 0, which stands for 1 past the end of the string.
In response to Audeuro
<code>copytext(icon_state,2)</code> will copy the text from the 2nd character to the end of the string, which could be infinite.
In response to DivineO'peanut
Please explain how the string could ever have an infinte length.
In response to DivineO'peanut
DivineO'peanut wrote:
DeathAwaitsU wrote:
That doesn't work for the "g9" icon_state.

Actually, it does. Since I convert the text to a number, and the <code>copytext()</code> copies from the 2nd character to the (edit, for you dum strict people) end of the string. :P

Here, try!
> icon_state = "g[text2num(copytext(icon_state,2))+1]"
> world << icon_state
>


you might want to use :

> icon_state = "g[text2num(copytext(icon_state,2,3))+1]"
> world << icon_state
>


that way the number would be 1,2,3..8,9,1,2,3...
instead of 1,2,3,4,5...,8,9,10,11,12,13. you only want the 2nd char to carry over.

just to nitpick. as is, the number would keep climbing.
In response to Jik
Oh, so that's what DAU meant when he said it doesn't work with 9, I thought he said it wouldn't add up after 9... D:

/shame