Is there a more efficient way to check to see if a value is bewteen to other values than doing :
it would be nice if you could do something like
Is it possible to create a function like this? Thanks for any help.
asked Jun 15 '11 at 06:11 AM
Of course. :) It isn't possible to declare a function that looks exactly like what you have above, however, because of 2 things:
What you need is a method that takes a single float, and then a variable number of sets of floats, in between which you want the first one to be. You can accomplish that with the
This method can be called like so:
The use of a Vector2 might seem odd. I chose it because it helps force the user to always supply sets of two floats. If I had declared the method to only take
answered Jun 15 '11 at 06:54 AM
You're not asking about integers...integers are whole numbers and don't have fractions. Anyway, you can make a function, but it will be less efficient because of the overhead involved in calling a function. What you wrote first is probably the most efficient way it can be done, since the compiler will short-circuit conditions where appropriate. For example, if scrollPosition.x is less than Screen.width*.25, it won't bother evaluating the next condition, since the first one already failed.
If you're talking about "more efficient" in terms of less code, rather than fastest speed, then you can make a function by passing in those parameters, but you can't use "||" in the parameters like that. Pass them in normally, and use && and || as appropriate in the code inside the function.
answered Jun 15 '11 at 06:44 AM