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;
}
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;
}
IN CLASS:
private int oldValue;
private int myNumber;
IN Start:
oldValue = myNumber = whateverValue;
IN Update:
if (myNumber == oldValue + 1)
{
// do whatever
oldValue = myNumber;
}
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;
}
}
}
}