Fields or Locals?

Monkey Forums/Monkey Programming/Fields or Locals?

rIKmAN(Posted 2013) [#1]
I am using Select...Case statements to differentiate between devices to allow me to calculate the correct values to use for drawing/updating various components in my game.

Am I better off using a single Select...Case and Fields within each Class to store the correct values, or using a Select..Case and Locals within every Method to do it?

I am wondering if there would be any performance difference between the 2 when considering Methods like OnUpdate() and OnRender() that are called every frame.

Is it completely negligable and I'm thinking too much? :)


muddy_shoes(Posted 2013) [#2]
I'm not entirely sure what you mean. The performance difference between accessing a local and a field is going to be minor, but the difference in creating the locals repeatedly could be significant depending what that creation involves.

I'd question what it is that you're calculating every frame for device-dependent values anyway. The device isn't going to be changing so why is any calculation necessary?


rIKmAN(Posted 2013) [#3]
Hey muddy,

It's mainly differering x/y values for drawing text and images that aren't their own objects (ie. just a DrawImage), which obviously change depending on the resolution of the device.

I should encapsulate them into objects, set the relevant x/y using a single Select...Case at the start and then use Self.x/Self.y throughout.
I am doing this with other objects already, so I've no idea why this didn't occur to me until I read your post.

This is the 3rd time this week I have made an idiotic post on here, I really should stop coding this late (4am) as my brain gives up on me and I turn into a simpleton lol!

Thanks for the nudge! :)


Paul - Taiphoz(Posted 2013) [#4]
use auto fit ?

and storing those calculated values in a field is always going to be faster than calculating them every frame for some local.


rIKmAN(Posted 2013) [#5]
I am using auto-fit, but I still need to account for different aspect ratios.

As I said it was late and my brain had stopped working - I am in the process of re-writing the bits that I had done arse upwards, my brain had gone numb and as soon as I read the reply from muddy it clicked that I was doing it all wrong.

Lesson learnt, go to bed earlier lol!