2

I'm trying to convert (and understand) this simplified piece of code from unityscript to C#:

private var actionList = new Array();                                                           

function Start()
{
actionList = new Array(12);

for(i=0; i<actionList.length; i++) 
    actionList[i] = new Array(4);
for(i=0; i<actionList.length; i++)
{
    actionList[i][1] = Rect(50+(i*55),50, 50, 50);        
    actionList[i][2] = new Array("action");     
}
actionList[0][3] = KeyCode.Alpha1;
actionList[1][3] = KeyCode.Alpha2;
actionList[2][3] = KeyCode.Alpha3;
actionList[3][3] = KeyCode.Alpha4;
actionList[4][3] = KeyCode.Alpha5;
actionList[5][3] = KeyCode.Alpha6;
actionList[6][3] = KeyCode.Alpha7;
actionList[7][3] = KeyCode.Alpha8;
actionList[8][3] = KeyCode.Alpha9;
actionList[9][3] = KeyCode.Alpha0;
actionList[10][3] = KeyCode.Minus;
actionList[11][3] = KeyCode.Equals;
}

function OnGUI()
{
for(i=0; i<actionList.length; i++)
    {
    if(actionList[i][1] != null)
        drawButton(actionList[i][1]);
    }
}

function drawButton(rect)
{
    GUI.Button(rect, "Hello");
}

The full code can be found here: Creating a Drag and Drop Spell Bar

My shot at this in C# (leaving out the keymappings):

using UnityEngine;
using System.Collections.Generic;

public class SpellBar : MonoBehaviour
{
private object[][] actionList;

void Start()
{
actionList = new object[12][];

for (int i=0; i<actionList.Length; i++)
    actionList[i] = new object[4];
for (int i=0; i<actionList.Length; i++)
{
    actionList[i][1] = new Rect(50+(i*55), 50, 50, 50);
}

//Keymappings
}

void OnGUI() 
{
for(int i=0; i<actionList.Length; i++)
    {
    if(actionList[i][1] != null)
        DrawButton(actionList[i][1]);
    }
}

void DrawButton(Rect rect)
{
    GUI.Button(rect,"Hello");
}
}

The error I get here is in OnGui, when using DrawButton(actionListi). Error: cannot convert 'object' expression to type "UnityEngine,Rect'.

I am not an experienced programmer in either unityscript or C#. I searched google for hours, trying Lists, ArrayLists etc, but cant find the solution. Thnx in advance!

EDIT 27-11-2012
So, I ran into a new related problem. Don't know if I should edit this in here, but I couldn't format properly in a reply.
In the JavaScript code I'm trying to convert, the following code is used to store a class in an array:

var openSlot = getNextSlot(actionList);    
if(openSlot != -1)
    actionList[openSlot][0] = new classEvilSmile();

While this is not exactly the way I'd want it, I would like to be able to store a method in an array, but I can't find a way to do it in C#. Is it simply impossible?
(Example) I'm trying to do something like this:

//In addition to what I posted above
void Start()
{
actionList[i][0] = void FireBall();
}

void FireBall()
{
Texture2D icon = Resources.Load("icon_fireball") as Texture2D;
String description = new String("Shoot a ball of fire");

//etc
}
2
  • 2
    That's not Java. It may be Javascript, but it's not Java. Commented Nov 23, 2012 at 22:12
  • I cringe when I hear people confuse "Java" and "Javascript". There are superficial similarities - but in most significant aspects, "Java" and "Javascript" are worlds apart. I edited the post and changed the title to "Javascript". Commented Nov 24, 2012 at 3:51

1 Answer 1

1

Unlike Javascript, C# is strong-typed, which mean you cannot pass an object to a method that ask for a Rectangle. You must give the method that exact type, or a child derived from that type.

So, instead of

private object[][] actionList;

you should have

private Rectangle[][] actionList;

Strong-type language performs a lot of compile time validation to see if you don't pass the wrong variable type to the wrong method parameters. The advantage is two fold, first you don't have to perform test at runtime to know if the variable is of the correct type. Second, it gives an easy way to find errors in your code without the need to run every part of it. If it compiles, you're already 80% done.

However, if you want to combine different type within that array, you have multiple choices. You can use a class like Tuple<> to combine a set of object that doesn't have matching type or you can cast your object to Rectangle before feeding it to the method like:

DrawButton((Rectangle)actionList[i][1]);

However, if the cast is invalid, you will get a runtime assert.

Sign up to request clarification or add additional context in comments.

6 Comments

Thnx for the quick reply! I couldn't really find documentation on Tuple<> in Unity (wouldn't know how to implement) but the casting to rectangle works. Would you say that it is not the right way to go at it? And if so, what would you suggest instead of the object array? Thnx again
@downdf: Tuple<> is not an Unity class, it's a .NET 4+ class, but in essence, it's just a generic wrapper around a set number of object. Let's say, you always have a Rectangle and a string for a name. You can do List<Tuple<string, Rectangle>>. So, that's a list that always contain a combo of string and Rectangle. msdn.microsoft.com/en-us/library/system.tuple(v=vs.100).aspx The best way is when you can manage without a cast, as it involve some little runtime cost and generally make code easier to read as you know what is what. But if you prefer it with a cast, it fine too.
I edited my OP with a related problem (hope thats the way things are done here), would appreciate if you could look at it. Cheers!
@downdf: Reference to a void? You mean an empty slot in an array? The keyword is null. As in MyObject = null; if (MyObject == null). Like I said, the variable are typed in C#, you don't need to put something in it for the compiler to know what should or can go in it.
Damn you're quick! Not sure if I totally understand your answer, but I think I didn't ask the question clearly. What I think I'm trying to ask is: can you store a method in an array? Edited the OP "reference to a void" to "method".
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.