Author Topic: [Solved] Ignoring entity prefix ?  (Read 3223 times)

0 Members and 1 Guest are viewing this topic.

MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
[Solved] Ignoring entity prefix ?
« on: November 10, 2015, 12:58:32 PM »
Hi

I was wondering if this is possible to "ignore" the prefix of a copied entity ?
For example : I do have finished 100% my chest script and the treasure received is based on the entity name on the map, trouble is, if I copy this same entity, there would be a suffix "_x" where x is a number that increase if the same entity has been pasted.

But, as you may read, if I go get the treasure, let's say I do named the treasure "small_key", and open one of it's copy "_2_small_key", the game cannot recognize the item _2_small_key since it read the entire entity:get_name().

I do have see that Solarus have an event to parse all prefixes (http://www.solarus-games.org/doc/latest/lua_api_map.html#lua_api_map_get_entity) and I know how to retrieve the X value (the pool enigna from MoS helped in some way), but I don't really know how to ignore the prefix itself.

any help would be apreciated

This was my attempt in on_interaction(), but ended with "string expected, got nil", name is declared as local at the start of the script


 
Code: (lua) [Select]
for entity in map:get_entities("^_([1-9])_$" ..self:get_name()) do
   if entity == find(self:get_name()) then
    name = self:get_name()
   end
  end

also tried this, same thing occurs

Code: (lua) [Select]
  for entity in map:get_entities() do
   if map:get_entities():match("^_([1-9])_$" ..self:get_name()) then
    name = self:get_name()
   end
  end
« Last Edit: November 10, 2015, 01:57:40 PM by Username »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1194
    • View Profile
Re: Ignoring entity prefix ?
« Reply #1 on: November 10, 2015, 01:25:27 PM »
I don't understand. You want to remove the numbered suffix of a string?

Code: [Select]
local name_without_suffix = name_with_suffix:match("^(.*)_[0-9]+$")
if name_without_suffix == nil then
  -- No match. It means that there was actually no suffix.
  name_without_suffix = name_with_suffix
end

Or this shorter and more idiomatic way, completely equivalent:
Code: [Select]
local name_without_suffix = name_with_suffix:match("^(.*)_[0-9]+$") or name_with_suffix


MetalZelda

  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: Ignoring entity prefix ?
« Reply #2 on: November 10, 2015, 01:45:08 PM »
I don't understand. You want to remove the numbered suffix of a string?

Code: [Select]
local name_without_suffix = name_with_suffix:match("^(.*)_[0-9]+$")
if name_without_suffix == nil then
  -- No match. It means that there was actually no suffix.
  name_without_suffix = name_with_suffix
end

Or this shorter and more idiomatic way, completely equivalent:
Code: [Select]
local name_without_suffix = name_with_suffix:match("^(.*)_[0-9]+$") or name_with_suffix

I want to grab the entity that have a prefix and make the code to ignore that prefix, since the script I use need to retrieve the item name from the map entity name, so I need to pass through map:get_entities(), but, when it is about to give the treasure, the game doesn't start the treasure and input  "string expected, got nil", which is normal, since there is no item _x_"item" in the database, even if the entity have a name, and start_treasure have "name" (the local) in value.
Gonna try your method.

Edit : Great, it works like a charm, thanks Christopho. :)

Code: (lua) [Select]
local name_without_suffix = self:get_name():match("^(.*)_[0-9]+$") or self:get_name()
hero:start_treasure(name_without_suffix)
« Last Edit: November 10, 2015, 01:57:59 PM by Username »