GML Object inheritance challenge...

Discussion in 'Programming' started by ajrdesign, Jan 16, 2020.

  1. ajrdesign

    ajrdesign Member

    Jun 29, 2019
    So I'm looking for advice on a good way to go about this challenge: I currently have a game where there's shields that should be able to destroy both projectiles (One object set with parent/children) and some ships (Another object set with parent/children). I DON'T want ALL projectiles to be destroyed by shields or ALL ships to be destroyed by shields.

    Right now I basically have shield looping through all these objects in an array and seeing if it's colliding with any of them. But it requires I add new objects to the arrays whenever I make them, which is pretty often.

    In my head I'd like to do something like this:

    If that worked I could just check the Shield Collision Obj to see if it has a collision with any shields and I would never have to worry about forgetting to add a object to an array. But because an object cannot have two parents I don't see a way for this to work...

    What I have works fine... for now. But it feels unsustainable. So my question is how would you approach this problem in GML?
  2. samspade

    samspade Member

    Feb 26, 2017
    There are a couple ways to do something like this.
    • Create a parent for both projectile and ship and give it a variable set to determine whether it can be destroyed
    • Create and maintain your own lists and iterate through them
    The first one is probably the easiest. It works within the existing system and doesn't require much set up. If there is a downside it is that you have to have the variable in a bunch of objects that might not generally need it or you'll get an error when you loop through them checking that variable. Still, this the method I use in a similar project.

    The second one sounds like what you already have. Essentially maintain a list of the objects and loop through that. This is actually the most efficient solution; however, it also requires the most work as you must implement a system for adding and removing from that list that works 100% of the time in all cases and under all circumstances. And then you have to maintain that system. This isn't as hard as it sounds (at least depending upon the game) as you could create a custom script for creating these objects which adds their id to the list and only ever use that script to create the objects and then only destroy the objects by another custom script that removes them from the same list. But you have to police yourself on it.
    ajrdesign likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice