Can someone assist me in converting this from JS to c#

Hey there,

I was on the unifycommunity and i saw this drag and drop spell bar thing. PERFECT I needed to know how to do this, as i assumed you needed to use arrays but i must admit due to me still being on shaky ground with this and no reference material I couldn’t figure out quite how to write it.

Anyways the item im talking about is here http://wiki.unity3d.com/index.php?title=Creating_a_Drag_and_Drop_Spell_Bar

As you can see the main hud and class’s are in Js - I’ve tried converting some from js to c#, but I keep getting unknown problems and it’s starting to make me want to smash my skull against a brick wall.

Can someone please rewrite them for me in c# so I can study them to see where my issues were. I would link my script, but it’s tied into a huge hud script and you’d have to weave through allot of stuff, it’s almost 5k lines long.

Thanks.

Check this out :

http://answers.unity3d.com/questions/12911/What-are-the-Syntax-Differences-in-C-and-Javascript.html

~lanDog

Thanks landon.

I managed to convert most of it, the last thing i need to do is to convert the array names:

private var clickInfo = new Array();
private var hoverInfo = new Array();

I was told this was the correct translation:
public var clickInfo = new List();
public var hoverInfo = new List();

However, it seems it is not. - I looked at the resource you provided with me and bookmarked it, it however doesn’t display this translation (Unless im blind). - Can you please assist a newb ? :slight_smile:

Many thanks.

Thanks landon.

I managed to convert most of it, the last thing i need to do is to convert the array names:

private var clickInfo = new Array();
private var hoverInfo = new Array();

I was told this was the correct translation:
public var clickInfo = new List();
public var hoverInfo = new List();

However, it seems it is not. - I looked at the resource you provided with me and bookmarked it, it however doesn’t display this translation (Unless im blind). - Can you please assist a newb ? :slight_smile:

Many thanks.

public List clickInfo = new List();
public List hoverInfo = new List();

Should be what you are looking for.

Make sure you include Import System.Collections.Generic if you’re using lists.

Thanks, guys…

Thank you wolf, snack and landon. This has been a great learning process for me and I’ve managed to learn from previous mistakes… However…

