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

[Guide] Setting up Visual Studio for Programmable Block Scripting

Discussion in 'Programming Guides and Tools' started by CyberVic, Jan 2, 2015.

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

    CyberVic Apprentice Engineer

    Messages:
    106
    First watch this video. The important parts to watch are till 3:35.
    https://www.youtube.com/watch?v=gAh1bNfRLPw

    In addition to the using statements they include in this video, you also need to include this:
    Code:
    using Sandbox.ModAPI.Ingame;
    using Sandbox.ModAPI.Interfaces;
    Add this as a local variable of your script class
    Code:
    IMyGridTerminalSystem GridTerminalSystem;
    From there intellisence is magic.

    A sample c# class should look like below.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Sandbox.Common;
    using Sandbox.Common.Components;
    using Sandbox.Common.ObjectBuilders;
    using Sandbox.Definitions;
    using Sandbox.Engine;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.Game;
     
    <div>namespace Scripts
    {
        class StatusReport
        {
            IMyGridTerminalSystem GridTerminalSystem;
            //https://steamcommunity.com/sharedfiles/filedetails/?id=360966557
    </div> 
    <div>        void Main()
            {
                List&lt;IMyTerminalBlock> blocks;
     
                blocks = new List&lt;IMyTerminalBlock>();
                GridTerminalSystem.GetBlocksOfType&lt;IMyRadioAntenna>(blocks);
                if (blocks.Count == 0) return;
                IMyRadioAntenna antenna = blocks[0] as IMyRadioAntenna;
     
                antenna.SetCustomName("Hello Galaxy!");
            }
    </div>    }
    }
    
    When copying and pasting from Visual Studio to in game
    • Do not copy any of the using statements
    • Do not copy the namespace
    • Do not copy the class
    • Do not copy the "IMyGridTerminalSystem GridTerminalSystem;" line
    • Basically just copy any local variables, the main method, and any other sub-methods you make
    • Makes sure to not include any closing curly braces for the class / namespace
    Example:
    Code:
    <div>        void Main()
            {
                List&lt;IMyTerminalBlock> blocks;
     
                blocks = new List&lt;IMyTerminalBlock>();
                GridTerminalSystem.GetBlocksOfType&lt;IMyRadioAntenna>(blocks);
                if (blocks.Count == 0) return;
                IMyRadioAntenna antenna = blocks[0] as IMyRadioAntenna;
     
                antenna.SetCustomName("Hello Galaxy!");
            }
    </div>
     
    Last edited by a moderator: Jan 2, 2015
    • Informative Informative x 2
    • Like Like x 1
  2. Morte

    Morte Trainee Engineer

    Messages:
    29
    Cheers dude.

    Been looking for something like this for a good while now :woot:
    Love programming but never know where to start and when I do I get confused a bit cause I don't know what all the variables can be used or what can be used and stuff.

    This made it so much easier =3
     
  3. CyberVic

    CyberVic Apprentice Engineer

    Messages:
    106
    Updated. Should also use Sandbox.ModAPI.Interfaces
     
  4. Don Jacobs

    Don Jacobs Junior Engineer

    Messages:
    657
    Finally got it. Thanks CV
     
  5. Mix-martes86

    Mix-martes86 Senior Engineer

    Messages:
    1,110
    Hello everyone,

    What better way to introduce myself than posting a little tibdit of a test I made yesterday :) :

    Code:
    void Main() 
    { 
            List&lt;IMyTerminalBlock> blocks = new List&lt;IMyTerminalBlock>();  
            Sandbox.ModAPI.Interfaces.ITerminalAction ba; 
    
            GridTerminalSystem.GetBlocksOfType&lt;IMyInteriorLight>(blocks);  
    
            blocks.ForEach(delegate(IMyTerminalBlock b)
            { 
                    ba=b.GetActionWithName("OnOff_Off"); 
                    ba.Apply(b); 
            } );
    }
    
    It basically turns off all Interior Lights in the current grid, using a Lambda function (delegate/anonymous, which I thought didn't work as per KSW's instructions) that describes what to do with each light object.
    Also, since the ForEach word is not working, I instead used the generic function ForEach contained within ALL collections declared of the type List&lt;T>.
    My pc slowed down quite a lot for a moment when I ran it, but I don't know if it's a performance issue of the ForEach function itself, or because my station has well over a hundred lights to turn off. I'm not native to C#, I'm more into Java (desktop, not mobile) and PHP, so I can't know for certain.

    This programmable block is a fun addition indeed, but as a programmer myself, I must admit it isn't precisely a user-friendly thing, since regular folk aren't programmers, probably something worth checking out, providing sample scripts and the like, since the guide was helpful for me even if I had to work it out a little, but assumes advanced programming knowledge (most notably, how class inheritance and class templating works) that isn't precisely day-to-day stuff.

    Regards.
     
  6. ROMB

    ROMB Trainee Engineer

    Messages:
    3
    Little update for better styling:

    Code:
    #region pre_script
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Sandbox.Common;
    using Sandbox.Common.Components;
    using Sandbox.Common.ObjectBuilders;
    using Sandbox.Definitions;
    using Sandbox.Engine;
    using Sandbox.ModAPI.Ingame;
    using Sandbox.ModAPI.Interfaces;
    using Sandbox.Game;
    namespace Scripts
    {
        class ExampleScipt
        {
            IMyGridTerminalSystem GridTerminalSystem;
    #endregion pre_script
    [COLOR= #880000]/* https://steamcommunity.com/sharedfiles/filedetails/?id=360966557 */[/COLOR]
    void Main()
    {
        List&lt;IMyTerminalBlock> blocks  = new List&lt;IMyTerminalBlock>();
        GridTerminalSystem.GetBlocksOfType&lt;IMyRadioAntenna>(blocks);
        if (blocks.Count == 0) return;
        IMyRadioAntenna antenna = blocks[0] as IMyRadioAntenna;
        antenna.SetCustomName("Hello Galaxy!");
    }
    #region post_script
        }
    }
    #endregion post_script
    
    So now you can collapse pre and post regions, and only script body will be displayed in VS editor.
     
    Last edited by a moderator: Jan 3, 2015
    • Informative Informative x 1
  7. CyberVic

    CyberVic Apprentice Engineer

    Messages:
    106
    Great idea!
     
  8. Deathpill

    Deathpill Trainee Engineer

    Messages:
    2
    I just wanted to suggest instead of Visual Studio Express, Use Visual Studio 2013 Community Edition. It's the most up to date version that's free and it allows the use of extensions/packages through Nuget unlike Express.
     
  9. cyrillius

    cyrillius Trainee Engineer

    Messages:
    10
    I also suggest that when you want modify a script you open with your favorite IDE the file here:

    C:\Users\$username\AppData\Roaming\SpaceEngineers\IngameScripts\local\$name_of_the_script\Script.cs
     
    Last edited by a moderator: Feb 16, 2015
  10. Lynnux

    Lynnux Junior Engineer

    Messages:
    881
    And sometimes you want/need

    using VRageMath;

    to use different types and classes (e.g. Color, Vectors)
     
  11. GaryP

    GaryP Trainee Engineer

    Messages:
    36
    I think there may have been an update because when I make a script it isn't there. Do I need to save it somewhere outside the programmable block?
     
    Last edited by a moderator: Mar 7, 2015
  12. Malware

    Malware Master Engineer

    Messages:
    9,867
    May I suggest that this guide is updated to use the new base class?
     
  13. ArgumentNullException

    ArgumentNullException Trainee Engineer

    Messages:
    32
    I can do you one better.
     
    Last edited: Jun 15, 2015
    • Informative Informative x 1
  14. Malware

    Malware Master Engineer

    Messages:
    9,867
    Does not change my suggestion, the script setups should use the MyGridProgram base class :)
     
  15. ArgumentNullException

    ArgumentNullException Trainee Engineer

    Messages:
    32
    I used your new interface, not your base class, I will try some tests with the base, I did not want your wiring getting in the way of testing however it might not cause an issue.
     
  16. Malware

    Malware Master Engineer

    Messages:
    9,867
    There should (hopefully) be no problems. What you do is use the base class for the script template, but cast to the interface in any testing framework you'd like to use in order to simulate its features. This is what the PB now does, actually. But it was meant as a suggestion, nothing else.
     
  17. JamesAM

    JamesAM Trainee Engineer

    Messages:
    59
    Just dabbling in the coding side of things for the first time, thought I'd make it a bit easier on myself with visual studio.
    I followed the instructions via the video link in the OP.
    However when it comes to linking the references I got a little stuck.
    It seems keen has rearranged the files in the bin folder and some of the files specified to reference were no longer present and there are a bunch of new ones in there too.
    You guys know which .dll files I need to incorporate into my VS document?

    I have a ridiculously good idea for a script that I can't wait to create.
     
    Last edited: Jun 17, 2015
  18. ArgumentNullException

    ArgumentNullException Trainee Engineer

    Messages:
    32
    You can use my templates (instructions included) if you like, the references are pre-wired, you only need to put in the location of the install.

    UPDATE: Added a VISX installer to auto-copy the templates, just run the VISX and look for templates starting with the name BSET. For more info check out the wiki.
     
    Last edited: Jun 18, 2015
    • Like Like x 1
  19. jkeywo

    jkeywo Trainee Engineer

    Messages:
    5
    All scripts are derived from MyGridProgram so -
    Code:
     class ExampleScipt : MyGridProgram 
    is more accurate to use as a wrapper class, it removes the need for declaring your own GridTerminalSystem and exposes Me, Storage, etc.
    Which is nice :)
     
  20. nogare321

    nogare321 Trainee Engineer

    Messages:
    64
    When the programming libraries have been updated, how do I make sure my local ones I've linked to in Visual Studio are also updated? Or is it done automatically?
    Thanks!
     
  21. jkeywo

    jkeywo Trainee Engineer

    Messages:
    5
    It should happen automatically when you update your game, assuming you linked the ones in the game directory and didn't make local copies for some reason.
     
  22. nogare321

    nogare321 Trainee Engineer

    Messages:
    64
    Good to know. Thanks!
     
  23. noxLP

    noxLP Junior Engineer

    Messages:
    729
    I have two problems, i can not find the VRage.common.dll (ive searched it with the explorer, not only visually) and i can not use Sandbox.Common.ObjectBuilders which gives an error saying ObjectBuilders is not found in Sandbox.common. I suppose ObjectBuilders is defined in the missing dll?

    Ps.: Why is not this thread pinned?
     
  24. noxLP

    noxLP Junior Engineer

    Messages:
    729
    Solved, i include VRage.Game.dll and that's it.
     
  25. entspeak

    entspeak Senior Engineer

    Messages:
    1,744
    Ah, I was wondering about that one. Just added it. Do I have to do "using VRage.Game.ObjectBuilders" or something like that?

    I also added VRage.Native.dll. I don't know if that does anything, though.
     
  26. noxLP

    noxLP Junior Engineer

    Messages:
    729
    Honestly i have no idea what's included in ObjectBuilders, i have not searched it in the documentation, but i have it just as the KSH's video says: using Sandbox.Common.ObjectBuilders;
    Though it seems to be in the VRage dll that using line gives me no errors, so...

    Now i have other problems :eek:ops:, f.i. it seems that don't recognize Echo and Storage.
     
  27. entspeak

    entspeak Senior Engineer

    Messages:
    1,744
    Have you done the MyGridProgram change?

    class ExampleScript : MyGridProgram

    Someone mentioned that this opens up Storage... don't know if it opens access to Echo.
     
  28. noxLP

    noxLP Junior Engineer

    Messages:
    729
    Oh, tks, no errors now in Main and in methods, but still errors in classes... should i create all the classes that way? o_O

    Edit: sorry, the error in the classes is: Cannot access a non-static member of outer type 'Sandbox.ModAPI.Ingame.MyGridProgram' via nested type 'Scripts.ExampleScript.Leg'

    Edit2: I answer myself: yes, i should create them that way, no errors now ;) I suppose i should delete the ':' part when pasting to SE
     
  29. entspeak

    entspeak Senior Engineer

    Messages:
    1,744
    I have it like this:

    class ExampleScript : MyGridProgram {

    And I got no errors. I put this before right before the section I am going to use for the in-game script. After this, I start my variable/constant declarations and go into the script. I close it ("}") at the end of the whole script - after the bit I'm using for the in-game script.

    I also remove "IMyGridTerminalSystem GridTerminalSystem;"

    The only part you need to paste into SE is the script section... which comes after "class ExampleScript : MyGridProgram {" - actually the stuff between that and its closing bracket. All the stuff previous is just to get it working in VS.
     
    Last edited: Sep 17, 2015
  30. noxLP

    noxLP Junior Engineer

    Messages:
    729
    I know that.

    Man, what a shock, you have to open the F**** region when you load the script, i thought i lost all the work cause i didn't saw the code!!!!:eek:ops:
     
Thread Status:
This last post in this thread was made more than 31 days old.