Code: Select all
player.x, player.y, cols, len = world:move(player, player.x + player.dx, player.y + player.dy)
player.xAcc, player.yAcc = 0, player.yAcc * 0.45
Code: Select all
SW, SH = 10, 12
Code: Select all
player.x, player.y, cols, len = world:move(player, player.x + player.dx, player.y + player.dy)
player.xAcc, player.yAcc = 0, player.yAcc * 0.45
Code: Select all
SW, SH = 10, 12
The right side of an assignment is always assigned to the left side. There can only be variables on the left, while the right side can be arbitrary expressions. Lua evaluates all expressions on the right side (going from left to right) and assigns the results to the variables on the left (also from left to right).But the above assignments seem to be slightly different. I'm not sure what's getting assigned to what in these lines.
Code: Select all
left1, left2, left3 = expression1, expression2, expression3
Functions in Lua can have more than one return value. The function world:move() probably returns 4 values. This line assigns these values to the variables player.x, player.y, cols and len.hasen wrote: ↑Tue Jan 30, 2018 8:37 amCode: Select all
player.x, player.y, cols, len = world:move(player, player.x + player.dx, player.y + player.dy)
Same here, just without the function call. player.xAcc is set to 0, player.yAcc is set to player.yAcc * 0.45. It's equivalent toCode: Select all
player.xAcc, player.yAcc = 0, player.yAcc * 0.45
Code: Select all
player.xAcc = 0
player.yAcc = player.yAcc * 0.45
Ok I see, so it was like I thought, it was just that the world:move was returning four values. And for the second one it seems obvious now, don't know why I didn't get that before. Thanks very much for your help.grump wrote: ↑Tue Jan 30, 2018 9:01 amThe right side of an assignment is always assigned to the left side. There can only be variables on the left, while the right side can be arbitrary expressions. Lua evaluates all expressions on the right side (going from left to right) and assigns the results to the variables on the left (also from left to right).But the above assignments seem to be slightly different. I'm not sure what's getting assigned to what in these lines.
Code: Select all
left1, left2, left3 = expression1, expression2, expression3
Functions in Lua can have more than one return value. The function world:move() probably returns 4 values. This line assigns these values to the variables player.x, player.y, cols and len.hasen wrote: ↑Tue Jan 30, 2018 8:37 amCode: Select all
player.x, player.y, cols, len = world:move(player, player.x + player.dx, player.y + player.dy)
Same here, just without the function call. player.xAcc is set to 0, player.yAcc is set to player.yAcc * 0.45. It's equivalent toCode: Select all
player.xAcc, player.yAcc = 0, player.yAcc * 0.45
Code: Select all
player.xAcc = 0 player.yAcc = player.yAcc * 0.45
Keep in mind two more things;
Code: Select all
player.x, player.y, cols, len = world:move(player, player.x + player.dx, player.y + player.dy) -- this probably returns 4 values, although there's no rule that it needs to; if it returns more than how many vars are on the left side of the equals sign, they get discarded, if it returns less, then the rightmost vars will be set to nil.
a,b = fn(), 'whatever', -- fn will return one value because of the comma
d,d = (fn()) -- fn will return one value because of the parentheses around it
Code: Select all
function f()
return 1, 2
end
function g(a,b,c,d)
print(a,b,c,d)
end
g(f(),f(),f()) -- prints 1,1,1,2
w,x,y,z = f(), f(), f() -- w=1 x=1 y=1 z=2
w,x,y,z = f(), f(), (f()) -- w=1 x=1 y=1 z=nil
Code: Select all
function myprint(t)
for i,v in ipairs(t) do io.write(tostring(v)) end
io.write('\n')
end
myprint({1,2,(function() return 3,4 end)()}) -- table + anon function at the end, not wrapped in parentheses --> 1,2,3,4
myprint({1,2,(function() return 3,4 end)(),5}) -- table + anon function not at the end, not wrapped in parentheses --> 1,2,3,5
myprint({1,2,((function() return 3,4 end)())}) -- table + anon function at the end, wrapped in parentheses --> 1,2,3
myprint({1,2,((function() return 3,4 end)()),5}) -- table + anon function not at the end, wrapped in parentheses --> 1,2,3,5
-- and just for some extra shenanigans, parameters to the anonymous function while it's at the last table slot
myprint({1,2,(function(a,b) return a,b end)(3,4)}) --> 1,2,3,4
myprint({1,2,(function(a,b) return a,b end)(3)}) --> 1,2,3
myprint({1,2,(function(a,b) return a,b end)(nil,4)}) --> 1,2,nil,4
myprint({1,2,(function(a) return a end)(3,4)}) --> 1,2,3
myprint({1,2,(function(a) return a end)(nil,4)}) --> 1,2
Code: Select all
love.graphics.draw(drawable, x, y, r, sx, sy, ox, oy, kx, ky)
Code: Select all
love.graphics.draw(drawable, obj:getPosition(), r, obj:getScale(), ox, oy, kx, ky)
Users browsing this forum: Bing [Bot], monsieur_h and 4 guests