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

Should this be working ? (string.Format() )

Discussion in 'Programming (In-game)' started by Acolyte, May 8, 2016.

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

    Acolyte Apprentice Engineer

    Messages:
    109
    This code works in VS but not in SE.

    public static void SetRotorAngle(string name, double angle, double velocity)
    {
    Echo(string.Format("SetRotorAngle : name={0} : angle={1} : velocity={2}", name, angle, velocity));

    ...
    }

    "An object reference is required for non-static fields method or property for Sandbox.ModAPI.InGame.MyGridProgram.Echo.get"

    I have seen lists and all sorts of things work in online tutorials, but they are very hit and miss for me - I am doing something wrong, but ca't figure out what.

    Help plz..
     
  2. Malware

    Malware Master Engineer

    Messages:
    9,867
    Echo is a property on MyGridProgram, not a static.

    When setting up your Visual Studio, use a proxy class which derives from MyGridProgram:

    Code:
    using System;
    using VRageMath;
    using System.Collections.Generic;
    using VRage.Game;
    using System.Text;
    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 class Program: MyGridProgram
    {
    	// YOUR SCRIPT GOES HERE
    }
    
    This is based on the actual wrapper class used by the game.

    Some personal advice: You should avoid using statics. While they have their place, they're considered bad form, causes bad programming habits (especially for non-scripting applications) and in the case of SE, can potentially cause problems if used wrong.
     
    Last edited: May 8, 2016
  3. Acolyte

    Acolyte Apprentice Engineer

    Messages:
    109
    OOps, yeah, thx, just spotted that. I recall had to make a few things static in VS for the code to run ( I was using your example in the guide btw). I should have taken the static keyword out. I did that and it worked, then I came back and saw your reply ;-)

    But..

    On a similar note, I had to comment out all the Count() methods for my string arrays. I recall using Count as a property sometimes (ie, Count, not Count() ) but that didn't work here either.

    I had to make this change in SE ( commented lines worked in VS).

    // Get current angle from detailed info

    string s = "Attached\nCurrent angle: 160°";
    string mytext = "Current angle";

    string[] sa = s.Split('\n', ':');
    string a = "";

    //for (int i = 0; i < sa.Count(); i++)
    for (int i = 0; i < sa.GetLength(0); i++)
    {
    if (sa == mytext)
    {
    int j = i + 1;

    //if (j >= sa.Count())
    if (j >= sa.GetLength(0))
    Echo("Detailed info messed up in SetRotorAngle()");

    a = sa[j];
    break;
    }
    }

    I know GetLength isn't the only command i could have used, but I am trying to figure out what works and what doesn't, and why..

    and, yes, I did just notice that I still have my dummy detailed info for VS still in there ho ho.
     
    Last edited: May 8, 2016
  4. Malware

    Malware Master Engineer

    Messages:
    9,867
    Arrays don't have any Count, they have Length. The Count() you speak of comes from Linq which is not made available in the SE api.

    The SE scripts are actual, real C# compiled the very same way any other C# is. However certain APIs are disabled, and there are instruction counters injected into the compiled assembly to control execution and make sure the game isn't killed by a wayward script. This injection system is unfortunately extremely flawed and disables certain aspects of C# it shouldn't - either completely or partially - like foreach, generics and exception handling.
     
  5. Acolyte

    Acolyte Apprentice Engineer

    Messages:
    109
    I'm sure I have seen Count() (or Count ? ) used in some youtube tutorials - was linq once available to SE ? Not that it matters, the most important thing is its not available now and that I understand why (thx).

    I will use your list in future, at least until Keen change it again ;-)

    But I cannot see the creation of an instance in your code - The reason I was using statics before is because VS was complaining about lack of objects for non-static stuff and making things static made them go away. I am coming to C# from C/C++ so I am not used to the main() function being a method of an object. I guess I would struggle with Java for the same reason.

    Should there be an instance declared in your example ? (to run in VS).

    If not then I'm still confused ???
     
  6. Malware

    Malware Master Engineer

    Messages:
    9,867
    Lists (i.e. List<IMyTerminalBlock>) have Count, arrays (i.e. string[]) have Length. Keen hasn't changed these things, no, this is .NET framework stuff, and no - linq was never available.

    As for the Main not being static, that is SE script framework stuff and has nothing to do with C# or .NET. A script is simply the instance of a class deriving from MyGridProgram, and the Main method is used by the game to interact with that instance.

    I didn't include any way to run it in VS - because scripts cannot be run in VS. They rely on in-game assets that simply aren't available. I use mockups and unit tests for that, and I certainly don't mix more non-ingame code in the script file than I have to - personal choice :) What I gave you is all you need for Visual Studio to compile your script and give you the correct intellisense and error messages, that's all.
     
  7. Acolyte

    Acolyte Apprentice Engineer

    Messages:
    109
    I have definitely seen lists used in youtube vids and there are also things that have changed behaviour when I moved code to SE. I think this is why I use string arrays now, they are the only things that work when I move them over.

    Its good that I know that the code example is for editing, and not for running - I'll figure out running it myself, and I just remembered that the reason had a static main() is because that is how the VS hello world template is constructed.

    Time for some more basic C# study for me (without SE) to get something I can run in VS without statics.

    I am starting to formulate plans for code that has SE specific stuff wrapped in helper functions that I can dummy out in VS while I test the rest of the script - I hope this is normal - urk !

    Thx for the help.. ;-)
     
  8. Malware

    Malware Master Engineer

    Messages:
    9,867
    Of course you've seen lists used. They are available and used all over the place. What gave you the impression they weren't? :) You were trying to use a Linq extension method on arrays. As I said Linq is not available. About the slight change of behavior, that's due to the instruction counter injection I told you about, it breaks certain constructs - but only C# constructs, not the API itself. The type whitelist deals with blocking access to dangerous types. As for changes, the absolute majority of the changes made to the programmable block after its initial release (if we exclude the API) has been done by me. So yeah, I kinda know its history :p

    (not Keen employee, just a source code contributor)
     
  9. Acolyte

    Acolyte Apprentice Engineer

    Messages:
    109
    [QUOTE="Of course you've seen lists used. They are available and used all over the place. What gave you the impression they weren't? :)
    [/QUOTE]

    The fact that I had lists of strings in a script that gave me some kind of SE exception/error, and when I changed the lists to string arrays the errors went away.

    I suspect my failings are down to my not knowing about scope issues in C#.

    I know Keen have to protect themselves against over-misuse of Programmable blocks, and I only hit the 50000 limit once in a bug that was entirely my fault, once I corrected my error the count went down to 21 ;-)

    I think the PB itself works well in the game, I think the problems are in documentation, what this forum needs is a couple of videos of the entire workflow of someone who knows what they are doing making a script. There are lots of tutorials about what an "if" statement does, and none about C# object scope.

    But then I'm just a simple C++ programmer ;-)
     
  10. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    error went away cuz looks like you used lists as arrays.

    most hard thing will be for you as C++ programmer is propertyes, they are like data fields, but actually they are methods.(but don't requare call syntax)
     
  11. Dawnkeeper

    Dawnkeeper Apprentice Engineer

    Messages:
    260
    Here is a guide to set up VisualStudio for use with scripting. I highly recommend using VS or Xamarin. It avoids a lot of problems.

    And Here is a little tool I wrote to get a script that spans multiple files easily into SE.
     
  12. Acolyte

    Acolyte Apprentice Engineer

    Messages:
    109
    So, a bit like the times when C++ programs declare a bunch of private variables in a class, the have a bunch of get() and set() methods to access them ? Maybe ?

    Just guessing ;-)

    @ Dawnkeeper, thx for the links, I already saw the first one.

    I am presently reading a C# book that seems to have all its examples declaring static main () :(
     
  13. Malware

    Malware Master Engineer

    Messages:
    9,867
    I already answered that one for you...
    In other words, it might as well have been called "DoCoolStuff"... :p all it shares with program entry point is the name.
     
    Last edited: May 8, 2016
  14. Dawnkeeper

    Dawnkeeper Apprentice Engineer

    Messages:
    260
    The static main method is needed to be able to run this class from the command line. So you will find it in all projects that will be run as an exe.
     
  15. Acolyte

    Acolyte Apprentice Engineer

    Messages:
    109
    @Malware - sorry, I misinterpreted, I thought you implied that static functions were bad - I never had a problem with main() not being static in SE for the very simple reason that nonstatic main() works in SE ;-) Its in VS I had problems, although I did then copy them to SE as part of the cut/paste which I believe is how I generated the errors that I started this thread with.

    @Dawnkeeper - Thx.

    @Both ;-) Does this mean my search for main() in class instances is a waste of time ? I just blitzed a 400 page C# game programming book, all main()'s were static.

    Ah well back to programming later, now its bath time :p

    Ciao
     
  16. Phoera

    Phoera Senior Engineer

    Messages:
    1,713
    in books you write application.
    in script you write...hmm...script class.

    where constructor called at script compilation,
    void Save() called at save.
    and void Main(string) called at PB run.
     
  17. Malware

    Malware Master Engineer

    Messages:
    9,867
    Yes, that is a waste of time :p

    I wasn't talking of main specifically, but statics generally. Statics have their place, but it is better to avoid using statics. The main entry point of a "real" application is obviously going to have to be a static method. I tried to explain my stance once, here:
    https://forums.keenswh.com/threads/h...ata-in-storage.7363603page-$1.html#post-1286837423
     
  18. mze9412

    mze9412 Junior Engineer

    Messages:
    791
    I started working on a small UI for it btw ;) It will be able to handle multiple scripts and save the output into folders and tell you if a script has changed since it was "compiled" the last time.
     
    • Like Like x 1
  19. Ronin1973

    Ronin1973 Master Engineer

    Messages:
    4,964
    I'm using Gilgame's SE Workbench for C# and Notepad ++ for editing XML.
    https://github.com/gilgame/SEWorkbench

    I'm pretty happy. Though I've never worked with Visual Studio (I'm a complete novice/amateur/hack/(insert your own pejorative here). You can load in ship blueprints directly to Workbench and it's designed specifically for SE. There are a few things that I wish it would do. But it's free and does the job nicely.

    I've not had anything compile in Workbench that wouldn't run in SE unless there was a bug.
     
Thread Status:
This last post in this thread was made more than 31 days old.