General question about how to structure parents / children

I used to make small online games using Macromedia Director and the scripting language Lingo about two decades ago. Now I'm back trying to make a game for the Playdate and I've made some progress but I need to get some pointers about how to think about this in general.

How to structure a powerbar?
I'm making a game where different cars will have different characteristics. One of them number of gears. I want to illustrate which gear is active by having a power meter withthree to five distinct lights that turn on or off.

After a lot of trial and error I managed to set up one Sprite that is called GearIndicator, and that in turn sets up any number of sprites called SingleLight.

The main thread can call on GearIndicator:changeGear(WhatGear) and GearIndicator will loop through a table of SingleLights that it sets up on its init function.

Everything works the way I want, but it feels as if I'm trying to do things in the way that I did back in the day, and it feels as if that way isn't what I'm supposed to do things.

I don't like that I can't fin a way to have a specific reference to the GearIndicator but that I rather just call GearIndicator:changeGear(whatGear) in general. Like I said, it works, but it feels messy. What If I want a second indicator for something else that could use a second instance of the GearIndicator?

I would also like to add the instances of SingleLight to the Sprite GearIndicator, so that I move the GearIndicator sprite to a location and the Single Light Sprites are added from (0,0) being the top left of the GearIndicator Sprite, but I couldn't figure out how.

Am I going at things in an unnecessary complicated way? Is there a best practice, and if so, what?

Any ideas pointers would help a lot.

1 Like

I don't know if there is a best practice but I have 3 ways to deal with this.

  • If there are only a few children, I update each child from the parent sprite's update method:
    melia2

  • If there are many children, I give a reference of the parent sprite to each child and call moveTo to update the location of each child according to its parent.

  • For counters, I usually use only one sprite, create an empty bitmap in the constructor, and draw the sub-sprites in a draw method.

I typically use the first two methods when the sub-sprites can collide with the player sprite. Otherwise, I tend to prefer the third method to keep the number of sprites low.

1 Like

Thank you for the in-depth reply.
I really need to get more familiar with Lua in general.

The 2 firsts code sample I pasted are written in a custom language I created to script sprites in my games and the third one is C code but hopefully you get the idea :sweat_smile:

Oh, hahaha. I thought it looked a bit different from my code, but I also thought it was down to me writing old fashioned code.

Your own script language you say... I definitely need to build some skills.