|
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.
(comments are locked)
|
|
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
(comments are locked)
|
|
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. So you are saying the absolute fastest way (speed wise) is to do what I am trying to do is to do what I have done? Is calling on a method, like in the answer below, any faster than calling on a function like you were suggesting to use in order to code faster? Thanks for your reply.
Jun 16 '11 at 03:09 AM
Macdude2
The method below is calling a function. But generally you should go for what makes your code easiest to understand.
Jun 16 '11 at 03:39 AM
Eric5h5
(comments are locked)
|
