Solarus-Games English Forum

Solarus => Development => Topic started by: polyglot762 on July 22, 2015, 12:55:13 AM

Title: Custom Entity: Ally Creation issue
Post by: polyglot762 on July 22, 2015, 12:55:13 AM
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
Title: Re: Custom Entity: Ally Creation issue
Post by: Diarandor on July 22, 2015, 03:22:36 AM
You have a mistake in your code. Write "y = y" instead of "y = x".
Title: Re: Custom Entity: Ally Creation issue
Post by: polyglot762 on July 24, 2015, 01:09:34 AM
 :o Yes... yes. I do...  :-\ gonna go fix it. thanks.
Title: Re: Custom Entity: Ally Creation issue
Post by: polyglot762 on July 28, 2015, 09:06:22 PM
Fixed the issue with it... it just needed this at the bottom to get it to display correctly.

ally2:set_position(x, y, layer)

Title: Re: Custom Entity: Ally Creation issue
Post by: polyglot762 on July 28, 2015, 09:13:44 PM
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")

Title: Re: Custom Entity: Ally Creation issue
Post by: Diarandor on July 28, 2015, 11:03:11 PM
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.
Title: Re: Custom Entity: Ally Creation issue
Post by: wizard_wizzle (aka ZeldaHistorian) on July 29, 2015, 12:05:51 AM
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()