ID:140671
 
Code:
mob/Stat()
statpanel("Gold")
stat("Gold: [usr.gold]")
golden:
if(usr.gold>=counter)
G = new
stat(G)
if(usr.gold>=usr.counter)
sleep(0.00001)
counter+=50
goto golden
usr.counter = 0
obj
gold
icon = 'gold.dmi'
mob
var
obj/gold/G
counter = 49


Problem description:
The goal was to make it display one gold object for every 50 peices of gold you have, but, my math was off somewhere and it always adds one extra gold simbolizer



Why are you using stat() and statpanel() outside of the Stat() proc?
In response to Nielz
sorry, forgot to include Stat() before it (in example, not game) but it is in Stat()
In response to Gigimoi
Oh, alright then
In response to Gigimoi
Oh, and you can't sleep() or spawn() for decimal durations
Just so you know
In response to Nielz
ive done it before..... several times.....
In response to Gigimoi
Well it doesn't cause errors or anything, but it will be rounded at runtime
In response to Nielz
oh..
In response to Gigimoi
And why would you want to sleep for a millionth of a second anyway?
In response to Nielz
Unless you would set world.tick_lag to 0.00001, but I don't think that's really possible :P
First off, this is a forum, not a chat room, you two.

Second, I don't see any sort of reasonable use for showing a gold "object" for each 50 you have... So if you have 5000 gold, you get 100 objects in your panel?
In response to Ephemerality
What on earth is your problem? We weren't "chatting"
Or are you just very anti-social?
In response to Ephemerality
yes... do you have a problem with that?
In response to Nielz
anti-social t.t
Anyway, I made a quick proc that calculates what you want without error:

mob/proc/convert_gold()
var
gold_objects
quotient
rest
divider = 50

quotient = src.gold/divider
rest = quotient - round(quotient,1)

gold_objects = rest >= 0 ? quotient-rest : (quotient-rest)-1

return gold_objects


Now if you really want to add objects in your stat panel, you can do it easily like this:

mob/Stat()
var/list/gold_objects = new()

for(var/objects = src.convert_gold(), objects, objects --)
gold_objects += new/obj/gold

statpanel("some tab", "gold objects", gold_objects)

..()

In response to Nielz
ok, i see, thanks
Well, that's a silly way of doing things...

mob/Stat()
var/obj/gold/G = new()
statpanel("Gold")
stat("Gold: [gold]")
for(var/v = 0, v < gold, v += 50)
stat(G)


Also: you DO realize you can declare variables INSIDE procs, and they don't ALL need to belong to an object, right?
In response to Garthor
yes, i just prefer doing it that way
In response to Nielz
Nielz wrote:
Anyway, I made a quick proc that calculates what you want without error:

Actually, the way you're trying to handle the remainder of division is pretty wrong and silly, and will not work right. All you should really do here is just use round(quotinent,1) and use that as the number, you don't have to do anything else - you don't even need to round up as far as this feature goes.

However, putting loops and object creations in Stat() is not so recommended because Stat() is called often, periodically, for every player, and there could be situations when it's called quite a lot. And you don't need to create many objects here in the first place - one is enough, then you can either loop calling stat() on that one object as many times as desired (look at Garthor's elegant solution), or even do it the list way by including multiple references (again, as many as desired) to that same object in the list and then calling statpanel() with that list.
Note that even in Garthor's example you can (and should) offload even that single object creation. Even by that, you will have a situation where multiple player's Stat() procs constantly create new objs every tick, meaning there's virtually always at least one dummy obj in existence anyway, so you're better off simply creating this obj once, storing it in a global variable for the remainder of the world session, then simply reusing that global object every time as needed.
This can be implemented very very quickly into Garthor's existing example:
mob/Stat()
var/global/obj/gold/G = new()
statpanel("Gold")
stat("Gold: [gold]")
for(var/v = 0, v < gold, v += 50)
stat(G)

Because the var is marked as global, it is only initialized once at world startup, and so it isn't reinitialized every time the procedure is called. Look up the 'global' modifier for more info.
Note that you don't have to use the modifier; the above example is identical in almost every way to the below one.
var/obj/gold/G = new()
mob/Stat()
statpanel("Gold")
stat("Gold: [gold]")
for(var/v = 0, v < gold, v += 50)
stat(G)
In response to Gigimoi
Garthor's advice is good though, because then you won't stuff the mob with variables that it doesn't need to have
Page: 1 2