I’m still having an issue with this. I think I’ve made this into a disaster, but I also want to thank you guys above, I’m learning from my mistakes which is awesome. Anyways, here is the script in it’s entirety:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Abartest : MonoBehaviour {
public Texture2D icon1;
public Texture2D icon2;
public Texture2D highlight;
public Texture2D activated;
public Texture2D slotBG;
public bool dragmode = false;
	// If we are dragging or not
	//********Array funcs*********\\
public List<Vector3> clickInfo = new List<Vector3>();								// Information where the mouse has clicked
public List<Vector3> hoverInfo = new List<Vector3>();								// Information where the mouse is currently hovering 
public List<Vector3> actionList = new List<Vector3>();							// List of action classes, rects, and requirements
public List<Vector3> bagList = new List<Vector3>();								// List of bag classes, rects, and requirements
 
void Awake  () {
	//	These should be loaded in Awake  ().
	//	Resouces.Load doesn't like being called in the declaration stage.
		
	icon1 = (Texture2D)Resources.Load("action-icon-1",typeof(Texture2D));
	icon2 = (Texture2D)Resources.Load("action-icon-2",typeof(Texture2D));
	highlight = (Texture2D)Resources.Load("slot-highlight",typeof(Texture2D));
	activated = (Texture2D)Resources.Load("slot-active-highlight",typeof(Texture2D));
	slotBG = (Texture2D)Resources.Load("slots-bg",typeof(Texture2D));
}
 
void Start() {
	// Set actionList attributes
	actionList = new Array(12);
	foreach(i=0; i<actionList.length; i++) actionList[i] = new Array(4);
	foreach(i=0; i<actionList.length; i++) {
		actionList[i][1] = Rect(203+(i*29),203, 24, 24);        // Set slot rect
		actionList[i][2] = new Array("action");     // Set slot restrictions
	}
	// Set actionList keymappings
	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;
 
	// Set bagList attributes
	bagList = new Array(12);
	for(i=0; i<bagList.length; i++)
		bagList[i] = new Array(3);
	for(i=0; i<bagList.length; i++) {
		bagList[i][1] = Rect(203+(i*29), 237, 24, 24);      // Set slot rect
		bagList[i][2] = new Array("action");                        // Set slot restrictions
	}
}
void Update (){
	// Handle mouse button down event
	if(Input.GetMouseButtonDown(0))
		storeMouseLoc();
	// Handle mouse down event
	if(Input.GetMouseButton(0))
		dragCheck();
	// Handle mouse up event
	if(Input.GetMouseButtonUp(0))
		compareMouseLoc();
}
 
void OnGUI(){
	// Reset mouse over info to null
	hoverInfo = new Array();
 
	// Draw the slot backgrounds
	GUI.DrawTexture(Rect(200,200,349,30), slotBG);
	GUI.DrawTexture(Rect(200,234,349,30), slotBG);
 
	// Add to buttons
	if(GUI.Button(Rect(200,300,200,25), "Add items to action bar")){
		var openSlot = getNextSlot(actionList);
		if(openSlot != -1){
			actionList[openSlot][0] = new classEvilSmile();
		}
	}
	if(GUI.Button(Rect(200,330,200,25), "Add items to bags")){
		openSlot = getNextSlot(bagList);
		if(openSlot != -1)
			bagList[openSlot][0] = new classFistPunch();
	}
 
	// Draw Bag GUI
	foreach(i=0; i<actionList.length; i++){
		// If there is an item class in the slot, draw the button
		if(actionList[i][0] != null)
			drawButton(actionList[i][1], actionList[i][0].icon, i, actionList);
		// Otherwise just check if the mouse is over the rect
		else
			checkRectHover(actionList[i][1], null, i, actionList);
	}
 
	// Draw Action Bar GUI
	foreach(i=0; i<bagList.length; i++){
		// If there is an item class in the slot, draw the button
		if(bagList[i][0] != null)
			drawButton(bagList[i][1], bagList[i][0].icon, i, bagList);
		// Otherwise just check if the mouse is over the rect
		else
			checkRectHover(bagList[i][1], null, i, bagList);
	}
 
	// Handle keyboard input
	getKeyInput();
 
	// If we are dragging, draw the drag icon
	if(dragMode)	drawDragIcon();
}
 
// Custom function to draw a GUI icon and store information at the same time
bool drawButton(GameObject rect,GameObject image,GameObject index,GameObject pointer){
	bool drawIcon = true;							// Boolean for if we should draw the icon (dragging)
	if(clickInfo.length > 0)											// If there was a previous click
		if(clickInfo[0] == rect  dragMode)					// If the clicked rectangle is this rect and we are dragging
			drawIcon = false;										// Dont draw the icon
	if(drawIcon)														// If we are good to draw the icon
		GUI.DrawTexture(rect, image);							// Draw the icon
	checkRectHover(rect, image, index, pointer);			// Now check if the mouse is hovering over the rect
}
 
// Checks if mouse is hovering over a rect
bool checkRectHover(GameObject rect,GameObject image,GameObject index,GameObject pointer){
	if(rect.Contains(getMousePos())){							// Check for mouse over
		hoverInfo.Push(rect);										// Store the rect info
		hoverInfo.Push(image);										// Store the icon info
		hoverInfo.Push(index);										// Store the index info
		hoverInfo.Push(pointer[index][0]);						// Store if slot contains an obj
		hoverInfo.Push(pointer);									// Pointer to class array
		hoverInfo.Push(getMousePos());						// Store click position
		if(pointer[index][0]){										// If there is an item that our mouse is over
			if(!Input.GetMouseButton(0))
				GUI.DrawTexture(rect, highlight);				// Draw mouse over highlight
			else
				GUI.DrawTexture(rect, activated);				// Draw in use highlight
		}
	}
}
 
// Stores current location on mouse down, only if we are above an item
bool storeMouseLoc(){
	if(hoverInfo.length > 0  hoverInfo[3] != null)
		clickInfo = hoverInfo;
}
 
// Checks for mouse dragging
bool dragCheck(){
	if(clickInfo.length > 0)
		if((getMousePos() - clickInfo[5]).sqrMagnitude > 60) dragMode = true;
}
 
// Compares current location to stored location on mouse up
bool compareMouseLoc(){
	// If we clicked a slot, and we are above a slot now, and there was an item in the previous slot
	if(hoverInfo.length > 0  clickInfo.length > 0){
		// If we are in the same bar and same location and not dragging, use the item
		if(hoverInfo[2] == clickInfo[2]  checkReq(clickInfo[4][hoverInfo[2]][0].type, hoverInfo[4][hoverInfo[2]][2])  !dragMode){
			// If there is an item in the slot, use it
			if(clickInfo[4][clickInfo[2]][0]){
				clickInfo[4][clickInfo[2]][0].use();
			}
		}
		// If we are in the same bar and same location and dragging, do nothing
		if(hoverInfo[2] == clickInfo[2]  hoverInfo[4] == clickInfo[4]  dragMode){
		}
		// If there is an item in the slot we are moving to
		if(hoverInfo[4][hoverInfo[2]][0]){
			var clickedItemReq = checkReq(clickInfo[4][clickInfo[2]][0].type, hoverInfo[4][hoverInfo[2]][2]);
			var hoverItemReq = checkReq(hoverInfo[4][hoverInfo[2]][0].type, clickInfo[4][clickInfo[2]][2]);
			if(clickedItemReq  hoverItemReq){
				var clickSwap = clickInfo[4][clickInfo[2]][0];
				var hoverSwap = hoverInfo[4][hoverInfo[2]][0];
				clickInfo[4][clickInfo[2]][0] = hoverSwap;
				hoverInfo[4][hoverInfo[2]][0] = clickSwap;
			}
		}
		// If the slot is empty that we are moving to
		else{
			// Check if click item can move to empty slot
			if(checkReq(clickInfo[4][clickInfo[2]][0].type, hoverInfo[4][hoverInfo[2]][2])){
				hoverInfo[4][hoverInfo[2]][0] = clickInfo[4][clickInfo[2]][0];
				clickInfo[4][clickInfo[2]][0] = null;
			}
		}
	}
	// If we clicked an item, but are not above an item now, destroy the item
	else if(hoverInfo.length == 0  clickInfo.length > 0){
		clickInfo[4][clickInfo[2]][0] = null;
	}
	clickInfo = new Array();			// Reset mouse clickInfo
	dragMode = false;					// Reset dragMode
}
 
// Returns a rect that is slightly smaller then the given rect
bool getHoverRect(GameObject hoverRect){
	hoverRect.x += 1;
	hoverRect.y += 1;
	hoverRect.width -= 1;
	hoverRect.height -= 1;
	return hoverRect;
}
 
// Returns the next available open slot in specified list, -1 if none found
bool getNextSlot(GameObject[] list){
	var slotFound = -1;
	for(i=0; i<list.length; i++){
		if(!list[i][0]){
			slotFound = i;
			break;
		}
	}
	return slotFound;
}
 
// Returns true or false if item meets list requirements
bool checkReq(GameObject item,GameObject[] list){
    bool result = false;
    for(i=0; i<list.length; i++){
       if(item == list[i]){
         result = true;
         break;
       }
    }
    return result;
}
 
// Draws the icon that is dragging
void drawDragIcon(){
	var pos = getMousePos();
	GUI.DrawTexture(Rect(pos.x - 15, pos.y - 15, 24, 24), clickInfo[1]);
}
 
// Returns the real mouse position
void getMousePos(){
	float pos = new Vector2(Input.mousePosition.x, (Screen.height - Input.mousePosition.y));
	return pos;
}
 
// Function that checks for keyboard input from user
void getKeyInput(){
	for(i=0; i<actionList.length; i++){
		if(Input.GetKey(actionList[i][3])  actionList[i][0]){
			GUI.DrawTexture(actionList[i][1], activated);
		}
		else if(Input.GetKeyUp(actionList[i][3])  actionList[i][0]){
			actionList[i][0].use();
		}
	}
}
}

I get like 250 errors, though :/… Can someone give it a look over and slap corrections on it for me. Or explain where im going wrong so i can do it myself?

Many thanks you awesome people :slight_smile: