Okay, I want an entity to be disabled when a bomb is placed on it. I am guessing that it is not working because the bomb is not created before the start of the map.
--bomb test
sol.timer.start(1000, function()
if bomb_1:overlaps(test) then
test:set_enabled(false)
end
return true -- To call the timer again (with the same delay).
end)
Error: In on_started: [string "maps/soulia_forest/soulia_forest.lua"]:57: attempt to index local 'bomb_1' (a nil value)
Yeah, I wouldn't put this test in map:on_started() necessarily. Is this something that only happens in this map? If so, maybe having a timer check over and over works, but it might make more sense to run the check code whenever you place a bomb.
Either way, what I would do is run some code that checks to see if both entities bomb and test both exist before trying to disable one. Try the method map:has_entities(), and if you get nil back, don't do the code.
If map:has_entitiy("bomb_1") and bomb_1:overlaps(test) then
...
I actually tried map:has_entities() and creating the bomb beforehand. I thought it was the timer, but that was not the case after some more testing.
local x, y, layer = hero:get_position()
local direction = hero:get_direction()
if direction == 0 then
x = x + 16
elseif direction == 1 then
y = y - 16
elseif direction == 2 then
x = x - 16
elseif direction == 3 then
y = y + 16
end
map:create_bomb{
x = x,
y = y,
layer = layer
}
--bomb test
sol.timer.start(1000, function()
if map:has_entitiy("bomb_1") and bomb_1:overlaps(test) then
test:set_enabled(false)
end
return true
end)
Error: In on_started: [string "maps/soulia_forest/soulia_forest.lua"]:80: attempt to call method 'has_entitiy' (a nil value)
I also tried:
sol.timer.start(1000, function()
for bomb in map:get_entities("bomb") do
if bomb_1:overlaps(test) then
test:set_enabled(false)
end
end
return true
end)
As well as...
sol.timer.start(1000, function()
if map:has_entities("bomb") and bomb_1:overlaps(test) then
test:set_enabled(false)
end
return true
end)
Oh, this is a little hilarious. I forgot to add a name to the created entity. It works now.
local x, y, layer = hero:get_position()
local direction = hero:get_direction()
if direction == 0 then
x = x + 16
elseif direction == 1 then
y = y - 16
elseif direction == 2 then
x = x - 16
elseif direction == 3 then
y = y + 16
end
map:create_bomb{
name = "bomb",
x = x,
y = y,
layer = layer
}
--bomb test
sol.timer.start(1000, function()
if map:has_entities("bomb") and bomb:overlaps(test) then
test:set_enabled(false)
end
return true
end)
An easy way to do this is to allow to define a custom event map.on_bomb_created(bomb_entity). You can call it, if it exists, from the bomb metatable (just define the function bomb_metatable.on_created).