1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

Sort Int List

Discussion in 'Visual Scripting' started by Djupo, May 1, 2018.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. Djupo

    Djupo Trainee Engineer

    Messages:
    13
    Hi, I'm trying to sort a list in vs tool using bubble sort algorithm but can't get it to work. :/

    Here's my code. Sry for quality. I tried to fit everything in the same window lol.
    [​IMG]

    Here's one example of the code I try to build in visual:


    Any advices? I have tried out different things for example insert method. Also i'm new to using lists so any tips are welcome!
     
    Last edited: May 2, 2018
  2. Malware

    Malware Master Engineer

    Messages:
    9,867
    Your image is missing :)
     
  3. Djupo

    Djupo Trainee Engineer

    Messages:
    13
    Oh... now it should be visible. :p It showed in my computer. Silly dropbox links...
     
  4. Stollie

    Stollie Apprentice Engineer

    Messages:
    136
    The very first thing that sticks out to me is you are using the 'once' script at the start with 'for' loops. The once script means the functions after are only going to run one cycle so its going to capture the first # and stop.

    The other thing is Sequence is misleading, it doesn't runs from A to Z starting at the first thing you have in a sequence. It actually runs every single thing on there at once as part of one frame update cycle. So essentially at the same time you are adding you are also going through a for loop cycle at the same time, not cycle 1 - add 8, cycle 2 - add 3 etc etc.

    They just made that node so you can have cleaner scripts as it definitely doesn't run things in sequences. Think of it more like a forking river than separated streams.
    --- Automerge ---
    If you want a good example of different types of lists used (including int) in VS go to C:\Program Files (x86)\Steam\SteamApps\common\SpaceEngineers\Content\VisualScripts\Station419 and open the 'Station419_SMS_DroneWaves.vs' file.
     
  5. Djupo

    Djupo Trainee Engineer

    Messages:
    13
    I tried to fix it but it still doesn't work like expected. I can't post image right now but will later. I removed the once script and sequences. I also initiated the list like in the example you posted and made sure the values are there. I think the problem lies in the part where I try to swap values in the list. For some reason it outputs duplicated values e.g. 1 3 3 8 8 8... Maybe I'm doing the nested for loop in wrong way?
     
  6. Stollie

    Stollie Apprentice Engineer

    Messages:
    136
    Put the file up on drop box and give me a detailed explanation of what it is you're trying to do. I'll take a look and see if I can fix it for you.
     
  7. Djupo

    Djupo Trainee Engineer

    Messages:
    13
    I'm working on a racing scenario and I'm trying to do a sort function to sort best player times from lowest to highest. So this algorithm called bubble sort would go through all values in the list and compare them two at a time and swap the values if needed. So when the for..loop is over the list should be in order. :)

    Here's the sorting part of my code: https://www.dropbox.com/s/mcnbzl6xjktafd4/Sorting.rar?dl=0
    Thanks for your help!
     
  8. Stollie

    Stollie Apprentice Engineer

    Messages:
    136
    Hey,

    So I had a crack at it, I couldn't get it to work easily, sorry, I did get it to work without for loops and just two scores (click link), so maybe you can scale that up.

    Couple of things I noticed that may help you. When you use 'Insert', it doesn't 'Remove' what you just inserted elsewhere, it actually inserts and pushes whatever slot was beneath that down one, so you'll probably have to feed the 'RemoveAt' command in there as well. Also, the 'Last' connector on a For Loop seems to go last number + next slot, the next slot being 0 so you may need to use an arithmetic node to do -1 if you use this connector.

    I have learnt through trial and error that VS scripts don't like you using 'Constant' nodes as integers so just make a 'Integer Variable' if you need to do something with arithmetic to minus 1 or set a limit to something like top 10 scores and 'Remove' anything on the list below that with a for loop.

    What you're trying to do sounds simple in theory but is actually quite complicated using VS scripts due to its lack of agility in working with Lists.

    To be honest I think its doable, I just don't have enough knowledge myself with for loops and I'm unwilling to spend hours figuring it out :p

    Another example you can look at which might help that uses most of these commands together is located at: -

    C:\Program Files (x86)\Steam\SteamApps\common\SpaceEngineers\Content\Campaigns\Official Campaign 01\Scripts\Mission 05

    Open up OC01_M5_OLS_DroneControl.vs and have a look at how they have used lists on the bottom right.

    You might be better off just asking the guys in the modding channel how to do a C# script for it, they may have a code snippet laying around somewhere that does something similar.
     
    Last edited: May 4, 2018
  9. Djupo

    Djupo Trainee Engineer

    Messages:
    13
    Oh cool. Thanks for the help! I'll look into it in next week. I thought too there were something odd happening with the "insert" command. I already tried to put "removeAt" there but it messed the order. I'll try to play with it more.

    Yes, if everything fails I think I try to make C# script or ask someone if they could do it. It's just kind of fun to try to problemsolve this. :D

    Once again thanks for your help. This has given me much more understanding on how things work in visual. :)
     
  10. Stollie

    Stollie Apprentice Engineer

    Messages:
    136
    No worries, any time :D
     
  11. Djupo

    Djupo Trainee Engineer

    Messages:
    13
    I did it! :woot: Followed your advice on using RemoveAt and variables instead of constants and also debugged my code writing chat messages almost after every command lol. Finally got it to work no matter what values were put on the list.

    I link the code here if somebody ever happen to need it. :) https://www.dropbox.com/s/d0i9sp2yg5d7imd/BubbleSortInVisual.rar?dl=0
     
  12. Stollie

    Stollie Apprentice Engineer

    Messages:
    136
    WEWT!!! Victory! :D
     
Thread Status:
This last post in this thread was made more than 31 days old.