For Each Button (Family) change Text (Family) ?

  • Hi All,

    I have 2 families: 1 for buttons, 1 for text.

    Buttons are Sprites, Text are text-object that are OVER the buttons.

    I'm trying to shorten my VERY LONG code to more efficient using the families and "For Each".

    With my old VERY LONG code it works great, but I can't make it work using for each:

    Instead of using every single button (individual object for each button) and every single text-object for each means (lots of objects) like this:

    Which is NOT efficient at all! I want to to make 1 Button Object and duplicate it to how many instances I ever need, same for the Text-Object (ONLY 1 with instances)

    So I understand the "idea" that I will need to pick the UID or something for each individual instance duplication of the Object and the Text.

    The main idea, I will have only 2 objects: Button & Text with their instances (efficiency!)

    But I don't know how to make it work, here is what I start playing around with:

    I've attached a new project C3 File so you can play with to see what I'm trying to do

    >> DOWNLOAD HERE <<

    Thanks ahead! :)

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks plinkie!

    The visual works, but not sure if I was clear because of my bad English.

    How do I make EACH button (when click) to do it's individual job, that's why I thought it should use "For Each" or UID in a way since they are all the same objects, different UID for each one of the buttons.

    This could really shorten my code, Thanks ahead!

  • Not really sure what you are asking. You use families to allow different buttons to do the same thing without telling them individually, so one event instead of many. When you say cursor is over button family, you are saying for each button object that exists in the family.

  • I thought I need "For Each" instead of put manually the UID for every event to make it more efficient.

    But I don't know how to do that.

    Well, it works but unfortunately it's not going to be very efficient after all, but it's another way to work.

    Since I can't explain what I'm trying to do very well (sorry about that) I've attached a C3 file of how it works in case any noob like myself is interested exploring using the UID + Families.

    It works exactly as I wanted now, using the UID so thank you for helping with the Families mess :)

    Here it is:

    >> DOWNLOAD C3 EXAMPLE HERE <<

  • here is how I would do it...

    https://www.rieperts.com/games/forum/buttonstext.c3p

  • What I did was make the button a container and put the text object in it, so that it will be automatically picked with the button. I also gave the button an instance variable with the name of the function to call when a button is clicked... then one event can be used for any number of buttons.

    I use an “every tick” event to reset all the buttons and text objects (since they are all picked by default). Then the mouse over and mouse click events filter out the ones we don’t want. So no for each loop is needed.

  • Thanks for sharing your way AllanR I appreciate it, seems very useful!

    The thing is that I was trying to shorten my code, but all this work-around not actually shorten so I can just use individual buttons as I original did (not in my examples here) but I was curious if using the families and UID will make the short much shorter, well I'm learning new things every day! :)

  • The families are used to group all buttons as one single object so you can refer to them like this in one event. Reverting back to saying if overlapping button 1, button 2, button 3 etc, that's what families are there to resolve.

  • The families are used to group all buttons as one single object so you can refer to them like this in one event. Reverting back to saying if overlapping button 1, button 2, button 3 etc, that's what families are there to resolve.

    That's how I'm using families after all (my favorite feature on C3) but as I said, it's not shorten my code not because of families, but the all UID vs individual objects as objects. I thought there is a way to make the code much shorter, maybe there is but it doesn't matter anymore I'm currently using my original code which is not based on UID, since it's much shorter and still organized.

    But I must say that I did learn A LOT from this discussion, I'm very glad that you guys are helping! :)

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