Steve C.
Steve C.

Reputation: 1353

How to reference enum in Unity3d alternate script?

I have one C# script titled TouchDetector.cs which looks exactly like this:

using UnityEngine;
using System.Collections;

public class TouchDetector : MonoBehaviour
{
  public delegate void deTouchEvent (enTouchType touchType);

  public static event deTouchEvent evTouchEvent;

  public enum enTouchType
  {
    SwipeLeft,
    SwipeRight,
    SwipeDown,
    SwipeUp,
  }   

  void Start ()
  {   
  }   

  void Update ()
  {   
    if (evTouchEvent == null)
        return;

    if (Input.GetKeyDown(KeyCode.UpArrow   )) evTouchEvent(enTouchType.SwipeUp   );
    if (Input.GetKeyDown(KeyCode.DownArrow )) evTouchEvent(enTouchType.SwipeDown );
    if (Input.GetKeyDown(KeyCode.LeftArrow )) evTouchEvent(enTouchType.SwipeLeft );
    if (Input.GetKeyDown(KeyCode.RightArrow)) evTouchEvent(enTouchType.SwipeRight);

    if (Input.touchCount > 0)
    {
        foreach (Touch t in Input.touches)
        {
            Vector3 swipe = t.deltaPosition * t.deltaTime;

            if (swipe.y >  0.5f) evTouchEvent(enTouchType.SwipeUp   );
            if (swipe.y < -0.5f) evTouchEvent(enTouchType.SwipeDown );
            if (swipe.x >  0.5f) evTouchEvent(enTouchType.SwipeRight);
            if (swipe.x < -0.5f) evTouchEvent(enTouchType.SwipeLeft );
        }
    }
}
}   

I'm trying to replace the following keycode calls to the corresponding swipe call above. I am somewhat new to programming and am trying to teach myself here. I've managed to get the swipe script working but have exhausted every avenue as far as how to get this as my controller if that makes sense. Thank you for any help or advice you can give me. I've tried the following.

if(SwipeLeft)
{
  //Do something here
}

Yet this is not working for me. I can't seem to reference the control type within my other control script. Here's what the script looks like in my Control.cs script that I am trying to replace:

if (Input.GetKeyDown(KeyCode.LeftArrow) && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
{
  if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.West;
  if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.North;
  if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.East;
  if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.South;
}

Instead of Input.GetKeyDown, I want it to say SwipeLeft or however I need to code it. Please help. I am getting very frustrated. I've looked up enum's but don't quite understand what I am reading or how to reference them. I'm assuming, from what I've read, that enum's are like int's in java. But if that's the case, how would I call those?

Upvotes: 0

Views: 15582

Answers (3)

TITAN_MIK
TITAN_MIK

Reputation: 1

Enumerations allow you to create a collection of related constants. Enumerations are a datatype. They can be created inside or outside a class. We would create an enum inside a class if only that class needed access to it. We'd create it outside a class if other classes also needed access to it.

It's explained very succinctly at this link: https://unity3d.com/learn/tutorials/topics/scripting/enumerations?playlist=17117

You just need to move your enum declaration to outside of your class. Right now it is inside your class so only that class can access it.

Upvotes: 0

Joetjah
Joetjah

Reputation: 6132

The enum keyword is used to declare an enumeration, a distinct type consisting of a set of named constants called the enumerator list. Every enumeration type has an underlying type, which can be any integral type except char. The default underlying type of the enumeration elements is int. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

This basically means you can have a list of things, and those things can be called based on their location in the list. For example:

enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth. A variable of type Days can be assigned any value in the range of the underlying type; the values are not limited to the named constants.

// keyword_enum.cs
// enum initialization:
using System;
public class EnumTest 
{
    enum Days {Sat, Sun, Mon, Tue, Wed, Thu, Fri};

    static void Main() 
    {
        int x = (int)Days.Sun;
        int y = (int)Days.Fri;
        Console.WriteLine("Sun = {0}", x);
        Console.WriteLine("Fri = {0}", y);
    }
}

Gives the output:

Sun = 1
Fri = 6

Since you have the enum in TouchDetector.cs public, you can use it in other classes referencing this class. Calling them is usually done through a Switch-statement.

For example:

public enum Color
{
    Red,
    Blue,
    Green
}

class Program
{
    static void Main(string[] args)
    {
        Color color = Color.Red;

        switch (color)
        {
            case Color.Red:
                break;

            case Color.Blue:
                break;

            case Color.Green:
                break;
        }
    }
}

Using case you can declare what to do when such thing is called. Instead of breaking, you'd be able to insert a method there, for example: doSomethingWithRed().

In your case, you could be saying something like:

var movingLeft = false;

case enTouchType.SwipeLeft:
    movingLeft = true;

And in the script you try to change, try:

if (movingLeft && m_playerJump <= -1.0f && m_playerSlide <=0.0f)
{
  if (m_playerDirection == enCellDir.North) m_playerNextDirection = enCellDir.West;
  if (m_playerDirection == enCellDir.East ) m_playerNextDirection = enCellDir.North;
  if (m_playerDirection == enCellDir.South) m_playerNextDirection = enCellDir.East;
  if (m_playerDirection == enCellDir.West ) m_playerNextDirection = enCellDir.South;
  //movingLeft = false; ?
}

Upvotes: 4

Jerdak
Jerdak

Reputation: 4056

Class (Example.cs):

public enum enTouchType{ one, two};
public class Example {
    public enum enTouchType { one, two };
}

Usage (SomeOther.cs):

void Foo(){
    enTouchType outer = enTouchType.one;
    Example.enTouchType inner = Example.enTouchType.one;

    switch(outer){
        case enTouchType.one:
            Console.WriteLine("outer.one");
            break;
    }
    switch(inner){
        case  Example.enTouchType.one:
            Console.WriteLine("inner.one");
            break;        
    }
}

[Slight Clarifying Update]

Enums are just a datatype so:

//(1) This won't work:
void evTouchEventExample(TouchDetector.enTouchType tt){
    if(SwipeLeft){}
}
//(2) Do something like:
void evTouchEventExample(TouchDetector.enTouchType tt){
    if(tt==TouchDetector.enTouchType.SwipeLeft){}
    //or use a switch statement, whatever.
}

All I was pointing out in my first example is that to use a nested enumerated type in another class you need to include the class name, TouchDetector.enTouchType. Your delegate referenced method for evTouchEvent should look like (2).

Complete example:

public class EnumTest : MonoBehaviour {
public delegate void deTouchEvent (enTouchType t);
public static event deTouchEvent  evTouchEvent;
public enum enTouchType { SwipeLeft, SwipeRight }

void Start () {
            // setup and do evt
    evTouchEvent += DummyTest.DummyDel;
    evTouchEvent(enTouchType.SwipeLeft);    
}
}
public class DummyTest {
public static void DummyDel(EnumTest.enTouchType t){
    if(t==EnumTest.enTouchType.SwipeLeft){
        Debug.Log("swipey_left");
    }
    else if(t==EnumTest.enTouchType.SwipeRight){
        Debug.Log("swipey_right");
    }
}
}

Upvotes: 1

Related Questions