Author
Topic: [Solved] Create a custom switch (Read 1028 times)

Hi everyone,I would like to create a custom switch. In fact, I have an idea but I prefer ask if there is a better way to script it.This is my idea: The hero throws a boomerang and activate the switch (which keeps the item). After the switch animation, the boomerang returns to the hero and something happens (for example, opening a door).

I already create a custom entity for the switch and it works when I throw my boomerang. Now, I need to indicate in the game that this custom entity is a switch, because I want to use the function "switch:on_activated" and the other.

switch:on_activated() won't be called by the engine ever because your entity is of type "custom_entity" and not "switch".But in your entity:add_collision_test callback, you can manually call a function of your choice if it exists. You can call it on_activated, just like for built-in switches, or something different.

sol.timer.start(1000, function() sol.audio.play_sound("secret") if entity.on_activated ~= nil then entity:on_activated() end ...[code]As you can see, switches could actually be reimplemented in Lua as custom entities.

--if hero's position is equal to entity's position, then hero is damaged when stepping on entity.function entity:on_activated()local x1,y1 = hero:get_position()local x2,y2 = entity:get_position()print("Entity: "..x2..","..y2)print("Hero: "..x1..","..y1) if x1 == x2 and y1 == y2 then sol.audio.play_sound("secret") hero:start_hurt(240,277,6) endendAlthough, it will be hard to get the hero exactly on the point without doing some manual range calculations.

--if hero's position is equal to entity's position, then hero is damaged when stepping on entity.function entity:on_activated()local x1,y1 = hero:get_position()print("Hero: "..x1..","..y1) if (x1 > 240 and x1 < 256) and (y1 > 272 and y1 < 288) then sol.audio.play_sound("secret") hero:start_hurt(240,277,6) endendThe easiest way to do this is by using the overlap function.