Detecting when a pickable is picked up

Started by b0undarybreaker, August 05, 2017, 08:27:14 AM

Previous topic - Next topic
I have two pickable items named card and gem and I want to be able to tell when they're picked up and make pointers over each item disappear. I'm using this script to do that:
Code (lua) Select
function map:on_update()
  if not map.card:exists() then
    cardpoint:remove()
    print("Card removed!")
  else if not map.gem:exists() then
    gempoint:remove()
    print("Gem removed!")
  end
  end
end

However, when I do that, it errors Error: In on_update: [string "maps/street_arspace.lua"]:79: attempt to index field 'card' (a nil value), line 79 being if not map.card:exists() then. Any suggestion for what I'm doing wrong?
It also for some reason doesn't work without that extra end there. I'm not sure what's up with that.

For a quick note, this happens if it's just card:exists() as well, so it isn't the map.card that's the issue.

A friend and I looked the code over and realized what was wrong. It was erroring saying card was a nil value without it being map.card, but only after I picked the card up. To fix that, I added in stops for the loop:
Code ( lua) Select
function map:on_update()
  if not card and cardchecked then
    cardpoint:remove()
    print("Card removed!")
    cardchecked = true
  end
  if not gem and gemchecked then
    gempoint:remove()
    print("Gem removed!")
    gemchecked = true
  end
end

So the error doesn't spam anymore. However, it won't do the gempoint:remove() still. Do I need to do something to gempoint to make it work properly?

Oh, boy. I think at some point I might need to move this to bug reports. The if not card and cardchecked doesn't remove the pointer, changing it to if not card:exists() and cardchecked both spams the console and doesn't remove the pointer, and I'm just not sure if erroring while trying to index a value for entity:exists() is exactly the best thing because if it can't find the entity then it's doing its job. If there's something huge I'm missing here, please let me know, but I'm just not sure what's going on with this anymore.

Quote from: b0undarybreaker on August 05, 2017, 08:27:14 AM
I have two pickable items named card and gem and I want to be able to tell when they're picked up and make pointers over each item disappear. I'm using this script to do that:
Code (lua) Select
function map:on_update()
  if not map.card:exists() then
    cardpoint:remove()
    print("Card removed!")
  else if not map.gem:exists() then
    gempoint:remove()
    print("Gem removed!")
  end
  end
end

However, when I do that, it errors Error: In on_update: [string "maps/street_arspace.lua"]:79: attempt to index field 'card' (a nil value), line 79 being if not map.card:exists() then. Any suggestion for what I'm doing wrong?
It also for some reason doesn't work without that extra end there. I'm not sure what's up with that.

The reason for the two ends is because you did "else if" with a space on line 5. It should be "elseif" with no space.

Try this instead:
Code (lua) Select
function map:on_update()
  if map.card and not map.card:exists() then
    cardpoint:remove()
    print("Card removed!")
  elseif map.gem and not map.gem:exists() then
    gempoint:remove()
    print("Gem removed!")
  end
end

Ah, right. I've been doing to much GML lately.
Also, I've realized that I've been going about this all wrong. I should be using map:on_obtained_treasure(treasure_item, treasure_variant, treasure_savegame_variable) instead.