Get Set Return method, properties stuff?
What exactly is the point of this?
public int HelloThere(bool talking)
{
Return;
}
I understand variables just fine, how to change them, why you want to change them. But putting an int into a method or having a method return a bool. I do not understand why you would want to do it, why it's done and no matter how many videos I've watched in which people with a lisp are explaining it I still don't understand it. We want it to return a bool, return a bool huh mr smartypants to whom your mom?
It seems like every person trying to explain this assumes people understand what they mean by returning a bool. Now that we've decided to have the method receive a string and return an int we're going to set and get the method. Type set; then type get;
It's so mindbogglingly retarded. Can someone explain this in a non autistic way or perhaps paste a link to some practical example of this and how exactly you use it and what's the point of it. It's beyond annoying watching hours of tutorials on people trying to explain this but because they're so bad at explaining it just end up wasting my time. And if you decide to close this question because you think it's obvious then shame on snorty.
Answer by djordr · Apr 18, 2018 at 02:21 PM
I'll explain it in non autistic terms my friend. In the Start function below we want the answer, who wouldn't want the answer?! So we create an int called answer.
void Start ()
{
int answer;
}
What do we want the answer to? We want an answer to some math problem. So we create this function:
int MathProblem(int apples, int oranges)
{
}
We want the answer in our start function to be equal to how many apples and oranges there are and we want the math to take place in the MathProblem function. The int at the start of the function means that we want the return value of our function to be an int. I'll explain what I mean by the return value to be an int in just a second. The two ints we created called apples and oranges represent values we pass into the function. To pass values into the function we write in our start function this.
void Start ()
{
MathProblem(5,4);
int answer;
}
What this means is that the two ints we created in the MathProblem function called apples and oranges. Their value is going to be equal to 5 and 4. So there are going to be 5 apples and 4 oranges. Now let's figure out how many apples and oranges we've got.
int MathProblem(int apples, int oranges)
{
int fruit;
fruit = apples + oranges;
return fruit;
}
This is self explanatory, apples+oranges. The two values we passed into our function is going to be equal to a newly created int called fruit. Now we type return fruit; This is going to make the answer to our math problem equal to the int we want the function to return. The int answer; we created in start function we now want to make equal to our mathproblem function. So in Start we type:
void Start ()
{
int answer;
answer = MathProblem(5,4);
}
So the answer is going to be equal to 9. Yes you could just type answer = 5+4. But that math function could be a lot more complicated and you could pass in different values which would make the function return different answers. It works a lot like a calculator. I have yet to understand getters and setters because I have yet to come across someone who explains it in non autistic terms. So sorry this is all I got for now, hope you understand the purpose of passing variables into a function and returning something a bit better after my explanation.
Answer by Hellium · Apr 17, 2018 at 09:54 PM
I don't really understand what you don't understand...
Take the very simple function:
public bool AreYouAnAdult( int age )
{
if( age > 18 ) // Or 21 if you come from the USA
return true ;
else
return false;
}
This function takes an integer, which is your age, and tells you if you are an adult or not.
Then, in your game, supposing you have an input field where the user can type his/her age, the game will be able to indicate whether he/she is an adult or not by calling this function.
if( AreYouAnAdult( int.Parse( inputfield.Text ) ) // int.Parse will try to convert the string to an integer so that your function can be called
Debug.Log("You are an adult!");
else
Debug.LogWarning("You are not an adult (yet)");
The getters and setters (in any Object Oriented Language) are functions designed to set and get the values of instance's fields with some additionnal constraints (to keep the instance consistent).
private float playerHealth ;
// Create a getter in order to display the health of your player on the screen for example
public float GetPlayerHealth()
{
return playerHealth ;
}
// Create a setter to alter the health (if your player has been healed or hurt),
// but make sure the health is correct!
public void SetPlayerHealth( float newHealth )
{
if( newHealth >=0 && newHealth <= 100 )
playerHealth = newHealth ;
}
Properties (in C#) are just a nice syntax to write the getters and setters, but you can use simple functions has described before.
private float playerHealth ;
// Create a getter in order to display the health of your player on the screen for example
public float PlayerHealth
{
get // Nice syntax for the getter
{
return playerHealth ;
}
set // Nice syntax for the setter. Here, the name of the parameter is always `value`
{
if( value >=0 && value <= 100 )
playerHealth = value;
}
}