MonoDevelop: Problems with default parameters

Discussion in 'External Tools' started by Kathrin-Jennifer, Nov 20, 2010.

  1. Kathrin-Jennifer

    Kathrin-Jennifer

    New Member

    Joined:
    Mar 29, 2010
    Messages:
    128
    Hi,

    I'm using MonoDevelop that was installed with Unity for implementation. There seem to be a problem with default parameters. E.g.:
    Code (csharp):
    1. public myMethod( int parameterOne, int parameterTwo = 0)
    It is underlined red owing to the second parameter that has a default value. Does someone know how to fix this?
     
  2. afalk

    afalk

    New Member

    Joined:
    Jun 21, 2010
    Messages:
    165
    If you're defining a function, why have a definition in the declaration? if it was an if statement I could see that, or iterating in a for-loop. If you are hard-coding the value for parameterTwo why include it at all ? :D

    But back to the coding --> Wouldn't you simply encode this as:

    Code (csharp):
    1.  
    2.  
    3. public myMethod(int parameterOne, int parameterTwo) {
    4.      parameterOne = some value;
    5.      parameterTwo = 0;
    6.  
    7. }
    8.  
    9.  
     
  3. Metron

    Metron

    Member

    Joined:
    Aug 24, 2009
    Messages:
    715
    @afalk: Now that's someone who doesn't understand how default parameter values work...

    @Kathrin-Jennifer: C# doesn't allow for default parameters (unfortunately; well... C#4.0 can but that is not included in Unity ;) ). Instead you'll have to overload the functions in this way:

    Code (csharp):
    1. public myMethod(int parameterOne) {
    2.  
    3.      myMethod(parameterOne, 0);
    4.  
    5. }
    6.  
    7. public myMethod(int parameterOne, int parameterTwo);
     
    Marzoa likes this.
  4. konistehrad

    konistehrad

    New Member

    Joined:
    Apr 27, 2010
    Messages:
    2
    Has there been anything else on this topic? Unity3 does indeed have support for default parameters, and their interaction with MonoDevelop leaves a lot to be desired: it will stop allowing code completion after their usage and will fail to index the class.
     
  5. Pip Robbins

    Pip Robbins

    Member

    Joined:
    Oct 19, 2010
    Messages:
    213
    Anyone else know much about this? Default parameters do work for me, monodevelop leaves it underlined with red, but does not report any errors or warnings.
     
    Marzoa likes this.
  6. AudiOishi

    AudiOishi

    New Member

    Joined:
    Dec 6, 2010
    Messages:
    2
    @PipRobbins

    I'm having the same issue, and I would also like to know more.
     
  7. Kathrin-Jennifer

    Kathrin-Jennifer

    New Member

    Joined:
    Mar 29, 2010
    Messages:
    128
    @Metron: Thanks for the workaround. I had in mind that I already tried to overload the functions, but it seemed that I was wrong ;)

    It would be much better to have default parameters instead of overloading the functions. But I haven't found a solution for using it with monodevelop so far...
     
  8. gvaughan

    gvaughan

    Member

    Joined:
    Jan 28, 2010
    Messages:
    31
    I upgraded to MonoDevelop 2.6ß3 (2.5.92) today - seems to have solved this issue. No apparent bugs - yet :)
     
  9. TheDemiurge

    TheDemiurge

    Member

    Joined:
    Jul 26, 2010
    Messages:
    17
    Downloaded Unity 3.4.0 today thinking for some reason that there was a MonoDevelop upgrade from 2.4 to 2.5, but I was wrong. And any minor changes that may have come, did not fix this. I'll have to try upgrading MD on its own...
     
  10. Pip Robbins

    Pip Robbins

    Member

    Joined:
    Oct 19, 2010
    Messages:
    213
    It appears to be working now, although I noticed that mono-develop will not indicate that its optional in the inteli sense which is a pity.
     
  11. xefhs

    xefhs

    New Member

    Joined:
    Jul 16, 2011
    Messages:
    34
    I've exactly the same problem with unity 3.4.2f3.

    How I can solve that, please ? :confused:
     
  12. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    825
    I have found a temporary solution. For example, you use Visual Studio, that works fine with named and optional arguments, as a primary IDE, but switch to MonoDevelop from time to time for debugging. Just switch "target framework" of all your C# projects to Mono / .NET 4.0:

    [​IMG]

    But be prepared that every time you restart Unity, it will change it back to 3.5.

    PS
    Tested in Unity 3.5.0b6 and MonoDevelop 2.8.2
     
    Last edited: Jan 24, 2012
  13. xefhs

    xefhs

    New Member

    Joined:
    Jul 16, 2011
    Messages:
    34
    I've just upgraded to 3.5 Developer Preview, he comes with MonoDevelop 2.8.2 (I don't change the target framework to 4.0), and now that work fine.
    Thank you anyway alexzzzz !
     
  14. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    825
    MonoDevelop 2.8.2 doesn't highlight default parameters as errors, but still doesn't allow me to start debugging until I change the target framework, because first it tries to compile the code and fails.
     
  15. xefhs

    xefhs

    New Member

    Joined:
    Jul 16, 2011
    Messages:
    34
    Yes, I've also this error when I use Run > Debug, but even if I solve this by switching to Mono / .NET 4.0, MD tries to open a new Unity instance and that fail :(

    PS :
    You've you noticed ? Since the 2.8.X, Monodevelop don't handle anymore the Semantic Highlighting \0/
     
  16. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    825
    Debugging is only possible if Unity is launched by MonoDevelop. If it is already running, close it, and then start debugging.

    I wasn't aware the previous versions did. I use MonoDevelop for debugging only.
     
  17. xefhs

    xefhs

    New Member

    Joined:
    Jul 16, 2011
    Messages:
    34
    In this case, it's simpler to use built-in Unity Debugger.

    And just out of curiosity what code editor do you use ?
     
  18. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    825
    You mean Debug.Log ? This way you can't trace your code step by step, line by line.

    Microsoft Visual Studio
     
    Last edited: Jan 26, 2012
  19. xefhs

    xefhs

    New Member

    Joined:
    Jul 16, 2011
    Messages:
    34
    Yes, but it's enough in most cases.
     
  20. FuzzeWuzze

    FuzzeWuzze

    New Member

    Joined:
    Jan 28, 2012
    Messages:
    3
    Ive been having this problem recently as well, as the A* Pathfinding Project now uses these default parameters.

    The problem im having is that EVERY time i change it to Mono/.NET 4.0 in the options, when i debug the probject(or do anything really that runs the Unity Project) it always gets set back to 3.5

    Ive tried force saving on both Mono and Unity..but it always reverts back to 3.5 and its driving me insane, i cant even work on my game because its too frustrating resetting it every 30 seconds when i find a bug and have to debug a few times.

    You guys updating your Mono, how did you overwrite it to make Unity use the new one? By default i just had another Mono install...not sure how you set it up to debug into Unity...
     
    Last edited: Jan 28, 2012
  21. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    825
    Try to not close Unity once it has been launched by MonoDevelop; use its Start/Stop buttons.

    UPD: I mean Play button.
     
    Last edited: Jan 28, 2012
  22. FuzzeWuzze

    FuzzeWuzze

    New Member

    Joined:
    Jan 28, 2012
    Messages:
    3
    I have nothing but problems with Mono and debugging in Unity, i dont know if im doing something wrong or what.

    Step 1) Close Unity
    2) Start Debugger from Mono which starts Unity
    3) Start Unity project, do debugging.
    4) Stop Unity project, make changes to Mono code
    5) Unity will now not respond to any input on the Start/Stop buttons, you can do things in the menus, minimize/maximize...but cant start/stop or terminate Unity.
    6) Stop the debugger in Mono
    7) Kill Unity3d

    Sometimes it gets really bad where Unity wont let me kill it, or wont close..it asks if i want to save my Scene i hit yes then it does nothing...never closes so i task manager kill the process. Open up Unity again..BOOM my entire Scene is gone, not just not in Unity..but not in the game folder at all..its totally gone. This has happened MANY times to me and its infuriating, even with just a small prototype scene that takes 5 minutes to setup.

    It has exhibited this behavior across two windows installs, so i dont think its my system or something else causing it. I had to reformat a week or two ago and it does the same thing now on a fresh system as it did before on my loaded system.
     
    Last edited: Jan 28, 2012
  23. Agamemjohn

    Agamemjohn

    New Member

    Joined:
    Mar 9, 2012
    Messages:
    3
    I came here because I was having some Default Parameter issues as well, and noticed that none of you guys seem to be aware of attaching to the currently running Unity process to debug.

    It is much faster and will definitely improve your workflow.

    While having unity and monodevelop open, click the "Attach To Process" button in Monodevelop (It's right next to [Debug (F5)]). You can also find the command under the Run toolbar button.

    Then select Unity Editor (Unity), there will be a Process ID that will change each time you open Unity, but there should probably only be one process there.

    And voila! You're attached and debugging. You can attach to the process mid-game as well, if you need to set something up or examine some odd behavior in the middle of the game itself.

    Then, if you hit Stop (Shift+F5), Unity player continues normally, and you can reattach later if you like.

    You can even use this method to attach to Development/Script Debugging versions of StandAlone builds or even iOS builds (it attaches over the network if the iOS device is on wifi). I believe it doesn't work for Web Player.
     
    Last edited: Mar 9, 2012
  24. alexzzzz

    alexzzzz

    Member

    Joined:
    Nov 20, 2010
    Messages:
    825
    $Captura.PNG

    Uncheck this option and there's no need to change target framework to 4.0 anymore.
     
  25. genail

    genail

    Member

    Joined:
    Jul 2, 2012
    Messages:
    581
    I've found a nice solution. Try to put this script in Assets/Editor

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4. using System.IO;
    5. using System.Text.RegularExpressions;
    6.  
    7. class UpgradeVSProject : AssetPostprocessor {
    8.     private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) {
    9.         string currentDir = Directory.GetCurrentDirectory();
    10.         string[] slnFile = Directory.GetFiles(currentDir, "*.sln");
    11.         string[] csprojFile = Directory.GetFiles(currentDir, "*.csproj");
    12.  
    13.         bool hasChanged = false;
    14.         if (slnFile != null) {
    15.             for (int i = 0; i < slnFile.Length; i++) {
    16.                 if (ReplaceInFile(slnFile[i], "Format Version 10.00", "Format Version 11.00"))
    17.                     hasChanged = true;
    18.             }
    19.         }
    20.  
    21.         if (csprojFile != null) {
    22.             for (int i = 0; i < csprojFile.Length; i++) {
    23.                 if (ReplaceInFile(csprojFile[i], "ToolsVersion=\"3.5\"", "ToolsVersion=\"4.0\""))
    24.                     hasChanged = true;
    25.  
    26.                 if (ReplaceInFile(csprojFile[i], "<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>", "<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>"))
    27.                     hasChanged = true;
    28.             }
    29.         }
    30.  
    31.         if (hasChanged) {
    32.             Debug.LogWarning("Project is now upgraded to Visual Studio 2010 Solution!");
    33.         } else {
    34.             Debug.Log("Project-version has not changed...");
    35.         }
    36.     }
    37.  
    38.     static private bool ReplaceInFile(string filePath, string searchText, string replaceText) {
    39.         StreamReader reader = new StreamReader(filePath);
    40.         string content = reader.ReadToEnd();
    41.         reader.Close();
    42.  
    43.         if (content.IndexOf(searchText) != -1) {
    44.             content = Regex.Replace(content, searchText, replaceText);
    45.             StreamWriter writer = new StreamWriter(filePath);
    46.             writer.Write(content);
    47.             writer.Close();
    48.  
    49.             return true;
    50.         }
    51.  
    52.         return false;
    53.     }
    54. }
     
  26. MattRix

    MattRix

    Member

    Joined:
    Aug 23, 2011
    Messages:
    62
    Thank you genail! It's insane that Unity doesn't let you do that in the first place.
     
  27. atmuc

    atmuc

    Member

    Joined:
    Feb 28, 2011
    Messages:
    792
    @alexzzzz when you uncheck Build Project in Monodevelop it never builds in Monodevelop and you cannot see errors in Monodevelop. it would be nice it worked like build in unity and show compile errors in Monodevelop.

    @genail it is nice script but it does not work for <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> in csproj. is there any way to change target to 4.0 automatically?
     
  28. mateiasu

    mateiasu

    Member

    Joined:
    Mar 11, 2013
    Messages:
    19
  29. badben

    badben

    Member

    Joined:
    Jan 16, 2013
    Messages:
    51
    Finally! Thank you! :)
     
  30. MattRix

    MattRix

    Member

    Joined:
    Aug 23, 2011
    Messages:
    62
    I know this is super old and I responded to it before, but I just was setting up a new project and decided to revisit it.

    I ended up creating a new script, which is based on genail's but it's using a secret built-in callback I discovered. It turns out that "OnGeneratedCSProjectFiles" is called on AssetPostprocessors whenever the CS project gets generated. This makes it happen at the correct time: after the solution has been generated, but before it has been opened. When it was using OnPostprocessAllAssets, it would happen too late.

    So yeah, just stick this anywhere in your project and you're good to go.

    https://gist.github.com/MattRix/daf67d66227c61501397

    I also made a more advanced version that removes any unityprojs from your solution as well (they were throwing nonsensical errors for me), so I've put that up here in case anyone finds it handy: https://gist.github.com/MattRix/d3b602ef002c6dae9580