- Home /
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....
Could you post the code that you are using to make these calls?
at work right now, will post it in less than 2 hours when i'm back home. But it's standard code. In the startup.js script I have static var A1; static var A2; etc.
on my unit.js I have var current_pos; and this variable does update correctly to A2 or A1 etc as the unit moves.
calling startup.A1 from the unit.js works as that variable does exist in the startup script. Calling startup.current_pos does not work as there is no such variable in the startup script. In PHP I did ".$varname." in a situation like the one above, but I have no idea how to do this in javascript
added the code, hope it helps. The unity function which calls a scrip returns a string.
In php I was able to break the string with ".$varname." somehow I need to do this here
Ok I'm a bit confused, it happens at my age :) Where do you define the current_pos variable - can you post that? The reason I ask is the way you have it would only work if it was a static (presu$$anonymous$$g startup is the name of a script). Can you also list the definition of $$anonymous$$6?
BTW if startup is the name of a script, following the convention of using an uppercase first letter makes these things easier to spot...
It sounds like you really want to be using arrays. Whenever you start having sequentially-labeled variables (A0,A1,A2,A3), it's usually a good idea to switch to an array ins$$anonymous$$d. Something like position_information(Letter, Number) is much more maintainable and accessible than having upwards of 10 separate variables and huge if/else blocks.
Unless I misunderstand. :)
Answer by whydoidoit · Jun 03, 2012 at 11:06 AM
@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 :) 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];
Answer by whydoidoit · May 31, 2012 at 11:22 AM
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;
nope it gives me the same problem.
Debug.Log(GameObject.Find("floor").GetComponent(startup).current_pos);
Returns:
$$anonymous$$issingFieldException: Field 'startup.current_pos' not found.
So its again not resolving the current_pos to A3 or whatever the value of curent_pos is.
I added a few lines from the startup.js at the main question.
I think I might now what is happening. current_pos is defined as string, in essence when I write GameObject.Find("floor").GetComponent(startup).current_pos, unity perhaps sees this :
GameObject.Find("floor").GetComponent(startup)."A3";
and of course "A3" does not exist, only A3 does, if this is the case how can I remove string qoutes?
That isn't how it works! I really need to see the code that actually creates current_pos - how are you adding that? current_pos is a variable right? Or something else and I've missed the point entirely :)
yes it's variable. Take for example this code which I use to deter$$anonymous$$e position of unit:
a = 0;
temprow = "Z";
while (a < 15){
temprow = alphaChars.Substring(a,1);
if (temprow == row){
move5coor = alphaChars.Substring(a+1,1);
break;
}
a++;
}
move5hit = move5coor+move1num;
move5hit for example will = "A4" or "A3" etc.
later in the script I check where the mouse has clicked then
current_pos = move5hit; or current_pos = move4hit;
etc
So throughout the script current_pos = a string value
I still can't see where you do something like:
var current_pos : String;
In the class definition or somewhere else.
All I do is this at the top of the script:
var default_pos = "A5"; var current_pos = default_pos;
Doesnt this define the current_pos as string?
I changed it to
var default_pos = "A5"; var current_pos : String; current_pos = default_pos;
Still gives me the same error:
$$anonymous$$issingFieldException: Field 'startup.current_pos' not found.