I'm trying to create some simple variables which can be used to compare the current direction of a sprite in a game and obtain the opposite and adjacent directions. Such that if the player's direction is set to U by direction=U then I can access direction.opposite to get D
This example clearly shows i havent understood Lua 101 but I'm intrigued if and how I can get the functionality I want. Sure, I can replace the values with strings and compare to names but is there a way to preserve my intended functionality without resorting to string comparisons (or even comparing numbers in their place) which i suppose suffer in performance compared to checking just the table address.
U, D, R, L = {},{},{},{}
U = {opposite = D, adjacent = {R,L},name = 'up'}
D = {opposite = U, adjacent = {R,L},name = 'down'}
R = {opposite = L, adjacent = {U,D},name = 'right'}
L = {opposite = R, adjacent = {U,D},name = 'left'}
print(U.opposite.name)
Here print prints 'nil' when i'd like it to print 'down'