Dynamically generate public static - arrays

I pass a string from VVVV to Unity via UDP, which is made up of multiple float groups, each one defined by a Char at the beginning, and an "!" at the end.
K0.0322 2.1062 4.2102 49.9711 43.2255!V44.7385 47.2003 49.8143 49.4658 4.1806 12.6100 6.2053!C49.9437 16.4352
I want to dynamically generate arrays with the content of each group
float[] k = {0.0322, 2.1062.........};
float[] v = {44.7385, 47.2003........};
and make them accessible for other unity scripts as public static.
Is this possible?
These public statics have to be initialized at the beginning of the script, so if they are generated dynamically they won't exist at the start.

I think maybe you can try using list
Public static List<float> newlist= new List<float>❨❩;

Related

Unity | Script to create a new Tag

I have a code which reads multiple string-arrays out of a dynamical dll. I'm creating GameObjects out of each string inside that array and i want to group each array of strings as unique groups.
So far, I've managed to give theese GameObject specific tags to group them, but theese tags were pre-created in the Unity Editor. This works so far, but in case the dynamic class provides more arrays than tags i have created previously, it won't be able to group the additional arrays.
My question: is there any way to create new tags via script while the game is running? With theese i could just simply add them to the new GameObjects.
In case that this shouldn't be possible, does someone have an idea of a different way to sort theese groups?
Thanks in advance!
In principle, the tag system is just a Dictionary of Lists. The problem is that Unity's Dictionary us defined at compile-time. The downside of rolling your own Tag system is you need some way to clear destroyed objects.
public class CustomTag : MonoBehaviour
{
private static Dictionary<string, List<GameObject>> Collection = new Dictionary<string, List<GameObject>>();
public static void Register(string tag, GameObject item) {
if (!Collection.ContainsKey(tag))
Collection.Add(tag, new List<GameObject>());
Collection[tag].Add(item);
}
public static void Deregister(string tag, GameObject item) {
if (!Collection.ContainsKey(tag))
return; // No Such Tag
// In case of multiple entries, remove all occurences of item
// If you're sure you will only have one entry per item, you can just use 'Remove'
Collection[tag].RemoveAll(m => m == item);
}
public static IEnumerable<GameObject> FindObjectsWithTag(string tag) {
if (!Collection.ContainsKey(tag))
return null;
return Collection[tag].AsEnumerable();
}
public string Tag;
private void Start() {
Register(Tag, gameObject);
}
private void OnDestroy() {
Deregister(Tag, gameObject);
}
}
Add this component to each GameObject you create, and set the Tag. It will take care of registering and deregistering. You can access the list of objects per tag using CustomTag.FindObjectsWithTag(). Note that with the current setup, objects won't register with the tag system until they Awake (which IIRC won't be until just before next update)

Instance an object in an array which is an attribute of antoher object

What is the correct way of declaring an instance of an object which is part of an array that is an attribute of another object? I have a class called ingrediente which is quite simple. It looks like this:
public class ingrediente{
public int cantidad;
public String nombre;
public int fechaDeCaducidad;}
I have another class called receta which has an array of ingrediente objects:
public class receta{
ingrediente [] ingredientes;
String preparacion;
String nombreReceta;}
I want to give values to the attributes of an ingrediente object which is part of the array ingredientes in an instance of the receta class. I'm not even sure if I have to declare the ingrediente object before giving values to it, but the code doesn't work (although it doesn't show syntax errors) wether I do it or not. The code looks like this:
recetas [CONTADORRECETAS].ingredientes [CONTADORINGREDIENTES2] = new ingrediente ();
recetas [CONTADORRECETAS].ingredientes [CONTADORINGREDIENTES2].nombre = TEMP2;
Can anybody tell me what I'm doing wrong? The code is inside a try and it throws java.lang.NullPointerException on those lines (on the first one if I include it and on second one if I don't). The value of the attribute doesn't get asigned. Variable TEMP2 is previously declarated and is of the correct type.

Silverlight Unit Testing multiple initializations with same tests

