Need Help With Plugin

 
#1

I need some help with a plugin I am working on. I am new to C# and OOP.
Have went thru the Tutorial and the SDK Tutorials also.

User-inserted image

How do I get the values from the form in to my C# code?

Code:

 public enum LocationType
{
Wall = 255,
Empty = 0,
Destination = 254,
Source = 1
}
public struct Location
{


instead of Wall = 255, how do I get Wall = textBox1.Text. textBox1 being the Textbox name.

Thanks
RichardZ

#2

Code:


//Option1:
// null => 0 AND returns false
// empty => 0 AND returns false
// non integer => 0 AND returns false
int i1;
if (!int.TryParse(textBox1.Text, out i1))
{
//it's not an integer
//raise an error
}

//Option2:
// null => 0
// empty => raises an exception
// non integer => raises an exception
int i2 = Convert.ToInt32(textBox1.Text);

//Option2:
// null => raises an exception
// empty => raises an exception
// non integer => raises an exception
int i3 = int.Parse(textBox1.Text);



If you're collecting input from a user, I'd go with option #1

#3

Thank you ptp,
I have decided to leave the values for Robot, Empty, Wall and Goal as set values.
Having them as part of an enumeration is my problem there I think.

In the picture I posted, Map Variable is $MyMap. This is a string of 6400 characters. Each character represents a 1'x1' block. How do I get this variable from EZ-Builder into my C#? I plan to use 2 for loops to move the data from the $MyMap variable to a 2 dimension array in the plugin.

#4

Code:


private void DoSomething()
{
//Get EZScript's map variable
var mapString = VariableManager.GetVariable("$MyMap");
//example map 4 x 3 => "X....X....X."

//Convert string to an array of 4 by 3
var map4X3 = StringToMap(mapString, 4);

DoSomethingWithMap(map4X3);

//Convert array to string
var newMapString = MapToString(map4X3);

//Set EZScript's map variable
VariableManager.SetVariable("$MyMap", newMapString);
}

private void DoSomethingWithMap(char[,] map)
{
for (var rowIndex = 0; rowIndex < map.GetLength(0); rowIndex++)
{
for (var colIndex = 0; colIndex < map.GetLength(1); colIndex++)
{
var cellValue = map[rowIndex, colIndex];
//DoSomething with cellValue
}
}
}

private static string MapToString(char[,] map)
{
return new string(map.Cast<char>().ToArray());
}

private static char[,] StringToMap(string flatString, int width)
{
var flatArray = flatString.ToCharArray();

var height = (int)Math.Ceiling(flatArray.Length / (double)width);
var result = new char[height, width];

for (var index = 0; index < flatArray.Length; index++)
{
var rowIndex = index / width;
var colIndex = index % width;
result[rowIndex, colIndex] = flatArray[index];
}

return result;
}

#5

Quote:


instead of Wall = 255, how do I get Wall = textBox1.Text. textBox1 being the Textbox name.


Quote:


Having them as part of an enumeration is my problem there I think.



If your wall value is an enum you should use a ComboBox to capture the user input.

To load the ComboBox LocationTypeComboBox:

Code:


LocationTypeComboBox.DataSource = Enum.GetValues(typeof(LocationType));
LocationTypeComboBox.SelectedIndex = 0;



To get the enum from the ComboBox:

Code:


LocationType locationType;
Enum.TryParse<LocationType>(LocationTypeComboBox.SelectedValue.ToString(), out locationType);



The "Standard" for a Label and Input is:
Label TextBox
Label ComboBox
...

#6

Thank you
I believe this is what I was missing.
I have picked up a few books on C# and going thru the lessons. catching on to the syntax easy enough but some of the concepts are a bit tough to get my head around. It is a bit different from TRS-80 Basic.

#7

When I execute the plugin, I get an error:

An exception of type 'System.Exception' occurred in EZ-Builder.exe but was not handled in user code

Additional information: Variable is an array: $MyMap

Makes sense to me. in EZ-Builder $MyMap is an array of 6400

the line of code reads:

Code:


var mapString = VariableManager.GetVariable("$MyMap");

#8

Quote:


In the picture I posted, Map Variable is $MyMap. This is a string of 6400 characters.


You mentioned a string.

if your variable is an Array:

Code:


//Get value from position 0 of $MyMap array
VariableManager.GetVariable("$MyMap", 0)

#9

I'm curious, what you plan to do with EZ-Script and an Array of 80x80 i.e. 6400 positions ?

#10

if you want to minimize the code you can use my extension method.
EZ-Script doesn't have typed types, GetVariable always returns a string.
My method will convert the string or array of strings to the type specified in the invocation.

Examples:

Code:


var str1 = this.GetVariable<string>("$str1");
var int1 = this.GetVariable<int>("$int1");
var arrayOfString1 = this.GetVariable<string[]>("$arrayOfString1");
var arrayOfInteger1 = this.GetVariable<int[]>("$arrayOfInteger1");



although is your responsibility to store the correct data type when using EZ-Script.

Code:


private T GetVariable<T>(string variableName)
{
if (!VariableManager.DoesVariableExist(variableName))
{
return default(T);
}

if (!typeof(T).IsArray)
{
if (!VariableManager.IsVariableArray(variableName))
{
return (T)Convert.ChangeType(VariableManager.GetVariable(variableName), typeof(T));
}

//requested type is array but the variable is not an array => return null;
return default(T);
}

var arraySize = VariableManager.GetArraySize(variableName);
var elementType = typeof(T).GetElementType();
var array = Array.CreateInstance(elementType, arraySize);

for (var i = 0; i < arraySize; i++)
{
var elementValue = Convert.ChangeType(VariableManager.GetVariable(variableName, i), elementType);
array.SetValue(elementValue, i);
}
return (T)(object)array;
}