ID:141474
 
Code:
mob/proc
hudproc()
serp
sleep(1)
for(var/obj/crhud/grenade1/A in usr.client.screen)
for(var/obj/crhud/grenade2/B in usr.client.screen)
for(var/obj/crhud/grenade3/C in usr.client.screen)
for(var/obj/crhud/grenade4/D in usr.client.screen)
for(var/obj/crhud/grenade5/E in usr.client.screen)
A.icon_state = "[usr.grenades]grenade"
B.icon_state = "[usr.grenades]grenade"
C.icon_state = "[usr.grenades]grenade"
D.icon_state = "[usr.grenades]grenade"
E.icon_state = "[usr.grenades]grenade"
goto serp


Problem description: Thats the code I have and it only works whenever I have 0 grenades or 5+ grenades however I cant understand why, I've tried recoding this 5 different ways and all of them have produced the same outcome, and before you say it's the icon_states believe me I checked them and thats not it.. Any ideas?

Just a couple recommendations:
1) Avoid using goto unless you have a reason to. Study up on flow control structures (if, else if, else, for, while)

2) The usr variable is not reliable outside of verbs and a procs that require client activity (Click, ect)

3) A little bit of OOP can allow you to program far less. Why bother creating 5 different types of grenade HUD objects when you can create 1 type and offset it's location slightly for the same effect.

mob/proc
hudproc()
while(1) //while 1 is true, and it always is
sleep(10) //Unless you're making a single player game, no sense in having a 0.1s sensitivity to this check.
//The difference here is that all the grenades will be of the same type. I'd recommend creating them and
//positioning them when a new player's mob is created (with icons corresponding to new char values)
//you could offset these with pixel_x and pixel_y values.
for(var/obj/crhud/grenade/A in src.client.screen)
A.icon_state = "[src.grenades]grenade"


Even still, there are better ways to do this, methinks.
In response to Vermolius
Vermolius wrote:
Even still, there are better ways to do this, methinks.

Like scratching on the infinite loop and calling the procedure only when the grenades var(iable) is altered? ;)
In response to Schnitzelnagler
Yeah, I was imagining something of the sort.