Reference variable

Hi, bit of background to the problem: I’m creating a board game and I’m mainly using javascript. Each piece can be anywhere on the board. I’ve created a script, attached to my floor called “startup” that holds all these global class variables. Just like chess they are A5, B9 etc.

Each unit also has a script attached to it, telling it what moves are possible. I’m however having difficulty updating my global variables after a move has happened.

i.e accessing the script attached to the floor when doing

startup.B9 works fine.

But I dont know in advance what position each unit is at so I added another variable to the script attached to each unit called : “current_pos”

And this is where i’m having a big problem!! Referencing startup.current_pos does not work as unity sees this entire call as a string. How can I force unity to see current_pos as variable that when I do startup.current_pos it will actually send startup.B9 or whatever position the unit is on to the startup script and not startup.current_pos

Any help would be greatly appreciated.

     if (skuif4_att_check == "none"){
       i = 0.0;
         while (i < 1.0) {
             i += 0.08;
             animation.CrossFade("CrouchRun");
             transform.position = Vector3.Lerp (transform.position, move4, i);
            yield; 
         }

         animation.CrossFade("Crouch");
         destroymove();
         move_check = "empty";
         current_pos = move4hit; 
 
             //1
         Debug.Log(current_pos);
             //2
         Debug.Log(startup.M6);
             //3
         Debug.Log(startup.current_pos);
             return;

1 = M6
2 = tank
3 = null

How can I go about fixing 3?

startup.js contains the following:

static var J1;
static var J2;
static var J3;
static var J4;
static var J5;
static var J6;
static var J7;
static var J8;
static var J9;
static var J10;
static var J11;
static var J12;
static var J13;
static var J14;
static var J15
etc....

startup is a script (class) and so to access the instance variables on it you need to get an instance of it.

If the startup script is attached to an object called “Floor” then you would do that like this:

 GameObject.Find("Floor").GetComponent(startup).current_pos = tag;

@negate wants to access a variable position from a board he was modelling in a series of variables, where each variable represents a board position using chess like rank and file notation. The solution was not to use a different variable for each board position but make a dictionary and key into that instead.

You have two choices either use an array to hold your positions rather than lots of variables (that really isn’t a good plan :slight_smile: or you use a Dictionary which can have an index of “A3” “D4” etc.

Dictionary:

   import System.Collections.Generic;

   static var positions = new Dictionary<String, String>();

Now you can set positions like this:

   positions["A3"] = whatever;

And get them using

   var someVariable = startup.positions[current_pos];

One thing about dictionaries though is that you would need to initialise all of the values (you can’t get out something you didn’t put it).

That is different with an array:

    static var positions = new String[15,15];

But now your current_pos needs a rank and a file element - you could use a Vector2

    var current_pos : Vector2;

    current_pos.x  = 1;
    current_pos.y = 3;

    var someVariable = startup.positions[current_pos.x, current_pos.y];