I have an object with lots of properties that I would like to test. I have written a TestMethod for each property, but the problem is that many of the properties manipulate the others when they are set. What I need is to be able to set up my test object, manipulate one of the variables, run the tests, reset the object to its original state, then repeat the process. That way, I can avoid having so much redundancy.
I looked into using a Data Driven approach (which is a perfect solution to this problem), but that doesn't seem to be available with the Silverlight Test Framework, since I can't find a way to use the DataSource attribute. I thought about trying to see if I could get access to the DataSource through the traditional MSTest framework, but alas, I only have Visual Studio Express.
I've thought to try to look into creating a custom test harness to see if that could solve this problem, but I thought I'd ask around for suggestions first.
Maybe I should just suck it up and write up all of the different configurations as separate TestInitialize Methods and comment out the ones I don't need as I go.
Any help would be greatly appreciated.
Update/Clarification:
Here is an example of how the object to be tested works. Say you have a shape with a position coordinate and coordinates for each side. When you Update the coordinates of the position or one of the sides, all other coordinates must be updated as well.
This is the functionality that is under test. What I would like to do is be able to set up multiple initializations (through ClassInitialize or what have you) where I would set up the initial value of my object and a mock containing the expected test results, then alter one of the properties in question. Something like this (this is for illustration only, so please ignore any poor practices xD):
// class under test (mock has the same properties & constructor)
public class MySquare
{
public Vector2 XYPosition;
public int Width;
public int Height;
public float TopSidePosition;
public float RightSidePosition;
...
public MySquare(float x, float y, int width, int height)
{
// set up all properties
}
}
// test object container
public class Container
{
public static MySquare square;
public static MySquareMock squareMock;
}
// desired test class initializations (it would be nice to be able to run each of these
// as well as the TestMethods and then reset to the next TestSetupClass)
[TestClass]
public class TestSetupClass1
{
[ClassInitialize]
public void SetupTest()
{
// set up initial value and expected result
Container.square = new MySquare(0, 0, 5, 5);
Container.squareMock = new MySquareMock(1, 1, 5, 5);
Container.square.XYPosition = new Vector2(1, 1);
}
}
[TestClass]
public class TestSetupClass2
{
[ClassInitialize]
public void SetupTest()
{
// set up initial value and expected result
Container.square = new MySquare(0, 0, 5, 5);
Container.squareMock = new MySquareMock(1, 0, 5, 5);
Container.square.RightSidePosition = 6;
}
}
// test methods
[TestClass]
public class TestMethods
{
[TestMethod]
public void TestPosition()
{
Assert.AreEqual(Container.squareMock.XYPosition, Container.square.XYPosition);
}
[TestMethod]
public void TestTopSidePosition()
{
Assert.AreEqual(Container.squareMock.XYTopSidePosition, Container.square.TopSidePosition);
}
// test method for each property
}
I didn't find a completely automated way of achieving my original goal, but I did come up with something pretty easy.
First, I created a singleton TestManager class to handle nearly all of my test setup, so that I only need one line in my MainPage (the test page) class. Within the TestManager, I added a string variable that I assign a name of a TestSetup class too from the MainPage.
Then, I created another TestClass called TestSetupClass and gave two static fields:
private static Type childType;
private static TestSetupClass childInstance;
In its constructor, I compare the underlying type to the class name specified in the TestManager and set the childType and childInstance if they match:
Type thisType = GetType().UnderlyingSystemType;
if (thisType.Name.Equals(TestManager.Instance.SetupClassName))
{
childType = thisType;
childInstance = this;
}
Next, I added a virtual method to the TestSetupClass with an AssemblyInitialize attribute. All of my setup classes inherit from TestSetupClass and override this method. Within the method, I use the childType to get MethodInfo for the child's overridden implementation of the method and invoke it using the childInstance:
MethodInfo childSetup = childType.GetMethod("Setup");
childSetup.Invoke(childInstance, null);
And voila! When you want to run a specific setup before running all the tests, you just specify its class name in the MainPage using the TestManager.
I know there are a few more places where I could have made this better, but it works, so what can you do.

Silverlight App object Does not exist' Error

