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

Setup visual studio for SE programming

Discussion in 'Programming (In-game)' started by gothosan, Jul 2, 2016.

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

    gothosan Junior Engineer

    Messages:
    723
    Latest update finally broke SE Workbench which I used for writing scripts, King, the user who made SE Workbench was last seen online few months ago and so I'm not sure if when when he will fix it.
    I'm now trying to setup VS for SE but so far I've failed.
    I'm not sure what files I need to refernce to and what Using statements to use, I have tried to follow an old guide I found in the forum but it did not help..
     
  2. Whiplash141

    Whiplash141 Junior Engineer

    Messages:
    964
    Now I have no clue if any of what I did is redundant because I'm used to Notepad++ but the following worked for me:

    References:
    Sandbox.Game.dll
    SpaceEngineers.Game.dll
    Vrage.Library.dll
    Sandbox.Common.dll
    Vrage.Math.dll
    Vrage.Game.dll

    My coding template
    Code:
    using System;
    using System.Collections.Generic;
    using VRageMath;
    using VRage.Game;
    using VRage.Library;
    using System.Text;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.Common;
    using Sandbox.Game;
    using VRage.Collections;
    using VRage.Game.ModAPI.Ingame;
    using SpaceEngineers.Game.ModAPI.Ingame;
    
    namespace ScriptingClass
    {
        class MyScript
        {
            IMyGridTerminalSystem GridTerminalSystem = null;
            IMyGridProgramRuntimeInfo Runtime = null;
            Action<string> Echo = null;
            IMyTerminalBlock Me = null;
            //=======================================================================================
            //////////////////////////START//////////////////////////////////////////
            //=======================================================================================
    
            void Main()
            {
    
            }
    
            //=======================================================================================
            //////////////////////////END//////////////////////////////////////////
            //=======================================================================================
        }
    }
    
    
    Again I think some of this might be redundant but it works :)
     
  3. gothosan

    gothosan Junior Engineer

    Messages:
    723
    for me it does not work, I get many warnings (43 in fact) some about the framework being at higher version than targeted version and some about indirect dependency of other files.
    --- Automerge ---
    How do I add the refernces in Notepad++?
     
  4. Inflex

    Inflex Developer Staff

    Messages:
    397
    Notepad++ is pure text editor with some basic syntax highlighting but noting more. No intellisense, no compirer or DLLs loading.

    Assuming you would use Visual Studio (Community edition is available here for free: https://www.visualstudio.com/) setting up environment for making Space Engineers in-game scripts is quite easy.
    All you have to do is to follow steps on screenshots below.

    1) These steps make new Visual Studio project and setup references to SpaceEngineer DLLs to make intellisense work.
    [​IMG]

    2)These are optional. In case you are interested in fully working compiler without annoying warnings follow these too.
    [​IMG]

    3)And finally use code bellow as template for your scripts
    Code:
    #if DEBUG
    using System;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    
    using VRageMath;
    using VRage.Game;
    using VRage.Collections;
    using Sandbox.ModAPI.Ingame;
    using VRage.Game.Components;
    using VRage.Game.ModAPI.Ingame;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.Game.EntityComponents;
    using SpaceEngineers.Game.ModAPI.Ingame;
    using VRage.Game.ObjectBuilders.Definitions;
    
    namespace SpaceEngineers
    {
        public sealed class Program : MyGridProgram
        {
    #endif
            //=======================================================================
            //////////////////////////BEGIN//////////////////////////////////////////
            //=======================================================================
    
            public Program()
            {
                // The constructor, called only once every session and
                // always before any other method is called. Use it to
                // initialize your script.
            }
    
            public void Main(string args)
            {
                // The main entry point of the script, invoked every time
                // one of the programmable block's Run actions are invoked.
    
                // The method itself is required, but the argument above
                // can be removed if not needed.
            }
    
            public void Save()
            {
                // Called when the program needs to save its state. Use
                // this method to save your state to the Storage field
                // or some other means.
    
                // This method is optional and can be removed if not
                // needed.
            }
    
            //=======================================================================
            //////////////////////////END////////////////////////////////////////////
            //=======================================================================
    #if DEBUG
        }
    }
    #endif
    
    


    ! For VS 2017 Users !
    1) During installation process make sure to include ".NET Desktop development" package into required packages.
    It is possible to install this package later via VS installation modifier.

    2) When creating new project make sure to choose ".NET Framework" C# class library. Never choose ".NET Standard" nor ".NET Core" for SE development. [​IMG]
     
    Last edited: May 14, 2017
    • Informative Informative x 5
    • Friendly Friendly x 1
  5. gothosan

    gothosan Junior Engineer

    Messages:
    723
    Thank you very very very much :D
    It works now :)
    --- Automerge ---
    Question: How can I ignore certian errors in VS?
    It gives me ambiguous reference errors and cannot conver errors for code that ingame is in fact valid and compile with no problem.
     
  6. Burillo

    Burillo Junior Engineer

    Messages:
    648
    just did that, and got "ambiguous references" errors (between ModAPI and Ingame API). any ideas on how to fix those?
     
  7. Inflex

    Inflex Developer Staff

    Messages:
    397
    Last edited: Jul 3, 2016
  8. Malware

    Malware Master Engineer

    Messages:
    9,867
    Make sure you set up your scripting project to use .NET 4.6.1 in the project properties.
    --- Automerge ---
    Just make sure you have usings in place only for the ingame interfaces and not their ModAPI equivalents, because you don't have access to those for scripts anyway.
    --- Automerge ---
    Just make sure you have usin
    Ingame is the one you should use and not the pure ModAPI namespaces.
     
    • Agree Agree x 1
  9. Inflex

    Inflex Developer Staff

    Messages:
    397
    I would like to ask for some reliable list of usings actually that are actually presented for ingame compiller. One located here: https://steamcommunity.com/sharedfiles/filedetails/?id=709010443 should be fine for new development branch, but it's actually not.
    At least "System.Linq" and "System.Text.RegularExpressions" are not there and i have to manually prefix types from these namespaces.
     
  10. Malware

    Malware Master Engineer

    Messages:
    9,867
    @Inflex @Burillo @gothosan

    This is the actual code template used by the game:
    Code:
    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using VRageMath;
    using VRage.Game;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.Game.EntityComponents;
    using VRage.Game.Components;
    using VRage.Collections;
    using VRage.Game.ObjectBuilders.Definitions;
    using VRage.Game.ModAPI.Ingame;
    using SpaceEngineers.Game.ModAPI.Ingame;
    public sealed class Program : MyGridProgram{
    #line 1 "Program"
    	// YOUR SCRIPT GOES HERE
    }
    
    
     
    • Like Like x 2
  11. gothosan

    gothosan Junior Engineer

    Messages:
    723
    Error code CS1061 IMyTimerBlock does not contain a definition for 'ApplyAction' ...
    what am I missing?
     
  12. Malware

    Malware Master Engineer

    Messages:
    9,867
    Try my template, you're missing a using.
     
    • Like Like x 1
  13. Inflex

    Inflex Developer Staff

    Messages:
    397
    @Malware
    Thx, that was fast. Why is this list so different from whitelist in official guide?
     
  14. gothosan

    gothosan Junior Engineer

    Messages:
    723
    nvm fixed.. Using sandbox.modapi.ingame
     
  15. Malware

    Malware Master Engineer

    Messages:
    9,867
    Whitelist is not the same as included usings. The whitelist tells you what you're allowed to use. We cannot include every namespace out there. Linq, for instance, is deliberately left out as Keen is notoriously anti-linq... it's just too easy to use wrong and get a bad performance hit, I guess.

    Also don't mix the ModAPI and the Ingame Script whitelists, they are very different.

    If I had made the original version of this, there would have been an actual class in the bottom there, allowing you to write whatever extra usings you needed.
     
  16. Inflex

    Inflex Developer Staff

    Messages:
    397
    @Malware
    Ok, makes sense. Anyway there's one more thing I'd like to ask. I haven't heard anything about deprecation of CurrentMethodCallCount in RuntimeInfo but after update to new in-game compiler whatever i try to throw on in-game compiler and execute CurrentMethodCallCount gives me 0. So I'd like to ask whether is this performance counter no longer supported or there's a bug in game or new compiler is really aggressive in inlining and I am just not trying hard enough.

    Imho instruction counter works just fine and there's no need for another performance counter that's punishing Linq users so I would vote for removing method call counter. But that's only my POV and Keen may have some reasons for keeping this one.
     
  17. Malware

    Malware Master Engineer

    Messages:
    9,867
    No, it's not deprecated - it just works as expected now. Try to run a method in a nested fashion. That counter was supposed to address stack overflows, but it didn't decrease the count again in the old compiler. I replaced it with a system that counts up as it enters a method, and counts back down when it exits. Oh, and inlining never affected this counter, and it certainly won't now since all injection is finally done on the source code rather than the compiled code, so you can finally use all C# features without the ILInjector making your script crash on you.

    I do agree that the naming of that method is no longer accurate, but hey... I must admit that I completely forgot it was available ingame - even if it was me who wrote that part too :p
     
    • Funny Funny x 1
  18. Inflex

    Inflex Developer Staff

    Messages:
    397
    Although I get the point that I misunderstood meaning of this property based on experiences from previous compiler version I'm still not able to make CurrentMethodCallCount return anything else than 0. As you can see on following snippet I tried some "nested fashion" method calling but CurrentMethodCallCount does not seem to work as intended.

    Fun part is that when I tried to find maximal recursion depth 990 seemed to be that number but more times I edit and recompile that script the lower I have to make recursion depth to make script "not too complex". Now it's 950 and still getting lower and lower. (Instruction cap is not exceeded for sure.) Maybe there are some restrictions on execution time and SE are getting slower and slower after each compilation. Anyway it's weird.

    Code:
    void Recusion(int i)
    {
        String s = new String(new char[0]);
        s += " LETS MAKE";
        s += " SOME FAKE";
        s += " INSTRUCTIONS" + new Random().Next();
    
        Echo(Runtime.CurrentMethodCallCount.ToString() + " " + i + s);
    
        if (i > 0)
            Recusion(i - 1);
    }
    
    void Main(string arg)
    {
        Recusion(950);
        Echo("DONE" + Runtime.CurrentInstructionCount);
    }
    
     
  19. Malware

    Malware Master Engineer

    Messages:
    9,867
    No there are no other restrictions, so there must be a bug. I'll need to check this out.

    First of all, I misremembered again. I made the new depth counter completely separate so the CurrentMethodCallCount is really obsolete. I must remember to actually tag it as such - or ask Keen to do it themselves, that's probably faster. But if I need to submit a bugfix anyway, I can do it there.

    For all intents and purposes you don't need to keep track of this counter anyway.
     
  20. Inflex

    Inflex Developer Staff

    Messages:
    397
    I was just curios if this counter is relevant anymore. I personally really like Linq and C# properties but with old method-call-counting I was not able to use them in bigger scripts because of exceeding call-count limit. Now when I know that there is no such counter anymore I can freely use them again I'm happy as a kid in a candy store.

    Thx for your time and keep up the good work
     
  21. Xeviar

    Xeviar Trainee Engineer

    Messages:
    3
    This is great stuff thank you, now I just have to learn what the heck to do with it.
     
  22. Neunzehn

    Neunzehn Trainee Engineer

    Messages:
    58
    This may be trivial but we can't use visual studio to actually compile the program, right? The point of using visual studio is that it's much easier with all the auto correction and function check that sort of thing.
    I'm trying to convert to visual studio from sublime text 2, and I'm not sure all the set up is worth it.
     
  23. Bleuhazenfurfle

    Bleuhazenfurfle Apprentice Engineer

    Messages:
    284
    I have Visual Studio, but I actually use and love vim for, well, pretty much everything, including K#. Been using it since forever, and I've yet to see a useful IDE with vim-compatibility-mode, so IDE's are mostly relegated to debugging in my world. Still, I'm somewhat amazed no one's put together a module for Visual Studio that lets you write just the exact code you'll be putting into your PB, maybe even with some measure of simulation. From what I've heard, I'm certain it's capable of it.
     
  24. Inflex

    Inflex Developer Staff

    Messages:
    397
    Actually it is. When you complete both steps from my image guide VS is capable to compile your script against the game DLLs and tell you with 100% accuracy whether is your script syntactic right and will be able to run and you don't have to copy&pate your code to the game to check it.
    The only thing VS is currently not capable of is actually running your script like in game environment because there is currently no way to connect VS and game session. That's the only limitation VS currently have.

    Sublime is great for casual scripting of experienced coders who don't need external help. For scripts under lets say 500 lines its good but from that point things tend to get messy and eye of an external checker comes handy. ;)
    I'm not sure how installation process of VS Express looks now since I use Enterprise and after that years it became routine process for me but it used to be 3 click procedure so nothing hard.

    Little more lightweight than standard VS is "new" product from MS called VS Code. It's super light "IDE" and it's free too. It was capable of parsing my Space Engineers VS solution and loading and reading all DLLs so you may find it ideal for you when you find out how to set it up right.
    --- Automerge ---
    You mean code without the using directives, Program class declaration and few braces. It's simply not worth it. You can copy&paste my template from above and you are good to go. No further plugin setup or other nonsense.
     
  25. Xeviar

    Xeviar Trainee Engineer

    Messages:
    3
    I just did the setup of my VS from Inflex' post above and it works perfect and it's easy (I don't know Anything and am stumbling to use it...) but I can paste in a complete script by someone and it compiles with no errors.

    The thing that I was missing before was to setup the "Step 2 Optional" - it really isn't optional if you want to avoid the 100's of error messages.

    @Inflex I'm wondering how to remove all the "2 or 3 spaces" at the end of the lines? They are burning up LOTS of my 100,000 character limit.

    Cheers
     
  26. Neunzehn

    Neunzehn Trainee Engineer

    Messages:
    58
    I got the two steps done and when I compile it says "A project with an Output Type of Class Library cannot be started directly. In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project."
    And since all our main function is inside a class i thought its just how things go, now I'm confused.
    My code pieces range from 20 lines to 800 lines, so it might be a good idea to use VS.
     
  27. Xeviar

    Xeviar Trainee Engineer

    Messages:
    3
    How about the code stub you use to get your Usings in there?

    I'm using this one from Malware with success

    Code:
    using System;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    using VRageMath;
    using VRage.Game;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.Game.EntityComponents;
    using VRage.Game.Components;
    using VRage.Collections;
    using VRage.Game.ObjectBuilders.Definitions;
    using VRage.Game.ModAPI.Ingame;
    using SpaceEngineers.Game.ModAPI.Ingame;
    public sealed class Program : MyGridProgram{
    #line 1 "Program"
    	// YOUR SCRIPT GOES HERE
    
        // end your script
    }
    
     
  28. Inflex

    Inflex Developer Staff

    Messages:
    397
    When I started to hit 100K limit the first thing I did was removing all empty lines, leading and trailing white spaces from each line and it got me truckload of new space. By the time I did this I already had custom C# preprocessor so it was an easy task to add few new compilation rules.
    If you are little skilled in C# it's question of few minutes to write a script which reads whole file, removes certain white spaces and saves result to clipboard.
    --- Automerge ---
    This is normal. As I sad VS is capable of compiling your script, not running it.
    Instead of Debug->StartDebugging (F5 for me) use Build->Build Solution (F7 for me).
    --- Automerge ---
    Mine template got exactly the same using directives as Malware's. I modified original template to match PB's environment.
     
    • Informative Informative x 1
  29. Neunzehn

    Neunzehn Trainee Engineer

    Messages:
    58
    That works, thank you.
     
  30. Izion

    Izion Trainee Engineer

    Messages:
    9
    This helped me to get setup, just built a test script and seems to work :)

    Thanks for all the info guys, time to start learning C# :p
     
Thread Status:
This last post in this thread was made more than 31 days old.