- Home /
code path returning error
I am working on a character generation script. Depending on the body type chosen, either petite, average or large, you get a stat bonus for dexterity or intelligence or strength. It all looks right, but I'm getting a BodyModifier(int, int, int, int): not all code paths return a value error.
BodyModifier( _bodytype, _dexterity, _strength, _intelligence); //Call is here
int BodyModifier( int _bodytype, int _dexterity, int _intelligence, int _strength)
{
_bodymodifier = 1;
Debug.Log("Bodytype" + _bodytype);
Debug.Log("Dex=" + _dexterity);
if(_bodytype == 1)
{
Debug.Log("Previous Dex: " + _dexterity);
_dexterity = _dexterity + _bodymodifier;
Debug.Log("Dex is increased" + _bodytype + _dexterity);
return _dexterity;
}
if(_bodytype == 2)
{
Debug.Log("Previous Int:" + _intelligence);
_intelligence = _intelligence + _bodymodifier;
Debug.Log(_bodytype + "Intelligence is increased" + _intelligence);
return _intelligence;
}
if(_bodytype == 3)
{
Debug.Log("Previous Str" + _strength);
_strength = _strength + _bodymodifier;
Debug.Log(_bodytype + "Strength is increased" + _strength);
return _strength;
}
}
I did use the code sample button, please forgive me if it's not quite right. Can anyone tell me where the problem is? Thanks!
Answer by rutter · Apr 20, 2012 at 07:08 PM
The compiler is performing some basic static analysis on your code. Only it's taking a very simplistic view, somewhat like this:
int ReturnAnInt()
{
if (something) return 1;
else if (somethingElse) return 2;
}
What happens if both of those conditions fail? Do we still return a number? We promised to, so the compiler is a little confused.
Now, it could be that your program is written such that at least one of those conditions will always be true; the compiler has no simple way of knowing that, so it assumes it's possible for each branch to be taken (or not) and examines the outcome.
The simple way to resolve this is with some "failsafe" case that will probably never happen, but which satisfies the compiler:
int ReturnAnInt()
{
if (something) return 1;
else if (somethingElse) return 2;
else return -2;
}
Or:
int ReturnAnInt()
{
if (something) return 1;
else if (somethingElse) return 2;
//this should never happen
return -2;
}
Or, if you're really confident:
int ReturnAnInt()
{
if (something) return 1;
else if (somethingElse) return 2;
throw new System.InvalidOperationException();
}
Any of the above should work. Pick your favorite?
Thanks! I added else if statements and a final else at the end where I simply return 0.
Answer by RustyShackleford · Apr 20, 2012 at 07:16 PM
Not all code paths return a value - you need to add something incase all the if statements fall through. If you're sure _bodytype will always be 1,2, or 3 just add a return 0; at the bottom of your function.
Your answer
Follow this Question
Related Questions
C# Function that returns instantiated object 1 Answer
Get enum value out of function c# 2 Answers
C# Return Type Error? 1 Answer
type function not returning. c# 2 Answers
Multiple Cars not working 1 Answer