Door system does not work (engine bug?)

Started by the bread, June 19, 2015, 09:56:07 PM

Previous topic - Next topic
June 19, 2015, 09:56:07 PM Last Edit: June 20, 2015, 06:54:01 PM by the bread
Today I tried implementing a door system with a door that opens automatically when you walk in front of it.
I'm using this piece of code:
local map = ...

function map:on_started()
  self:enable_doors()
end

function map:enable_doors()
  for door in map:get_entities("house_door") do
    door:set_enabled(false)
    local sensor = map:get_entity(door:get_name() .. "_opening_sensor");
    function sensor:on_activated()
      sol.audio.play_sound("door_open");
    end
    function sensor:on_left()
      door:set_enabled(true);
      sol.audio.play_sound("door_closed");
    end
  end
end


The names have to be something like "house_door1" and "house_door1_opening_sensor".

When I tried it, it worked fine at first.
But then things went really weird. I wasn't able to figure out a scheme yet, it's just that it does work sometimes and sometimes it doesn't.
Some things I have already noticed:
1. Even changes that should have nothing to do with a door and a sensor, like adding some tiles elsewhere can stop the correct opening of the door.
2. It makes a difference whether I run the quest by opening the "solarus_run.exe" or whether I run it directly from the quest editor. It's already happened that the exactly same quest worked fine on the one and it didn't open the door on the other.
3. Additionally to point 2, another difference between running the quest from the editor and manually executing it is, that the latter returns this error in the "error.txt" file
Error: In on_started: [string "maps/somewhere.lua"]:10: attempt to index local 'sensor' (a nil value)
On the other hand, the running-from-editor version doesn't return an error and in fact, it never ever has.
EDIT: I just noticed that the running-from-editor version is returning the same errors in the "error.txt" file as well. It's just that it doesn't use the "error.txt" file in the same path of the quest but the "error.txt" file in the path of the solarus engine.

So this bug appears to be a very crazy thing that is confusing me very much at this very moment ::). Perhaps I have simply made a normal mistake in my code but I suppose that it is some kind of bug of the editor. Any help is VERY appreciated.
Thank you in advance :)

I don't see a mistake in the code, I will have to check this.
But what I can say right now is that running the quest from the editor is experimental and there are some bugs.

Just out of curiosity, can you try with map:open_doors()/map:close_doors() instead?

I have forgotten to mention that I'm not using a door entity but a dynamic tile as a door. The map:open_doors() function isn't ought to work on dynamic tiles as well, is it? Anyway, it doesn't. Because when I add this line
map:open_doors("house_door");
to the map:on_started() function, it doesn't change anything. So I can't switch the code to map:open_doors()/map:close_doors()
By the way, have you figured out a reason why this problem occurs? Right now, it does not work in both ways of execution...

June 27, 2015, 05:45:03 PM #3 Last Edit: June 27, 2015, 06:20:17 PM by froggy77
- It works with entities.
- Create two doors: "yourmapid_auto_door_a_1" (not "yourmapid_auto_door_a", you have to add "_1") and "yourmapid_auto_door_a_2"
Create one sensor: "yourmapid_sensor_a" under your two doors (like in the image)
"yourmapid" is your map ID (e.g. "library", "house", "dungeon" or what you like)

e.g.: "house_auto_door_a_1" and "house_auto_door_a_2" and the opening sensor "house_sensor_a"
e.g.: "house_auto_door_b_1" and "house_auto_door_b_2" and the opening sensor "house_sensor_b" for a 'second pair of doors'
e.g.: "house_auto_door_b_3" and "house_auto_door_b_4" but these doors will be opened with the same sensor  "house_sensor_b"
It's a, b, c or object, lol, what you like,  it should work:
e.g.: "house_auto_door_lol_1" and "house_auto_door_lol_2" and the opening sensor "house_sensor_lol".

local map = ...

function map:on_started()

  self:auto_doors()
end

function map:auto_doors()

  for door in map:get_entities(map:get_id() .. "_auto_door") do
    local door = string.sub(door:get_name(), 0, -3)
    local sensor = map:get_entity(string.gsub(door, "auto_door", "sensor"))
    function sensor:on_activated()
  map:open_doors(door)
    end
    function sensor:on_left()
  map:close_doors(door)
    end
  end
end

I think there is a problem with the name of your entities:

  for door in map:get_entities("house_door") do
    door:set_enabled(false)
    local sensor = map:get_entity(door:get_name() .. "_opening_sensor");


If I understand correctly, the sensor's name starts with "house_door". So it gets also disabled by this loop!

To fix this issue, you can either give different names or add a test: if door:get_type() == "door" then