Haunted code, paranormal activities

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
pacman
Citizen
Posts: 81
Joined: Thu Mar 14, 2013 4:10 pm

Haunted code, paranormal activities

Post by pacman »

I want to look for slope tiles around the player and highlight the closest one.
Everything (kinda) works when I check for slopes once - when the origin point is on the left side of the player.
Left side should detect left-side slopes, and right side of the player looks for right-sided slopes.

But when I add similar code to also check right side peculiar things are happening.
https://dl.dropboxusercontent.com/u/876747/ax.png
Here you can see that slope detection works only for a fragment of slope.
Blue number is l_tile4['distance']. When it doesn't work it changes to 6664 - why?
In my code there is NOTHING that could make this variable go for 6664 :(

All that black magic happens in player.lua
Obviously there is no variable = 6664, so what else could change my variable?
Attachments
Square.love
(14.19 KiB) Downloaded 99 times
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Haunted code, paranormal activities

Post by jjmafiae »

I blame obama.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Haunted code, paranormal activities

Post by davisdude »

Side note:
Unless you're way over-thinking your map, it should be current_map[tile_y][tile_x] on line 159 of main.lua.
And if I understand correctly, in your player:update( dt ) (which is really, too long, I recommend breaking it into many smaller functions), shouldn't you be checking (current_map[l_tile4['y']][l_tile4['x']]['d'])?
I also did some quick debugging and found that the value change is cause by this line:

Code: Select all

if r_tile1['d'] == 'r' then r_tile1['distance'] = math.abs((r_tile1['x'] * tile_size) - p2[1])
else						r_tile1['distance'] = 6661 end
Before this line the value is 4,666. After that line the value is 6661.

I have discovered the source of the error:
Both r_tile1 and l_tile4 have the same x and y values, and as a result, share the same tile coordinate. Since you store the distance as a value on the grid (tile = current_map[tile_y][tile_x] = r_tile1 = l_tile4), they value is overwritten from that.
jjmafiae wrote:I blame obama.
Obama will find a way to blame it on Bush.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
jjmafiae
Party member
Posts: 1331
Joined: Tue Jul 24, 2012 8:22 am

Re: Haunted code, paranormal activities

Post by jjmafiae »

Davisdude wrote:Obama will find a way to blame it on Bush.
Bush will find a way to blame Clinton.
User avatar
pacman
Citizen
Posts: 81
Joined: Thu Mar 14, 2013 4:10 pm

Re: Haunted code, paranormal activities

Post by pacman »

davisdude wrote:I have discovered the source of the error:
Both r_tile1 and l_tile4 have the same x and y values, and as a result, share the same tile coordinate. Since you store the distance as a value on the grid (tile = current_map[tile_y][tile_x] = r_tile1 = l_tile4), they value is overwritten from that.
That's why when I changed these two variables to "SOMETHING" and "DIFFERENT" it still didn't work.
Changing ['distance'] in one variable to ['distance2'] worked...
I must sit down and think how to not make mistakes that look like error in matrix ; _ ;

current_map[tile_y][tile_x] - starting with Y? That's uncomfortable :o
Map isn't something I put thought in. Just wanted some tiles without using any available library.

You make my hearth calm :oops:

Thanks Obama!

Edit. I debugged with the use of love.filesystem (running stuff in console on Linux/Mac OS is easier and faster than on Win *sigh*) and here the value of l_tile4['distance'] is changed after r_tile4['distance']

Edit2. And if these two variables will "get_tile" the same tile... wouldn't it be independent value?

Code: Select all

var1 = get_tile[1][1]
var2 = get_tile[1][1]
var1['distance'] = 10
var2['distance'] = 20
var1 has nothing to do with var2... But it's not true apparently (???)
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

Re: Haunted code, paranormal activities

Post by davisdude »

pacman wrote:Edit2. And if these two variables will "get_tile" the same tile... wouldn't it be independent value?

Code: Select all

var1 = get_tile[1][1]
var2 = get_tile[1][1]
var1['distance'] = 10
var2['distance'] = 20
var1 has nothing to do with var2... But it's not true apparently (???)
You would think so, but what you really are doing is making something like this:

Code: Select all

var1 = get_tile[1][1]
var2 = get_tile[1][1]
var1['distance'] = 10
var2['distance'] = 20

-- This is the same as saying:
get_tile[1][1]['distance'] = 10
get_tile[1][1]['distance'] = 20
So what you need to do instead is make a new table to store the values, and not something attached to the map values.

Recommended Reading- especially the part titled "Table values are references"
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
pacman
Citizen
Posts: 81
Joined: Thu Mar 14, 2013 4:10 pm

Re: Haunted code, paranormal activities

Post by pacman »

I understand... So I need to copy what I want to other table.
Now in http://lua-users.org/wiki/CopyTable there are 2 different ways to copy table.
Shallow Copy is easy and simple but Deep Copy requires some knowledge to understand :roll:

Code: Select all

function deepcopy(orig)
    local copy
    copy = {}
    for orig_key, orig_value in next, orig, nil do
        copy[deepcopy(orig_key)] = deepcopy(orig_value)
    end
    setmetatable(copy, deepcopy(getmetatable(orig)))
    return copy
end
What is "next"? Why is there "orig, nil" after that?
It uses function deepcopy inside of deepcopy - sounds like Never ending story!
Does "setmetatable" is necessary if I don't want copy metatables?
Can not into tables :cry:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Haunted code, paranormal activities

Post by bartbes »

pacman wrote:What is "next"?
A function from the standard library, of course!
pacman wrote:Why is there "orig, nil" after that?
Due to the way the for loop works in lua, the function 'next' will get called with the arguments 'orig' and 'nil' the first time, then with 'orig' and its first return value after, until it returns nil.
pacman wrote: It uses function deepcopy inside of deepcopy - sounds like Never ending story!
But you're only ever calling deepcopy on less than you started with, so it has to end at some point. (Though from the looks of it, cyclical structures would make it mess up badly, so this deepcopy function is not flawless.)
pacman wrote: Does "setmetatable" is necessary if I don't want copy metatables?
No, the metatables are copied, so they need to be accessed, that's why the calls are there.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Haunted code, paranormal activities

Post by Robin »

pacman wrote:What is "next"? Why is there "orig, nil" after that?
This is just a case of premature optimization, it's just what pairs(orig) returns:

Code: Select all

$ lua
Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
> orig = {}
> return next, orig, nil
function: 0x418a60	table: 0x17f1360	nil
> return pairs(orig)
function: 0x418a60	table: 0x17f1360	nil
pacman wrote:It uses function deepcopy inside of deepcopy - sounds like Never ending story!
Yes, this version only works with tables without cycles (tables that don't contain themselves, directly or indirectly, and don't contain any other tables with cycles). This is a version that does always end:

Code: Select all

function deepcopy(t, cache)
    if type(t) ~= 'table' then
        return t
    end

    cache = cache or {}
    if cache[t] then
        return cache[t]
    end

    local new = {}

    cache[t] = new

    for key, value in pairs(t) do
        new[deepcopy(key, cache)] = deepcopy(value, cache)
    end

    return new
end
Help us help you: attach a .love.
User avatar
pacman
Citizen
Posts: 81
Joined: Thu Mar 14, 2013 4:10 pm

Re: Haunted code, paranormal activities

Post by pacman »

Thank you all for explanations :nyu:
Everything is clear :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest