- Home /
Code Problem
I have been scripting to make a targeting thing. My code tells me I can't implicitly convert float to int. How do I fix this... This is my code:
public class BaseStat {
private int _baseValue;
private int _buffValue;
private int _expToLevel;
private float _levelModifier;
public BaseStat () {
_baseValue = 0;
_buffValue = 0;
_levelModifier = 1.1f;
_expToLevel = 100;
}
//Basic Setters and Getters
public int BaseValue {
get{ return _baseValue; }
set{ _baseValue = value; }
}
public int BuffValue {
get{ return _buffValue; }
set{ _buffValue = value; }
}
public int ExpToLevel {
get{ return _expToLevel; }
set{ _expToLevel = value; }
}
public int LevelModifier {
get{ return _levelModifier; }
set{ _levelModifier = value; }
}
Answer by CrucibleLab · Nov 09, 2012 at 11:18 PM
In this case, first you need to decide what type of numeric value you need returned; float versus int.
If it should return float, then you can try:
private float CalculateExpToLevel() {
return (float)_expToLevel * _levelModifier;
}
If it is int, then try:
private int CalculateExpToLevel() {
return _expToLevel * (int)_levelModifier;
}
Remember, the main idea is to perform arithmatics with the same numeric types. If you're dealing with mixed types like in your case, little bit of self-study will make you proficient in type casting, which is one of the fundamental concepts in programming.
Hope it helps. :)
Answer by CrucibleLab · Nov 09, 2012 at 09:08 PM
Quite simply, since your _levelModifier variable is of type float, make a quick change to your LevelModifier function as follows:
public float LevelModifier {
get{ return _levelModifier; }
set{ _levelModifier = value; }
}
Answer by Michael Covert · Nov 09, 2012 at 09:10 PM
You can't implicitly convert a float to an int, since you'll lose data (the trailing decimal information). You could explicitly convert it, which is done like this:
(int)myFloatValue
or, it seems more likely that what you're trying to do is actually just return the float, which would make your LevelModifier property look like:
public float LevelModifier {
get{ return _levelModifier; }
set{ _levelModifier = value; }
}
This way you'd be skipping that conversion entirely.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# or Objective-C 1 Answer
lookat wont track target 1 Answer