Solarus-Games English Forum

Solarus => Development => Topic started by: YoshiMario2000 on September 14, 2016, 12:29:34 AM

Title: Help with custom entity code.
Post by: YoshiMario2000 on September 14, 2016, 12:29:34 AM
So, what I want to do is create a custom method for a custom entity in it's code.
But, The way I was attempting to do it, proved that I wasn't able to do it how I wanted to.

It apears that I can't just call the method from outside the code.

Code of the custom entity.
Code (lua) Select
local entity = ...
local game = entity:get_game()
local map = entity:get_map()
local sprites = entity:get_sprites()
local val = game:get_value("RED_BLOCKADE") or true

function entity:on_created()
  self:bring_to_back()
  self:set_traversable_by(false)
end

function entity:RED_on_activated()
  if val then
    self:set_traversable_by(true)
    sprites:set_animation("lowering", "lowered")
    val = false
  else
    self:set_traversable_by(false)
    sprites:set_animation("raising", "raised")
    val = true
  end
end


Code of Manager for custom entity.
Code (lua) Select
-- use blockademanager = require("scripts/blockades")
-- and then run blockademanager:swap_color_m(specifiedcolor, map)


local blockademanager = {}

function blockademanager:swap_color_m(color, map)
  local game = map:get_game()
  if color == nil then
    error("No value specified in the colorswap function. Please specify.")
  elseif color:upper() == "RED" then
    if game:get_value("RED_BLOCKADE") == nil then
      game:set_value("RED_BLOCKADE", true)
    else
      game:set_value("RED_BLOCKADE", not game:get_value("RED_BLOCKADE"))
    end
    print(game:get_value("RED_BLOCKADE"))
    if map:get_entities_count("RED_BOCKADE_") >= 1 then
      for i in map:get_entities(map:get_entities_count("RED_BOCKADE_")) do
        i:RED_on_activated()
      end
    end
  else
    error("Value on the BLOCKADE colormode switch function given an invalid value. Doing nothing.")
  end
end

return blockademanager


And if your wondering why I don't just put the code into the library. It is because I have two variants of each color. Goal is to replace the built-in system of crystals and blocks.

The sprites are attached below.
Title: Re: Help with custom entity code.
Post by: Diarandor on September 14, 2016, 01:00:33 AM
Hi! I guess this will not solve your problem, but in your "Code of Manager" script, in line 19 you wrote:
Code (Lua) Select
for i in map:get_entities(map:get_entities_count("RED_BOCKADE_")) do
which makes no sense because "map:get_entities_count("RED_BOCKADE_")" is an integer.
Title: Re: Help with custom entity code.
Post by: YoshiMario2000 on September 14, 2016, 01:24:18 AM
Oh, well... I didn't realize that.
Guess I should replace that with something that calls that index number of the enitity. Thanks!