I just can not figure out how to translate this operator from Lua to C # operator?

--Это некая карта из массива байт --0x0 используется как ничего, а 0x1 как стена local map = { { 0x1, 0x1, 0x1, 0x1 }, { 0x1, nil, nil, 0x1 }, { 0x1, nil, 0x1, 0x1 }, { 0x1, 0x1, 0x1, 0x1 }, } local yMap, xMap = math.floor(player.y / 32), math.floor(player.x / 32) if map[yMap] and map[yMap][xMap] then return map[yMap][xMap] end 

Well, to the point - I just can not understand how it works in general in Lua?

I'm if map[yMap] and map[yMap][xMap] then about the line with the comparison: if map[yMap] and map[yMap][xMap] then

I tried to understand and write this in C #

 if ((map[yMap] != 0x0) & (map[yMap][xMap] != 0x0)) 

But all is in vain, the compiler of course swears that I am comparing byte [] and byte. I don’t understand why it works on Lua ?! Maybe he takes a byte from the beginning of the array?

And yes, as I correctly understood:

if map[0] then and if map[0] not nil then is this the same thing?

That is, if NOT nil returns true?

-

In Lua, I almost don’t know anything, I just need to transfer the Lua script to C #, please explain to the fool how it works?

  • Why so in the forehead then? What is normal for a scripting language is not suitable for strongly typed. So in order to translate all this, we need a context - what exactly does this code do in the context of the rest? And you have to redo much straight from the very foundation. - Sergey
  • False is only false and nil . "if map [yMap] and map [yMap] [xMap] then" - first the existence of map[yMap] is checked map[yMap] and empty lines also match), then the existence of map[yMap][xMap] - FareakyGnome
  • And if map[0] not nil then causes an error. The table key can also be false (but in general, everything except nil): if map[0]~=nil then . And in louach arrays, indices with 1 - in the map will always not work. - FareakyGnome

1 answer 1

if map [yMap] - yes this is the same as asking map [yMap] exists? In strongly typed languages, a two-dimensional array is defined in advance, as you have. And this test can be omitted.

  public struct Coord { public int x, y; } Coord player; player.y = 3992; player.x = 62; int[,] map = { { 0x1, 0x1, 0x1, 0x1 }, { 0x1, 0x0 , 0x0, 0x1 }, { 0x1, 0x0 , 0x1, 0x1 }, { 0x1, 0x1, 0x1, 0x1 }, { 0x1, 0x1, 0x1, 0x1 }, } ; int xMap = (int)Math.Floor( (double) player.x / 32); int yMap = (int)Math.Floor( (double) player.y / 32); Console.WriteLine( ); if (xMap<=map.GetLength(1) & yMap<=map.GetLength(0)) if (map[yMap,xMap] == 0x0) { Console.WriteLine("Ok"); } else Console.WriteLine("None"); else Console.WriteLine("Bad index"); } 

But if there is a need to do a check, then something like this:

 (map.GetType().IsArray) && (map.Rank==2) 
  • if (map[yMap][xMap] == 0x0) works, thanks! How I did not immediately guess? Probably no need to sit until morning .. - TheRio Miner
  • And if yMap or xMap becomes negative or becomes longer than the array length (goes beyond the border indices) in Lua, is it going beyond the array boundaries, everything will be considered as nil? - TheRio Miner
  • one
    if we assume that x is the horizontal dimension of a and y vertically, then it works like this: xMap <= map.GetLength (1) & yMap <= map.GetLength (0) - Mike V.
  • Yes, I did a little differently: if ((yMap < 0 | xMap < 0) | ((map.Length <= yMap) | (map[0].Length <= xMap))) If it goes beyond the boundaries, it will return 0x0 , and it works :) (I got an error from GetLenght ) - TheRio Miner