Author Topic: Detecting when a pickable is picked up  (Read 75 times)

b0undarybreaker

  • Newbie
  • *
  • Posts: 11
    • View Profile
Detecting when a pickable is picked up
« 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
  1. function map:on_update()
  2.   if not map.card:exists() then
  3.     cardpoint:remove()
  4.     print("Card removed!")
  5.   else if not map.gem:exists() then
  6.     gempoint:remove()
  7.     print("Gem removed!")
  8.   end
  9.   end
  10. 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.

b0undarybreaker

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Detecting when a pickable is picked up
« Reply #1 on: August 05, 2017, 10:44:52 am »
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.

b0undarybreaker

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Detecting when a pickable is picked up
« Reply #2 on: August 05, 2017, 11:00:01 am »
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
  1. function map:on_update()
  2.   if not card and cardchecked then
  3.     cardpoint:remove()
  4.     print("Card removed!")
  5.     cardchecked = true
  6.   end
  7.   if not gem and gemchecked then
  8.     gempoint:remove()
  9.     print("Gem removed!")
  10.     gemchecked = true
  11.   end
  12. 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?

b0undarybreaker

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Detecting when a pickable is picked up
« Reply #3 on: August 05, 2017, 11:07:27 am »
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.

llamazing

  • Jr. Member
  • **
  • Posts: 71
    • View Profile
Re: Detecting when a pickable is picked up
« Reply #4 on: August 05, 2017, 02:45:18 pm »
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
  1. function map:on_update()
  2.   if not map.card:exists() then
  3.     cardpoint:remove()
  4.     print("Card removed!")
  5.   else if not map.gem:exists() then
  6.     gempoint:remove()
  7.     print("Gem removed!")
  8.   end
  9.   end
  10. 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
  1. function map:on_update()
  2.   if map.card and not map.card:exists() then
  3.     cardpoint:remove()
  4.     print("Card removed!")
  5.   elseif map.gem and not map.gem:exists() then
  6.     gempoint:remove()
  7.     print("Gem removed!")
  8.   end
  9. end

b0undarybreaker

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Detecting when a pickable is picked up
« Reply #5 on: August 06, 2017, 09:11:13 am »
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.