Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question Using LINQ for getting a sum

Discussion in 'Editor & General Support' started by TranceDreams, Oct 11, 2023.

  1. TranceDreams

    TranceDreams

    Joined:
    Apr 22, 2013
    Posts:
    5
    How do I use linq to get the total sum of 'max' from 'wilds' here?
    i'm aware of using for i loops, I want to know how to use Linq for this

    var wilds: List.<munsters>;
    var totalCount : int;

    class munsters {
    var beast: String;
    var min: int = 1;
    var max: int =1;
    }




    I'm aware of using linq like below for specific items in a list
    foo = someList.Find(function(c : GameObject) c.GetComponent //etc...

    I think there should be some kind of method with linq using sum but everywhere I search it uses C#
    Im still using unity script

    What would be the correct way of something along the lines of this
    totalCount = wilds.Sum(munsters => munsters.max);


    thank you
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    What year is this post from?! Are you really still using UnityScript?! In 2023??! :)

    I'm not sure, but isn't Linq actually only C# syntactic sugar? I can't imagine it would even exist in UnityScript...

    There's probably a really gnarly way to do it in UnityScript / JavaScript, I just haven't bothered to keep up with that kinda stuff, but it supports lambdas and delegates and chained-on stuff, object iterators, predicates, filters, mappers, etc., and that's all Linq is under the hood.

    ALSO... this is what ChatGPT-4 says about the matter:

    https://chat.openai.com/share/adc9d464-f4e1-47a1-a8ee-fab550c4bdec
     
  3. TranceDreams

    TranceDreams

    Joined:
    Apr 22, 2013
    Posts:
    5
    Thanks for replying! Yes, i'm super old school with my coding and unity version. The last time I upgraded resulted in tears and I vowed never to bother with C or upgrading again. Shoot I barely stopped using windows 7 not too long ago :)

    I happened across Linq by accident and I love using the where + find functions to pick and choose from things in lists. I know there has to be some keyword I'm missing when I google search or some kind of hidden archive of Linq examples for UntyScript out there, I only ask for help 2 seconds before I go mentally insane.

    The last post in the chat here is a great example of using linq in the correct format. I just dont know to format the C version of using sum to UnityScript
    https://forum.unity.com/threads/how-to-use-system-linq-in-unityscript.314973/

    Whoever posted the first answer here is basically what Im looking for but I can't figure out how to translate the syntax to replace the lambda operator. with a 'Function(x)' version
    https://stackoverflow.com/questions...of-objects-how-do-i-get-the-sum-of-a-property

    upload_2023-10-10_20-31-28.png
    this doesn't work
    totalCount = wilds.Sum(munsters => munsters.max);

    or this
    totalCount = wilds.Sum(function(c : munsters) c.max);
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    There has to be an actual "learn javascript" kinda page you can google up that goes over the syntax...
     
  5. TranceDreams

    TranceDreams

    Joined:
    Apr 22, 2013
    Posts:
    5
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Seems like once again, ChatGPT-4 to the rescue!

    EDIT: well, Chat got things wrong a LOT... try this:

    https://chat.openai.com/share/00e3acda-837d-4e1a-8b5e-3da07e7c9642

    Pay close attention: Chat said pretty much everything in both JavaScript and UnityScript, and there's more differences than I realized.

    This finally worked:

    Code (csharp):
    1. class PassingDelegates
    2. {
    3.     function Greet(person: String, formatter: Function): String {
    4.         return formatter(person);
    5.     }
    6.  
    7.     function FormalGreeting(name: String): String {
    8.         return "Hello, Mr. " + name;
    9.     }
    10.  
    11.     function CasualGreeting(name: String): String {
    12.         return "Hey there, " + name + "!";
    13.     }
    14.  
    15.     function Start() {
    16.         Debug.Log(Greet("John", FormalGreeting));
    17.         Debug.Log(Greet("John", CasualGreeting));
    18.     }
    19. }
    and then call it:

    Code (csharp):
    1.     var pd : PassingDelegates;
    2.  
    3.      pd = PassingDelegates();
    4.  
    5.     pd.Start();



    Seriously though man, you are holding yourself back bigtime. I hadn't thought of this language for ten years now and didn't realize how far behind the times it is. It's like using Fisher Price tools to build a house. You could do it... but boy it would be a lot of work.

    You really should consider porting to C# and then upgrading at least to LTS Unity. I know it's a huge jump and would take you down for a while, but at some point stuff isn't gonna work very well anymore. :0

    If and when you do upgrade, make a backup and keep these steps in mind:



    ISSUES RELATED TO UPGRADING PROJECTS (eg, changing to a higher Unity version)

    Upgrading to a later version of Unity is a one-way process. Any project that has been updated should NEVER be reverted to an earlier version of Unity because this is expressly not supported by Unity. Doing so exposes your project to internal inconsistencies and breakage that may actually be impossible to repair.

    If you want to upgrade to a newer version of Unity, do not even consider it until you have placed your project fully under proper source control. This goes double or triple for non-LTS (Tech Stream) versions of Unity3D, which can be extremely unstable compared with LTS.

    Once you have source-controlled your project then you may attempt a Unity upgrade. Immediately after any attempted upgrade you should try to view as much of your project as possible, with a mind to looking for broken animations or materials or any other scripting errors or runtime issues.

    After an upgrade you should ALWAYS build to all targets you contemplate supporting: iOS and Android can be particularly finicky, and of course any third party libraries you use must also "play nice" with the new version of Unity. Since you didn't write the third party library, it is up to you to vet it against the new version to make sure it still works.

    If there are issues in your testing after upgrading Unity, ABANDON the upgrade, revert your project in source control and be back where you were pre-upgrade with the earlier version of Unity.

    Obviously the less you test after the upgrade the more chance you will have of an undiscovered critical issue.

    This risk of upgrading is entirely on you and must be considered whenever you contemplate a Unity version upgrade.

    Do not upgrade "just for fun" or you may become very unhappy.

    ISSUES RELATED TO GOING BACK OR UNDOING BREAKING CHANGES TO YOUR WORK

    Keep in mind that Unity does NOT back your files up. That's on YOU.

    Use either version control (see below) or else make your own backups.




    PROPERLY CONFIGURING AND USING ENTERPRISE SOURCE CONTROL

    I'm sorry you've had this issue. Please consider using proper industrial-grade enterprise-qualified source control in order to guard and protect your hard-earned work.

    Personally I use git (completely outside of Unity) because it is free and there are tons of tutorials out there to help you set it up as well as free places to host your repo (BitBucket, Github, Gitlab, etc.).

    You can also push git repositories to other drives: thumb drives, USB drives, network drives, etc., effectively putting a complete copy of the repository there.

    As far as configuring Unity to play nice with git, keep this in mind:

    https://forum.unity.com/threads/prefab-links-keep-getting-dumped-on-git-pull.646600/#post-7142306

    I usually make a separate repository for each game, but I have some repositories with a bunch of smaller test games.

    Here is how I use git in one of my games, Jetpack Kurt:

    https://forum.unity.com/threads/2-steps-backwards.965048/#post-6282497

    Using fine-grained source control as you work to refine your engineering:

    https://forum.unity.com/threads/whe...grammer-example-in-text.1048739/#post-6783740

    Share/Sharing source code between projects:

    https://forum.unity.com/threads/your-techniques-to-share-code-between-projects.575959/#post-3835837

    Setting up an appropriate .gitignore file for Unity3D:

    https://forum.unity.com/threads/removing-il2cpp_cache-from-project.1084607/#post-6997067

    Generally the ONLY folders you should ever source control are:

    Assets/
    ProjectSettings/
    Packages/

    NEVER source control Library/ or Temp/ or Logs/
    NEVER source control anything from Visual Studio (.vs, .csproj, none of that noise)

    Setting git up with Unity (includes above .gitignore concepts):

    https://thoughtbot.com/blog/how-to-git-with-unity

    It is only simple economics that you must expend as much effort into backing it up as you feel the work is worth in the first place. Digital storage is so unbelievably cheap today that you can buy gigabytes of flash drive storage for about the price of a cup of coffee. It's simply ridiculous not to back up.

    If you plan on joining the software industry, you will be required and expected to know how to use source control.

    "Use source control or you will be really sad sooner or later." - StarManta on the Unity3D forum boards
     
    Last edited: Oct 11, 2023
  7. TranceDreams

    TranceDreams

    Joined:
    Apr 22, 2013
    Posts:
    5
    Very helpful! Thanks a bunch, I will play around with the code a little more. It's probable something very simple.

    I do like to visit the asset store still and I know eventually I'll have to upgrade to enjoy the fantastic goodies there, but it wont be until the bitter end. I will definitely keep a link to this page - I didn't study enough before I upgraded last time and having this will make it less daunting. Thanks again

    ps- Im using version 5.3.2 still! AND STILL learning it!
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Hey, look, if you're having fun, then you're having fun... I can't find any problems with that.

    Just know how much freer you would be to have fun and explore new language features and Unity functionality if you came ahead a little bit.

    I absolutely DO NOT advocate going past LTS. In fact, I still use a lot of older Unity myself so I get the issue. But I also use Unity2021 professionally and all of my code (old and new) is in C#.
     
  9. TranceDreams

    TranceDreams

    Joined:
    Apr 22, 2013
    Posts:
    5
    totalCount = wilds.Select(function(a : munsters) a.max).ToList().Sum();

    I was curious about the ChatGPT thing and it helped a lot.