Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IntField - Resetting / Not holding value

Discussion in 'Scripting' started by Failosipher, Oct 31, 2013.

  1. Failosipher

    Failosipher

    Joined:
    Oct 15, 2013
    Posts:
    53
    Hi, Please take a look at the following code.

    The window displays, and has 3 int fields. They are all set to 0. You enter any number, and click away, and it will reset to 0. I've been bashing my head against this problem for over a week now. I've added serialization. I do not know what else to do.

    Can anybody please offer some help? I can't be the only person using intfields in their editor, right?

    Code (csharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class NewMap : EditorWindow
    5. {
    6.    
    7.     private NewMapWindow myWindow;
    8.    
    9.    
    10.     [MenuItem ("THDME/New Map")]
    11.     static void Init() { GetWindow(typeof(NewMap)); }
    12.    
    13.     void OnEnable ()
    14.     {
    15.     if (myWindow == null)
    16.             myWindow = new NewMapWindow();
    17.     }
    18.    
    19.    
    20.     void OnGUI()
    21.     {
    22.         myWindow.OnGUI ();
    23.  
    24.     }
    25.    
    26.    
    27. }

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System;
    4.  
    5. [Serializable]
    6. public class NewMapWindow
    7. {
    8.     [SerializeField] private int width;
    9.     [SerializeField] private int height;
    10.     [SerializeField] private int num_floors;
    11.  
    12.    
    13.     public NewMapWindow()
    14.     {
    15.     width = 0;
    16.     height = 0;
    17.     num_floors = 0;
    18.  
    19.     }
    20.    
    21.    
    22.     public void OnGUI()
    23.     {
    24.         width = EditorGUILayout.IntField("Width: ", 0);
    25.         height = EditorGUILayout.IntField("Height: ", 0);
    26.         num_floors = EditorGUILayout.IntField("Number of Floors: ", 0);
    27.    
    28.        
    29.     }
    30. }
    31.  
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You're not using the variable in the IntField, but are constantly setting it to 0 instead.

    --Eric
     
  3. Failosipher

    Failosipher

    Joined:
    Oct 15, 2013
    Posts:
    53
    Wow. Really.

    So the value it returns, is also the value in the parameter for the IntField function? Is it just me, or is this really weird?

    P.S. Thanks.
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Yes you are correct, if you like to see the old value in the field before editing and not 0 , so you have to say the value in the field is default the old value.
    Just do it like this:

    Code (csharp):
    1. num_floors = EditorGUILayout.IntField("Number of Floors: ", num_floors);
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not weird, it's how all functions like this work in Unity. If it's going to return a value, there's no other way it could work. The alternative would be for it not to return a value, but instead use the variable as a ref variable.

    --Eric
     
  6. Failosipher

    Failosipher

    Joined:
    Oct 15, 2013
    Posts:
    53
    I see what you mean. I just thought there would be something like a refresh call. I've just never seen a function that just loops into itself like that. I like how it works though, and this is good to know. Thanks so much for your help!!!
     
  7. Lupinus

    Lupinus

    Joined:
    Feb 5, 2016
    Posts:
    16
    I'd have to agree with Failosipher... it is definitely weird to me. I've worked with a few programming languages and I'd have expected the IntField "int Value" Parameter to just be the default value shown in the field.... not actually setting the value permanently. i.e. .intfield("text to display", TemporaryDefaultValueReplacedbyUser).

    I'm really glad this forum exists, or I'd have gone batty by now, learning the difference in how these functions work. :)
     
  8. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    no this is perfectly fine.

    The property functions in EditorGUILayout (and similar functions in EditorGUI, EditorGUIUtility, GUI, etc.) don't hold state. (well some like ScrollView do... but for the sake of simplicity assume they don't) you need to provide them the state that they should be at when they are drawn.

    passing the field back like this also gives the scriptor flexibility. for one example you can validate the result and see if its an acceptable value. if not roll back to the previous value.
     
  9. Lupinus

    Lupinus

    Joined:
    Feb 5, 2016
    Posts:
    16
    Makes logical sense... just isn't something I'd be used to so some learnins are needed for some of us. :) Thanks for the clarification and the example.