Z
Z
Zimaell2020-06-18 11:21:51
C++ / C#
Zimaell, 2020-06-18 11:21:51

How should the logic and syntax of passing data to another class work?

The essence is simple, in one class there is an array with data about opponents, then there is an enemy behavior class where this array should be connected and actually give each opponent the necessary properties based on the data from the array ...
I still don’t understand how to do this.

Based on PHP knowledge, the array should look like this

$arr_enemy=array(
  'enemy_1'=array(
    'name'=>'enemy 1',
    'life'=>100
    ),
  'enemy_2'=array(
    'name'=>'enemy 2',
    'life'=>50
    )
  );

I don’t know how to make such an array in C#, since the array is multidimensional and, moreover, with different data types ...

well, then let’s say I already have an array, then how to assign it in another class
public class EnemyData : MonoBehaviour{
Dictionary<string,string> ArrayEnemyData = new Dictionary<string,string>
  {
    ["EnemyName"] = "Enemy 1",
    //["EnemyLife"] = 100, 
  };
}

public class Enemy : MonoBehaviour{
  arr = EnemyData.ArrayEnemyData;
}

of course it doesn't work that way...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LiptonOlolo, 2020-06-18
@LiptonOlolo

Use string ("enemy_1" etc.) as a key, and the Enemy
(Dictionary) class as a value.
You get something like:

var name = enemies["enemy_1"].Name;
var life = enemies["enemy_1"].Life;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question