Custom Entity: Ally Creation issue

Started by polyglot762, July 22, 2015, 12:55:13 AM

Previous topic - Next topic
July 22, 2015, 12:55:13 AM Last Edit: July 22, 2015, 01:15:20 AM by Neovyse
Hi all,
I have been working on implementing code like this into my solarus game
though it works and the companions spawn I am having trouble getting
the companions to spawn near the hero.
The typical methods such as altering the x and y have not worked.
Any suggestions?

Code sample attached.
Code (lua) Select
  local hero = self:get_map():get_entity("hero")
  local x, y, layer = hero:get_position()
  local direction = hero:get_direction()

  if game:get_value("a1000") and game:get_value("a1006")
  then
  ally1 = map:create_custom_entity{
      name = "a_butterfly",
      x = x,
      y = x,
      layer = layer,
      direction = direction,
      sprite = "allies/a_butterfly",
      model = "ally_butterfly"
    }
  self.created = true
  local ally1 = true
  local allynumber = 1
  end

You have a mistake in your code. Write "y = y" instead of "y = x".
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

 :o Yes... yes. I do...  :-\ gonna go fix it. thanks.

Fixed the issue with it... it just needed this at the bottom to get it to display correctly.

ally2:set_position(x, y, layer)


Hi all,
I am having a new problem with the scripting of my custom_entities and enemies.
I have been attempting to get my custom entity to detect enemies when they are close... however...
I keep getting "Attempt to Index global 'enemy' (a nil value)" error.
I have put the same code into an enemy script but am still having trouble.

With custom_entities is there a unique syntax?
How do I get enemy:get_position() to function?
Any ideas? Thanks for the comments and help.


The code below are my present attempts...
  local enemy_x, enemy_y, enemy_layer = enemy:get_position()
  local enemy = entity:get_map():get_entity("enemy")


July 28, 2015, 11:03:11 PM #5 Last Edit: July 28, 2015, 11:09:14 PM by Diarandor
The error message you get means that the variable "enemy" is not defined in the code (before the line of the error), so it is equal to nil.

Also, if you are working on an enemy or custom entity script, don't forget to write "entity = ..." (or "whatever = ...") in the first line, so that the entity you are defining is referenced by that "whatever" variable. Maybe that is the problem, but I don't know since I don't know the rest of the script or what you are doing.

You should start looking similar scripts of enemies and custom entities made by other people and modify them.
"If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you."

Based on just those two lines you posted, you're creating your enemy variable after you're referencing it, so that  could be your problem. Hard to tell without more of the code though.

local enemy = entity:get_map():get_entity("enemy")
local enemy_x, enemy_y, enemy_layer = enemy:get_position()