How to toggle visibility for later created sprites inside a family

  • Hello,

    I'm trying to add a debug system to my game.

    To make this easier, I have a family for Helper Sprites

    I create a toggle for the T key. When the T key is pressed, it will toggle the visibility of the sprite.

    However, some sprites are not going to exist when the game starts so when the game creates them later, they will not always sync with the properties of the family, so if the family is suppose to be invisible, some sprites will not go invisible.

    The helper sprite's properties are initially set to invisible.

    Here's a screenshot of the code:

    https://i.imgur.com/muqMAaz.png

    I don't understand what I'm doing wrong here.

  • b_DebugIsVisible is an instance variable. This means that each sprite instance in this family has its own value in b_DebugIsVisible.

    If you want to toggle visibility for all sprites, you need to use a global variable.

  • Hey Dop,

    I could not embed the screenshot of my code on the forum for some reason, you'll have to go to the link. It must have something to do with the major forum upgrade because I initially could not do a lot of things that I wanted to show you and I only just got my avatar back. What a mess. Sorry if my initial request was confusing in any way. I could not edit it after I posted it.

    My understanding of families here are that if you add an object to a family and you add instance variables for that family, then those objects in that family will be impacted by those instance variables that the family has.

    In this case, I set an instance variable to the family, and like the screenshot showed you, it should be toggling all objects in that family to visible or not visible.

    This is fine initially but later on when helper sprites are created (for example, I wanted to toggle the visibility of my pathfinding_destintion_helper sprite for each of my people), but people are spawned later rather than at the start of the layout, and if I have the toggle off some people spawn with it's visibility off and some people spawn with it on. This seems to occur mainly if a person is created in the middle of toggling it so some sprites in the family have their visibility on when the boolean is off, and I can't seem to match the sprite's visibility with the family's visibility later for some reason. I just don't understand what I am doing wrong here.

    If I use a global variable, that means I have to toggle the visibility specific for each sprite instead, and I know I can do that, I just thought it would be easier to do this with families since later on, I will likely add other objects for debugging and my outlook of this would be a simply matter of just adding the object to the family so I don't have to go adding it to the code.

  • You think of a family as of one big object, which is wrong. There is no such thing as "family visibility". When you create an instance variable on a family, what actually happens is you are adding a copy of this variable to each member of the family.

    So each sprite has its own copy of this variable, and each instance of the sprite has its own value in this variable. If you toggle it for existing instances (setting it to true) and then spawn new instances (with default value of false), you end up with a mix of true and false values.

    .

    Like I said, if you want to control visibility of all family instances at once, you need a global variable:

    If bDebugVisibleGlobalVar=1 -> Family_Debug_Sprites set visible
    Else -> Family_Debug_Sprites set invisible
    

    To toggle it between 0 and 1 you can use this expression:

    Set bDebugVisibleGlobalVar to (bDebugVisibleGlobalVar=0)
    
  • Ahh I see what it's doing. I just needed a better method to check it's values and properties later. I thought I could do this with an every tick event for those sprites, but it was not working, I'll try the global variable.

    I didn't think they were one big project. I just thought that any later created objects after run time would inherit the values and properties of the family it belongs to, like in the case I put forward, they would just be set to invisible even if they were visible upon creation. Obviously this is not the case.

    Thank you for your help, again, Dop. I will do what you suggested. It makes sense.

  • When you create new objects in runtime, they inherit properties not from the family, but from that one object instance which you placed on the layout in editor. You know how Construct requires at least one instance of each sprite to be placed on the layout? This instance is used as a reference to create all other instances of this sprite in runtime.

    So, for example, if you define "Health" instance variable on the family Enemies, you can still set different default health for each enemy type.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Thanks again, Dop;

    just a few things, and I am sorry I have more questions, but I am trying to get my head around this because if I do, I believe it will help me out with my overall design.

    1) I just want to declare that even though I have been using C2 for a while and my ideas for games are there. I have had no prior programming experience, and there are still features with C2 that I am yet to fully understand and I am still learning how the logic works, but I do believe I am growing with this every day, so if some of my questions make me seem like a complete dud, you know why. I do not proclaim myself to be experienced with any of this.

    2) To confirm about families; you can define instance variables for multiple objects without having to set it for each object, you just set it to a family. But, say if an instance of an object that is a part of a family takes 'damage' then you can tell C2 to only subtract from the health value of that one instance, it does not necessarily effect every other family member, so later if new objects were added to that family then you do not have to program the 'health system' again of the game for that object, it will just work.

    I have read about families but I'm still short of fully understanding how they work. I will have to go and look it all up again. I have not really been using them for my projects and I feel I probably should.

    3)I was hesitant writing number 2 because as I was writing it, I thought why would you want to create multiple sprite objects when I could use the one object just with different animations to look different or is that bad practice? I feel like I should be using the one object with different animations to make it look different. For example, each of my buildings is just one object with different animations, but if I am to create an enemy building I'm torn between using the one object or create a different object and put it into a family.

    4)(I wanted to ask this privately but it seems like this forum doesn't allow private messaging anymore) Specifically; I wanted to get my head around something because I am using what you suggested, I'll show you here:

    i.imgur.com/GKfdFed.png

    This is definitely so much easier, less code a lot easier to understand, and it works.

    The only thing I do not understand is how that is setting Global_b_DebugFamilyVisible between the values of 0 and 1. Setting something to "(name=0)" seems odd to me. I just don't understand how that means "if you are 0, set to 1, and if you are 1, set to 0."

  • 2) Correct! Technically defining instance variables on families is the same as defining them on individual objects, but with families you can make your code more efficient and compact. If you add Health variable to each of your enemies sprites, you will have to add a bunch of almost identical events:

    If Zombie.Health<=0 then Zombie destroy

    If Vampire.Health<=0 then Vampire destroy

    ...

    If you add them to a family and define Health variable on the family, you can have just one event:

    If Enemies.Health<=0 then Enemies destroy

    .

    3) Yes, you can make one object with multiple animations instead of the family of objects. But what if you need animations to actually animate different states of the sprite (for example zombie running, zombie attacking)?

    Also, the cool thing about families is that you can add one object to several families, and use these families for different purposes!

    For example, you can have Zombie and Vampire in family Enemies, but also have Zombie, Vampire, Chicken and Player in family LivingCreatures. It will allow you to do this:

    Enemies on collision with Player -> Player subtract 1 from health

    LivingCreatures on destroyed -> LivingCreatures spawn BloodSplatter

    .

    4) In programming true is 1 and false is 0.

    So the formula (variable=0) can either return true (1) or false (0). If variable was 0, the result will be 1, and if variable was 1, the result will become 0.

    Here is another compact way to toggle a variable: Set variable to (variable=1 ? 0 : 1)

    This means "if variable=1 then 0, else 1".

    And of course you can do this the long way with two events:

    If variable=0 : Set variable to 1

    If variable=1 : Set variable to 0

  • Thanks for answering with detail, Dop!

    Well, what I have been doing for different types of animations, say for the building, I have folders for each type of building and then the animations inside them, I have an instance variable for the type of building and this is checked from when the building is placed for construction all the way through it's main purpose. It is the type that defines it's appearance and it's purpose. When changing animations for this building, I would simply set it to the appropriate animation while checking it's type.

    Although, buildings don't change in animation as much as people do (for walking and what not), so I do not have to manage it as much, but I have a state variable for people and I control that using events, I simply determine the direction they are facing with state(number). The number being the value of the state variable matching the appropriate animation for the same number. For example state0 faces up, etc. I just don't know if I should implement different objects for other types of units, and define those with a type variable like I do with buildings or not, yet.

    Unfortunately, if I am going to use a family system, which I think I might need to do eventually, it's not that easy to change what I have now, because you can't just replace objects with families. It would take a while for me to go through all of the events to change it physically because there are a lot of events. I guess I could still use the one object with different animations method for buildings and still use families for other things like damage and enemies and stuff like that. I'm just not sure how to go about doing all that at the moment, I have to do some extra planning.

  • It's actually quite easy to "upgrade" objects to a family, I did this many times, here is the tutorial:

    scirra.com/tutorials/535/how-to-upgrade-an-object-to-a-family

  • Perhaps it is a good idea, I will do it. Thanks again!

    Saying thanks to you all the time makes me feel like a bloody robot. :P

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)