- Home /
The question is answered, right answer was accepted
c# to js conversion
I have trouble translating this peace of code from c# to js. here is the c# code:
public byte Block(int x, int y, int z){
if( x>=worldX || x<0 || y>=worldY || y<0 || z>=worldZ || z<0){
return (byte) 1;
}
return data[x,y,z];
}
and here is my js code:
function Block(x : int, y : int, z : int) : byte{
if( x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z<0){
return ???? 1;
}
return data[x, y, z];
}
I would appreciate any help or suggestions.
I have tried anything by now and nothing works. I think that
return data[x, y, z];
is not right either.
It is right, assu$$anonymous$$g data
is of type byte[,,]
.
data is of type byte[,,], but it gives me error when I play it:
NullReferenceException: Object reference not set to an instance of an object (wrapper managed-to-managed) object:ElementAddr (object,int,int,int)
You didn't initialize the array, then, so it's just null.
Answer by Eric5h5 · Jan 19, 2015 at 05:54 PM
Remove the ????, so it's just
return 1;
It will be implicitly converted to byte, since that's what the function returns.
Answer by tanoshimi · Jan 19, 2015 at 05:24 PM
I'm not a Javascript person, so the following is a guess (though, in the absence of any other responses, a guess is still better than nothing, right?)
Javascript does not support multidimensional arrays. You don't include the line of code on which data
is declared, but you certainly won't be able to access it as data[x,y,z]. What you can do is create an array in which each element is an array (and then each element in that array is an array, etc. etc.). In which case, you'd access a 3rd-level element as
data[x][y][z]
As for the first problem, I believe you need to explicitly cast a variable of type byte at declaration. So, that would make the whole solution something more like:
function Block(x : int, y : int, z : int) : byte{
var output : byte;
if( x >= worldX || x < 0 || y >= worldY || y < 0 || z >= worldZ || z<0){
output = 1;
}
else {
output = data[x][y][z];
}
return output;
}
If that doesn't work, er... just use C# - it's better :)
Javascript (A$$anonymous$$A Unityscript) certainly does support multi-dimensional arrays.