- Home /
Check with an int Increase 1
Hi, I have a little problem, I want to Check When Int increases by 1 to add 100 to the score EXAMPLE
if ((int)PhotonNetwork.player.customProperties["Kills"] + 1){ money =+ 100; }
Answer by Jwizard93 · Jul 09, 2017 at 12:44 AM
IN CLASS:
private int oldValue; private int myNumber;
IN Start:
oldValue = myNumber = whateverValue;
IN Update:
if (myNumber == oldValue + 1) { // do whatever oldValue = myNumber;
}
Answer by durnurd · Jul 09, 2017 at 03:05 AM
Here's a nice overly-complicated answer:
I'm assuming player.customProperties
is a Dictionary<string,object>
or something similar. A clean way of doing this without needing to check every frame would be to add a property indexer on player
itself. If the player is responsible for keeping track of money, then it can just stop there:
class Player {
private Dictionary<string, object> customProperties;
public object this[string key] {
get {
return customProperties[key];
}
set {
var oldValue = customProperties.ContainsKey(key) ? customProperties[key] : null;
customProperties[key] = value;
if (key == "Kills") {
money += ((int)value - (int)oldValue) * 100;
}
}
}
player.customProperties["Kills"]
becomes player["Kills"]
whenever you are modifying its value.
Otherwise, if another class is required to track money, then include a custom event callback to notify the observer. For example:
class Player {
public delegate void CustomPropertyChangeCallback(string key, object oldValue, object newValue);
public event CustomPropertyChangeCallback CustomPropertyChanged;
private Dictionary<string, object> customProperties;
public object this[string key] {
get {
return customProperties[key];
}
set {
var oldValue = customProperties.ContainsKey(key) ? customProperties[key] : null;
customProperties[key] = value;
if (CustomPropertyChanged != null)
CustomPropertyChanged(key, oldValue, newValue);
}
}
}
And then in your class that keeps track of Money:
class MoneyTracker {
void Start() {
PhotonNetwork.player.CustomPropertyChanged += PropertyChanged;
}
void OnDestroy() {
//Clean up after ourselves in case we are destroyed before Player is.
PhotonNetwork.player.CustomPropertyChanged -= PropertyChanged;
}
private void PropertyChanged(string key, object oldValue, object newValue) {
if (key == "Kills") {
int delta = (int)newValue - (int)oldValue;
if (delta > 0) {
money += delta * 100;
}
}
}
}
And there is no way that "If" detects if there is a change in value? An example would be as If((int) PhotonNetwork.player.customProperties ["kills"] =+ 1) or something like that?
Do not take reference to the Player "kills" Osea my question is, there is no way to detect by the IF () If an INT has increased or decreased its value? As for example would commonly be Public int value; If (value + = 1) Which rose 1 to its value or how it would be Public int value; If (value - = 1) Which decreased 1 to its value Is there any way to achieve this ???
If in a given case I do this? It could work I could recognize that Player ("kills") increased a value
if ((int)PhotonNetwork.player.customProperties["$$anonymous$$ills"] == (int)PhotonNetwork.player.customProperties["$$anonymous$$ills"] + 1){
money ++ ;
Debug.Log ("ADD $$anonymous$$oney");
}
No that is not going to work an integer never equals itself plus another integer.
You HAVE to store the old value and compare it with the new value in order to detect a change in value.
Well you don't HAVE to but this is the way to go about this. It's a completely trivial solution. $$anonymous$$aybe some information on why you don't want to do this would help..
//+= and -= are not logical operators they actually increment and decrements the value stored in the variable and cannot be in an if-statement.
Check it out:
if ((int)PhotonNetwork.player.customProperties["$$anonymous$$ills"] == oldValue + 1)
{
money++;
Debug.Log ("ADD $$anonymous$$oney");
oldValue = (int)PhotonNetwork.player.customProperties["$$anonymous$$ills"];
}
And at the start of the game:
oldValue = (int)PhotonNetwork.player.customProperties["$$anonymous$$ills"];