- Home /
What structure code is this and how can i use it with enum?
I have an issue I'm trying to figure out what structure code is this found it in javascript and trying to convert it to C# I made it work by replacing {} to () and made it work as an object See second example.
The main issue is how can I use it in such way that will take enum to get correct value
Example: public enum Type { None, Normal, Fighting, Fire, Water, Grass, }
player.type1 is a value from enum "Fire". rival.type1 is a value from enum "Grass".
I would like to use it as Debug.Log("The value is:" Effectiveness + player.Type + rival.Type) In result i would like to get value 2.
Javascript Objective-oriented Programming with out class example below var Effectiveness= { Fire: { Normal: 1, Fire: 0.5, Water: 0.5, Grass: 2 }, Water: { Normal: 1, Fire: 2, Water: 0.5, Grass: 0.5 } };
Unity C# work around it it work fine but first time using it see example below
var Effectiveness= ( Fire: ( Normal: 1, Fire: 0.5, Water: 0.5, Grass: 2 ), Water: ( Normal: 1, Fire: 2, Water: 0.5, Grass: 0.5 ) ); Debug.Log(Effectiveness.Fire.Grass); // Will spit out result as 2
Answer by Hellium · Feb 22, 2019 at 01:08 PM
You could use a simple 2D array. It can be a little bit tedious to fill, but you could use a file you will be able to load the data from (formatted in CSV for instance). You could also use a ScriptableObject with a custom editor to format it in the inspector.
Following code not tested:
using UnityEngine;
using System;
public enum Type
{
Normal,
Fire,
Water
}
public MyMonoBehaviour : MonoBehaviour
{
private int[,] effectiveness ;
private void Start()
{
int enumLength = Enum.GetNames(typeof(Type)).Length;
effectiveness = new int[enumLength, enumLength];
SetEffectiveness( Type.Normal, Type.Fire, 1 );
// ....
Degug.Log( GetEffectiveness( Type.Normal, Type.Fire ) );
}
public int GetEffectiveness( Type type1, Type type2 )
{
return effectiveness [(int) type1,(int) type2];
}
public void SetEffectiveness( Type type1, Type type2, int value )
{
effectiveness [(int) type1,(int) type2] = value;
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Listing multiple objects 2 Answers
Creating different damage types with enums switches 0 Answers
Problem with getting a value from a enum 2 Answers