name 'App' does not exist in the current context.
How that possible?
Have to note my initialization code is different than MainPage() type, as I converted SketchFlow app into production Silverlight. They instruct you to do init code via System.Windows.Controls.Frame():
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new System.Windows.Controls.Frame() { Source = new Uri("/MyAppScreen.xaml", UriKind.Relative) };
}
public static string ValueFromHome =
"A Value on Home page";
the goal was to set up public var inside App object so I can access it from various screens down the road
Accessing Resource data requires calling App object I believe as in below, is that correct? so this won't help me
string color = App.Current.Resources["customColor"].ToString();
If you are just storing strings, look into using Resource files. Then they can be translated if that ever becomes necessary.
EDIT (to explain the resource file usage): To access the resource, first create a .resx file in your project (let's say you name it MainResource.resx), change the access modifier drop down to public, add your string with Name: ValueFromHome and Value: "A Value on Home page".
Then you can get the value by adding a using to the namespace of the resource if needed and calling it directly like so:
string value = MainResource.ValueFromHome;
I'd be wary of static variables hanging around. Maybe you could use a MainViewModel to store that value. If you really need a static variable create a new static class in your project and put your ValueFromHome property in that class. The App probably isn't available since it is a Silverlight construct and not made to be available to all areas.

Creating New Array with Class Object in GWT

I would like to create a new array with a given type from a class object in GWT.
What I mean is I would like to emulate the functionality of
java.lang.reflect.Array.newInstance(Class<?> componentClass, int size)
The reason I need this to occur is that I have a library which occasionally needs to do the following:
Class<?> cls = array.getClass();
Class<?> cmp = cls.getComponentType();
This works if I pass it an array class normally, but I can't dynamically create a new array from some arbitrary component type.
I am well aware of GWT's lack of reflection; I understand this. However, this seems feasible even given GWT's limited reflection. The reason I believe this is that in the implementation, there exists an inaccessible static method for creating a class object for an array.
Similarly, I understand the array methods to just be type-safe wrappers around JavaScript arrays, and so should be easily hackable, even if JSNI is required.
In reality, the more important thing would be getting the class object, I can work around not being able to make new arrays.
If you are cool with creating a seed array of the correct type, you can use jsni along with some knowledge of super-super-source to create arrays WITHOUT copying through ArrayList (I avoid java.util overhead like the plague):
public static native <T> T[] newArray(T[] seed, int length)
/*-{
return #com.google.gwt.lang.Array::createFrom([Ljava/lang/Object;I)(seed, length);
}-*/;
Where seed is a zero-length array of the correct type you want, and length is the length you want (although, in production mode, arrays don't really have upper bounds, it makes the [].length field work correctly).
The com.google.gwt.lang package is a set of core utilities used in the compiler for base emulation, and can be found in gwt-dev!com/google/gwt/dev/jjs/intrinsic/com/google/gwt/lang.
You can only use these classes through jsni calls, and only in production gwt code (use if GWT.isProdMode()). In general, if you only access the com.google.gwt.lang classes in super-source code, you are guaranteed to never leak references to classes that only exist in compiled javascript.
if (GWT.isProdMode()){
return newArray(seed, length);
}else{
return Array.newInstance(seed.getComponentType(), length);
}
Note, you'll probably need to super-source the java.lang.reflect.Array class to avoid gwt compiler error, which suggests you'll want to put your native helper method there. However, I can't help you more than this, as it would overstep the bounds of my work contract.
The way that I did a similar thing was to pass an empty, 0 length array to the constructor of the object that will want to create the array from.
public class Foo extends Bar<Baz> {
public Foo()
{
super(new Baz[0]);
}
...
}
Baz:
public abstract class Baz<T>
{
private T[] emptyArray;
public Baz(T[] emptyArray)
{
this.emptyArray = emptyArray;
}
...
}
In this case the Bar class can't directly create new T[10], but we can do this:
ArrayList<T> al = new ArrayList<T>();
// add the items you want etc
T[] theArray = al.toArray(emptyArray);
And you get your array in a typesafe way (otherwise in your call super(new Baz[0]); will cause a compiler error).
I had to do something similar, I found it was possible using the Guava library's ObjectArrays class. Instead of the class object it requires a reference to an existing array.
T[] newArray = ObjectArrays.newArray(oldArray, oldArray.length);
For implementing an array concatenation method, I also stepped into the issue of missing Array.newInstance-method.
It's still not implemented, but if you have an existing array you can use
Arrays.copyOf(T[] original, int newLength)
instead.

Resources