Author Topic: [i]SOLVED: [Unknown Error: Testing for Ropes to activate chest][/i]  (Read 2077 times)

gmanplays@gmail.com

  • Newbie
  • *
  • Posts: 24
    • View Profile
Hey everyone, back after tiling a good portion of an overworld, and the entirety of my first dungeon, when I ran into a problem while I was making my project. I was attempting to create a simple script which enabled a chest to appear when all of the Ropes in the room were killed, until I got to the point through trial and error where I'd created a script I couldn't possibly refine anymore, but nonetheless failed to generate the desired result. Instead, it played the sound which indicates the appearance of a chest once the room was loaded. Consider the attached script. What do you think went wrong? Did I refer to the wrong prefix in the has_entities command? I'm not sure. Any help would be appreciated

Post-Script: Hey, I'm not sure where I'd put this, I didn't see a sub-forum that was marked "Help", although I'm notorious for overlooking obvious things. If the mods would move this post to that potentially extant topic-section, I wouldn't be mad.
« Last Edit: February 11, 2019, 05:27:45 am by gmanplays@gmail.com »

YoshiMario2000

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Re: Unknown Error: Testing for Ropes to activate chest
« Reply #1 on: February 08, 2019, 05:07:25 am »
Rather than doing a repeat command and wait until,
you should preform a check on each enemy to check if all the enemies are defeated when they are slain.
like so:
Code: (lua) [Select]
function map:on_started()
  -- Find all the entities named "rope" and define their on_dead() function.
  for rope in map:get_entities("rope") do

    -- Another method of defining a function, One that I like using for dynamically handled code.
    rope.on_dead = function(rope) -- Because the function isn't defined like the other functions here, we need to set the first variable to be the object that this event is being called on.

      if rope:get_map():get_entities_count("rope") <= 0 then
        -- Do things here for making the chest appear, the local variable map will need to be defined inside this function.
        local map = rope:get_map()
        -- map:get_entity("rope_chest"):enable()
        sol.audio.play_sound("chest_appears")
      end

    end

  end

end
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?

gmanplays@gmail.com

  • Newbie
  • *
  • Posts: 24
    • View Profile
Re: Unknown Error: Testing for Ropes to activate chest
« Reply #2 on: February 08, 2019, 11:00:32 pm »
Excellently done! Thank you very much. I'm new to coding, so what do you think went wrong with my loop?

llamazing

  • Full Member
  • ***
  • Posts: 198
    • View Profile
Re: Unknown Error: Testing for Ropes to activate chest
« Reply #3 on: February 08, 2019, 11:59:18 pm »
That is not the correct way to write a loop at all. It's just constantly checking map:has_entities("rope") over and over again in an endless loop that won't allow for anything else to happen in the mean time. It's doing way more processing than it needs to and wil hinder the rest of the game from being able to run properly.

Compare it to Yoshi's method that counts the number of entities present once any time one of them dies. Until one of them dies, the number of them isn't going to change, so no need to be checking it over and over again in between. See how this method is vastly better?

YoshiMario2000

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Re: Unknown Error: Testing for Ropes to activate chest
« Reply #4 on: February 09, 2019, 05:01:42 am »
I think it's also worth noting that technically, the number of ropes can be changed via other methods that don't involve them being killed, but these are cases in which you'd be doing something in your code and not something that the engine would do on it's own.

I would also advise that the chest activation script for these ropes use a save game variable to check if this challenge has been done before, and set the map state to reflect this. The map won't automatically do this for you on it's own. You'll have to remove the enemies under map:on_started() and enable the chest again.

The method "entity:remove()" won't call the the "enemy:on_dying()" and "enemy:on_dead()" events, so you can safely remove them from the map without code accidentally being executed.
This signature was way too long before, but now it's short!
Also, I am Still Alive!
On ad Off I go!

Do you ever get the feeling that the fandom of a product(s) ruin the potential that you could have had to enjoy the product?