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?
Haunted code, paranormal activities
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Haunted code, paranormal activities
- Attachments
-
- Square.love
- (14.19 KiB) Downloaded 99 times
Re: Haunted code, paranormal activities
I blame obama.
Re: Haunted code, paranormal activities
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:
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.
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
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.
Obama will find a way to blame it on Bush.jjmafiae wrote:I blame obama.
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
Re: Haunted code, paranormal activities
Bush will find a way to blame Clinton.Davisdude wrote:Obama will find a way to blame it on Bush.
Re: Haunted code, paranormal activities
That's why when I changed these two variables to "SOMETHING" and "DIFFERENT" it still didn't work.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.
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
Map isn't something I put thought in. Just wanted some tiles without using any available library.
You make my hearth calm
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
Re: Haunted code, paranormal activities
You would think so, but what you really are doing is making something like this:pacman wrote:Edit2. And if these two variables will "get_tile" the same tile... wouldn't it be independent value?var1 has nothing to do with var2... But it's not true apparently (???)Code: Select all
var1 = get_tile[1][1] var2 = get_tile[1][1] var1['distance'] = 10 var2['distance'] = 20
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
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
Re: Haunted code, paranormal activities
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
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
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
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
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
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Haunted code, paranormal activities
A function from the standard library, of course!pacman wrote:What is "next"?
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:Why is there "orig, nil" after that?
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: It uses function deepcopy inside of deepcopy - sounds like Never ending story!
No, the metatables are copied, so they need to be accessed, that's why the calls are there.pacman wrote: Does "setmetatable" is necessary if I don't want copy metatables?
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Haunted code, paranormal activities
This is just a case of premature optimization, it's just what pairs(orig) returns:pacman wrote:What is "next"? Why is there "orig, nil" after that?
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
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:pacman wrote:It uses function deepcopy inside of deepcopy - sounds like Never ending story!
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.
Re: Haunted code, paranormal activities
Thank you all for explanations
Everything is clear
Everything is clear
Who is online
Users browsing this forum: Google [Bot] and 0 guests