Best method for checking if varible is something

Hi all,

What would you say is the best method to check when a variable has reached a certain number. In my case I have assigned a networked int to entities but I want to run functions on that entity when the networked int I set reaches 0.

Would I use Think hooks?
Would I use for k,v in pairs?

What should I use?

Thanks,
Computer600

Dear Computer600,

I’d probably use a Think hook.

Kind regards,
EvacX

I wouldn’t. If you’re networking the value, then you’re setting the value meaning you know the value it is going to be set to… If you know the value when you set it, why waste extra processing power to figure it out somewhere else?

If the client needs to run something and the server sets the value, if there are no on-change/callbacks for what you’re using, simply set a quick-net-message saying it’s reached 0…

I assume that since he is using networked variables that he needs the client to be aware of the value at all times, then who the hell cares if he uses a few processor cycles to continuously check if that value is 0?

We care because we want him to be a good coder. Encouraging bad practices is a bad idea long term.

That’s not bad practice. Part of being a programmer is being able to determine the performance impact of a piece of code and in this case it’d be completely unnoticeable and not worth spending 2 more seconds wasting your time on, regardless of your method.

A good programmer will spend time to optimize his program, Wirth’s law is true because of people like you :stuck_out_tongue:

I agree it won’t be noticeable but still… if there is a better way then do it, don’t be lazy.

You’re wrong and you should feel bad. Premature optimization is the root of all evil (not that it really applies much to this case, I am speaking in general just like you are).

The guy is clueless, he shouldn’t be wasting his time pondering about performance.

And hey, guess what? There’s always a better way, ideally the guy shouldn’t even use bare net messages, that’s technically lazy programming in and of itself.
It’s dangerous, unmanaged and can cause collisions.

A “good” programmer would write a replacement to DTVars that allows you to tie unlimited variables to an entity as well as allow you to hook in on signals and events in general and this can go on and on, but I don’t see anyone doing this or yelling at people for not doing it.

What he should be practicing is writing maintainable and understandable code, not waste his time with worthless issues like this.

This isn’t a “worthless” issue since you want to keep track of variables fairly often. (Hence why he is asking it as a general question) Thus it’s a good thing that he asks this on a forum so he gets informed about the best way of doing it.

Anyway let’s not derail this, I think he got the answers he needs and now just has to make a decision.

Last time I checked extra information is never a problem, nor do I think it’s really up to you guys to tell him he should only be looking at the simplest solution instead of trying to learn a more efficient or more proper way. I taught myself lua without a mentor and it took me years to teach myself optimization tricks because I derived them myself or found them through reading. It is important to have gradual exposure to proper concepts even if you can’t initially understand them.