Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kaway · Jul 08, 2017 at 10:03 PM · multiplayerscore

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; }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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;

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
       }
     }
   }
 }


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kaway · Jul 09, 2017 at 01:41 PM 0
Share

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 ???

avatar image kaway · Jul 09, 2017 at 01:47 PM 0
Share

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");
             }

avatar image Jwizard93 kaway · Jul 10, 2017 at 12:26 AM 0
Share

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.

avatar image Jwizard93 Jwizard93 · Jul 10, 2017 at 12:36 AM 0
Share

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"];

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

106 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Score/Points when player kills enemy (multiplayer) 1 Answer

View PvP scores on screen in real-time 1 Answer

multiply Score on Collision not working? check my code plz 2 Answers

Multiplayer ScoreBoard not Updating Correctly 0 Answers

multiplayer score profile 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges