0


function custom(m)
    local array = {}
    for i=1,m do
        array[i]={0,0}
    end
    return array
end

cache={}
for i=1,10 do
    cache[i]=custom(32)
end

addhook("always","omg")
function omg()
    cache[10]=cache[9]
    cache[9]=cache[8]
    cache[8]=cache[7]
    cache[7]=cache[6]
    cache[6]=cache[5]
    cache[5]=cache[4]
    cache[4]=cache[3]
    cache[3]=cache[2]
    cache[2]=cache[1]

    for i=1,32 do
        cache[1][i][1],cache[1][i][2]=player(i,"x"),player(i,"y")
    end
end

what i want it to do: make a table that holds every player's position for the previous 10 frames. e.g. cache[5][16][2] shows the y position of player #16 5 frames ago.

what it is doing: it is only showing the current position. old positions are not saved or something. it feels like cs2d is executing my list of cache[x]=cache[x-1] in reverse o.OO

flag offensive
asked 2010-10-07 21:46:44.251465
1
add comment
4 Answers:
0


Haha, everyone trips over this at least once. When I first encountered this, I was like WTF LUA HAZ BROKEN (similarly for Python), but it's actually a really intuitive design feature of Lua (Feature, not bug). http://failboat.me/2010/python-tip-copying-an-object/ for a little preview.

Basically, tables (and userdata) are stored as references to a memory location rather than as an actual array. For example, try the following example:

cache = {{1,1},{2,2}}
-- #Let's emulate a queue
cache[2] = cache[1]
cache[1][1] = 0; cache[1][2] = 0;
-- #Lee's magic goggle: cache = {{0,0},{0,0}}
-- #WTF?
-- #Let's try that again:
cache[1] = {1,1}
-- #Lee's magic goggle: cache = {{1,1},{0,0}}

The assignment operator automatically assigns the reference of the right-hand value to the left-hand variable. So the following happened:

Assume that we have the following memory blocks:

MEM - cache = x - MEM
MEM - x| cache[1] = y1 | cache[2] = y2 | - MEM
MEM - y1| cache[1][1] = z1 | cache[1][2] = z2 | - MEM - y2| cache[2][1] = z3 | cache[2][2] = z4 | - MEM
  1. cache[2] = cache[1] => cache[2] now points at y1
  2. cache[1][1] = y1+offset(1) = 0 => cache[1][1] now points at 0 (literals are not references), same for cache[1][2] = 0
  3. When we try to index cache[2][1], we go to the memory block that cache[2] points, which is y1, and then we increase its pointer by one, hence we have y1+offset(1), which from 2. is 0.

However, when we initiated a new table by writing {1,1}, we created a new location

w | w[1] | w[2]

so that: 1. cache[1] = {1,1} means that cache[1] now points to w. 2. Indexing cache[1][1] => w + offset(1) === w[1], which is 1. 3. If we change the value of cache[2][1], we only change y1[1], so cache[2][1] = the new value, but since w[1] is unchanged (w is at a different location), cache[1][1] is still 1.

permanent link | flag offensive
answered 2010-10-07 23:08:18.693203
41
add comment
0


ya.... ok......... so how do i get my script to work? http://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value is this relevant?

k got it

function deepcopy(object)
    local lookup_table = {}
    local function _copy(object)
        if type(object) ~= "table" then
            return object
        elseif lookup_table[object] then
            return lookup_table[object]
        end
        local new_table = {}
        lookup_table[object] = new_table
        for index, value in pairs(object) do
            new_table[_copy(index)] = _copy(value)
        end
        return setmetatable(new_table, getmetatable(object))
    end
    return _copy(object)
end

cache={}
for i=0,9 do
    cache[i]={}
    for j=1,32 do
        cache[i][j]={0,0}
    end
end

addhook("always","omg")
function omg()
    for i=0,8 do
        cache[9-i]=deepcopy(cache[8-i])
    end
    for i=1,32 do
        cache[0][i][1],cache[0][i][2]=player(i,"x"),player(i,"y")
    end
end
permanent link | flag offensive
answered 2010-10-08 00:37:52.510267
1
add comment
0


Hi Lee,

I was wondering the following thing:

i have a function in python, that is supposed to change the content of a list.

def edit_table_reference(t):
        t2 = ['hello','people']
        t = t2

a= ['hi','there']
edit_table_reference(a)
#output: a will not be changed, and still contains ['hi','there']!.

I can do stuff like t.append() or t[0] = 'bll' but i cannot change the reference of t?

permanent link | flag offensive
answered 2010-10-08 22:32:37.838901
46
add comment
0


@eTarPu: References depend on the scope. Let's look at the following example:

a = [0, 0]
def pass_reference(b):
    global a #We now have a as a reference to a global variable.
    b = a

def pass_reference2(b):
    global a
    a = b

b = [1, 1]

pass_reference(b)  #=> a = [0, 0], b = [1, 1]
pass_reference2[b] #=> a = b = [1, 1]

In python, dereferences and indirection are automated based on a set of assumptions (Oh god, not C again T_T). Whenever we pass an object into a function, we're really passing in a reference to that function. However, python creates a new stack whenever a function is called so a second reference is created in order to envelope the first reference (since references are automatically dereferenced when evaluated, double references are transparent.)

--------------------------------------- Global stack
|b| |a| | | | | | | | | | | | | | | | |
--------------------------------------- pass_reference(b) stack
|b = &Global.b| |a =Global.a| | | | | |
---------------------------------------

When we call passreference(b), the following happens: Assuming that we shorten the stack name of passreference to PR

PR.b = &Global.b
PR.b = &Global.a

Global.a is unchanged because Global.b is never on the left hand side.

When we call pass_reference2(b), the following happens:

PR.b = &Global.b
PR.a = Global.a
PR.a = PR.b => Global.a = &Global.b

Since Global.a is now a LHS value, a is now a reference to b.

permanent link | flag offensive
answered 2010-10-09 22:34:38.837850
41
add comment
Your answer:
You are now not logged in but you can answer first and then login
toggle preview



Tags:

× 1

Asked: 1 year, 7 months ago

Seen: 851 times

Last updated: 1 year, 7 months ago

Related questions


Made with Django.