Welcome to Keen Software House Forums! Log in or Sign up to interact with the KSH community.
  1. You are currently browsing our forum as a guest. Create your own forum account to access all forum functionality.

Frist codethingy

Discussion in 'Programming (In-game)' started by StuffYouFear, Jan 12, 2016.

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

    Messages:
    416
    Just felt like sharing this, as this took two weeks of reading C# for complete middle aged morons and reading and rereading and googling yalls scripts to work out how to get something onto the lcd screen.

    Code:
    const string lcdpanel = "lcd";
    IMyTextPanel output;
    List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();
    void Main()
    {
    	{
    		if(output==null) 
    		output=GridTerminalSystem.GetBlockWithName(lcdpanel) as IMyTextPanel;
    	}
    	string publictextstring = "Bacon";
    	output.WritePublicText(publictextstring);
    }
     
    • Like Like x 1
  2. Sinbad Senior Engineer

    Messages:
    2,788
    I don't think you don't need to
    Code:
    List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();
    I'm pretty sure the rest of your code never uses this list.

    but other then that: welcome to the middle aged new programmers club!
     
    • Like Like x 1
    • Agree Agree x 1
  3. gothosan Junior Engineer

    Messages:
    723
    if you want here is a simple code for writing text to an LCD screen:
    Code:
    void WriteToScreen(string ScreenName, string text, bool AppendToScreen)  
    {  
    	Echo("entering writer");
    	IMyTextPanel LCD = GridTerminalSystem.GetBlockWithName(ScreenName) as IMyTextPanel;  
    	if (LCD !=null)  
    		{  
    			LCD.ShowTextureOnScreen();  
    			LCD.WritePublicText(text,AppendToScreen);  
    			LCD.ShowPublicTextOnScreen();  
    		}  
    } 
    ScreenName is a string so you can simply call this function and write "Debug Screen" for example, it will look for a screen with this name.
    text obviously is also a string and is the text you want to write.
    AppendToScreen as a boolean, if false then whatever was written on the screen will be replaced with the new text.
    If true then whatever you want to write will be added to the text that is already in the LCD.
    WritePublicText will write to the public property of the LCD, you can use LCD.WritePrivateText(text,AppendToScreen); for writing to the private property.
    Likewise LCD.ShowPrivateTextOnScreen(); will display text from the private text property.
    ShowTextureOnScreen can be used to display images, if you want I can write you such function :)
     
  4. albmar Trainee Engineer

    Messages:
    69
    @gothosan I just want to mention that you only have to use ShowPrivateTextOnScreen(), ShowPublicTextOnScreen() or ShowTextureOnScreen() once to choose if the private text, public text or texture should be displayed on the panel's screen. It does the same like the drop down menu "Show text on screen" of the panel's menu. There had been a bug, where WritePrivateText() and WritePublicText() didn't update the screen and therefore one had to workaround by using the show methods to update the screen. But this has been fixed.
     
  5. gothosan Junior Engineer

    Messages:
    723
    Well... I admit, I stole this piece of code from someone else :p and I use it ever since :woot:
     
  6. StuffYouFear Apprentice Engineer

    Messages:
    416
    Opps thanks, that was kinda sorta leftover code from attempt #5ish to figure out how to call to a certain textpanel. I was basically playing around trying to learn it and didnt dawn on me that wasnt needed. Im still having trouble with the order in what to do things when it comes to making lists, filling the lists, and pulling a particular var from the list. I'm just glad for all yalls advice and it will definitely help. Maybe my next post will be on something useful.
     
  7. gothosan Junior Engineer

    Messages:
    723
    Remember that all lists and arrays are zero based, meaning the first item index is 0, and therefore an array of five items actually run from zero to four.
    here is an example:
    Code:
    void SomeFunction()
    {
    	string[] SomeStringArray = new string[5]{"one","two","three","four","five"};//define an array with 5 items and populate it
    	for(int i = 0; i < 5;i++)
    	{
    		Echo(SomeStringArray[i]);//loop on array and write the text on screen
    	}
    }
    
    In this example I made an array of five items and I initialized it so each index hold data.
    The Echo command display the text in the control panel when you press 'k' and click on the programmable block that run this code.
    Here we know the array have 5 items so the condition inside the for loop is 0 to 5, the condition will be false once i variable becomes 5, 5 cannot be smaller than 5.
    The thing that can easily mistaken between arrays and lists is that both have .Count in them but .Count is used to know how big a list is, aka how many items it hold, while for an
    array you need to call .Length

    If you don't know how big an array could be and you want to loop on all its items you can use this

    Code:
    for(int i = 0; i < SomeArray.Length; i++;)
    {
    	//some code
    }
    And if you need to dynamically change the array size as well as when you initialize it you need to use string[] SomeStringArray = new string[array size]
    that will initialize a new instance of the variable and will wipe all data it held before, if it held anything.
    As far as I know in C# there is no way to preserve this data other than to copy it to temporarly variable and then copy back into the array.
    in Visual Basic 6.0 however there is the word preserve (if I remember correctly) which did allow to change array size without deleting any data it hold.

    to make things somewhat easier ingame you need for most times a list when collecting more than one block.
     
    • Informative Informative x 1
  8. StuffYouFear Apprentice Engineer

    Messages:
    416
    Ah thats why that failed, as you saw this left over snippet of code,

    List<IMyTerminalBlock> panels = new List<IMyTerminalBlock>();

    I only had one textpanel on grid and trying to do anything with panels(1) kept giving a error, though it was a compiler error so most likely I was doing other things wrong, but now I see I was referencing the second item in a list that had no second item aswell.
     
  9. Axtel_Sturnclaw Trainee Engineer

    Messages:
    24
    I believe the syntax for accessing the array is panels[1](get the element of panels at index '1'). Panels(1) attempts to call a function named Panels, passing the integer value of 1.
     
    • Agree Agree x 2
  10. gothosan Junior Engineer

    Messages:
    723
    Actually it failed for two reasons:
    1) The panel list was basically populated with all functional blocks the grid has, regardless if they are LCD panels or buttons or reactors for that matter.
    So trying to access indevidual item would crash the script since it won't match the type needed.
    2) The list was not used at all, the variable output was used correctly but the if sentence was not.
    it sould have been like this

    void Main(string Argument)
    {
    if(output == null)
    {
    output=GridTerminalSystem.GetBlockWithName(lcdpanel) as IMyTextPanel;
    }
    //rest of the code
    }


    the position of the brackets {} for the if sentence was not correct.
     
  11. gothosan Junior Engineer

    Messages:
    723
    When accessing any array the return type is depended on the array type:
    If the array is string then for example Panels[1] will return a string from the second item of the array (remember that lists and arrays are zero based so the first item index is zero).
    The returned value will be "" if its string and that item is empty, for numbers in such case it will probably return null (kinda unsure) but if you try to access an item that does not exist due to array size you will get exception "IndexOutOfBound" which will crash the script unless is handled.
     
  12. Sinbad Senior Engineer

    Messages:
    2,788
    i think what he was trying to say was that [ i ] is the array element operator and ( i ) is the member function operator.
     
  13. StuffYouFear Apprentice Engineer

    Messages:
    416
    Ok I took what yall said, "borrowed" shamelessly code from http://forum.keenswh.com/threads/cargo-inventory.7325186/
    (this is baised off his way of counting cargo containers)and after a whole day of ignoring work at work and doing anything important at home...all night....
    I got code that does what I was trying to get work the first time. This time it makes a list of just IMyTextPanel, and the puts numbers up on them in the order of the list corresponding the the index placement....or make it say bacon...or both!

    Code:
    void Main()
    {
    	List<IMyTerminalBlock> panel = new List<IMyTerminalBlock>(); //new list
    	panel.Clear();  //clears list
    	panel = new List<IMyTerminalBlock>();   //Ive no idea why this is done, wasnt this done on line 3?
    	GridTerminalSystem.GetBlocksOfType<IMyTextPanel>(panel);	//Makes the GridTerminalSystem populate the index of "panel" with any
    																//instances of IMyTextPanel found on grid
    															 
    	for (int x = 0 ; x < panel.Count ; x++) //makes a int named "x", makes it 0, and if x is a lower number that the total index count of "panel",
    											// it will add "1" to it, and run whats in its {}
    	{
    		int y = x;  //made a int thats basicly a clone of x, its probly not needed but it made me feel warm and fuzzy and was kinda trouble shooting
    		IMyTextPanel lcd = panel[x] as IMyTextPanel;  //so Im telling a method I can access called "IMyTextPanel" to make a instanced of itshelf,
    													  //and then fill the requiered var's copyed from the list "panel" at postion "x", and that it is a
    													  // IMyTextPanel as well
    		if (x <= panel.Count)   //as long as "x" is less than or equal to the total index count of "panel" do stuff below
    		{
    			string displaytext = Convert.ToString(y);   //make a string, and make it the converted form of the index "y".
    			//string displaytext = "Bacon"; this was for testing, and bacons awsome
    			lcd.ShowPublicTextOnScreen();   //tells the currently selected instance of IMyTextPanel to enable public text on screen
    			lcd.WritePublicText(displaytext,true);  //same as above, but shoves numbers or bacon on screen, I have no idea what that bool is for...append?
    		}
    	}
    }
    This in of itshelf is kinda pointless, but it has taught me how lists work, and how to call to methods, somewhat anyway. Calling to a named LCD would be wayyyyyyy simpler, but say I had like 20 gyros I could use this to make a list of only gyros and go though and normalize their settings vs their orientation. Anyway thank yall for yalls help, and if you find errors or Im doing something horribly horribly wrong let me know. Also sorry for my spelling in the notations, spellcheck is my first language and its lacking in SEWorkbench.

    Edit: thank you gothosan, your post had already answered that the bool append does, and now rereading it while awake makes much more sense, append=apply to end of current line
     
    Last edited: Jan 14, 2016
    • Like Like x 2
  14. Wicorel Senior Engineer

    Messages:
    1,243
    The order of the last two are reversed. I've also added a fourth step to show that it is repeating.

    1. makes an int named x and makes it zero (0)
    2. if x is lower name the total index count of "panel, it runs the instructions between the {}. if NOT, then starts instruction after }
    3. after running the instructions, it increments x by one
    4. go to step 2
     
  15. gothosan Junior Engineer

    Messages:
    723
    That code compile as it should and thats good :)
    I was happy to help :)
     
  16. Krougal Senior Engineer

    Messages:
    1,012
    I need that!
     
  17. Ronin1973 Master Engineer

    Messages:
    4,830

    Yeah... same boat. I program things overly simple and very inefficiently. But as I learn more my coding gets better. I find it easier to learn by going about things the long way. It's easier to read the code. A programming language is still a language. No one begins a language talking in complete or complex sentences. Stay simple until you better understand what's going on. As long as you're learning new things efficiency will come in time.

    That's my $0.02.
     
Thread Status:
This last post in this thread was made more than 31 days old.