How do I "pick" a variable?

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I am making a small project to help me manage some stuff at work (self employed), and I am trying to do something that sounds like this:

    I have a set of 18 buttons, all setup to represent specific timeframes (e.g. January 2024, Year 2024, All time, etc...). These buttons are in a family.

    I have a map of my region with 54 different postcodes. These 54 postcodes are all separate sprites which I have put in a family.

    What I am trying to achieve is some kind of "heat map", where whenever I press a button, every postcode on the map "radiates" proportionally to the number of jobs I got during the timeframe selected by pressing the button.

    So I have setup each button with its own variable (set up by the family itself, not on each button separately), and have created a global variable to store which button has been pressed.

    For example, if I press the button "Year 2024", the global variable will change to "Year_2024". The reason I set it up this way is to I could then "pick" the variable on my postcode sprites based on what the global variable value is, as these sprites have 1 variable by time frame (so 18 variables), each with their own values, variating for each sprite, for each timeframe.

    I have set up my variable names on the sprite family to be the same names as whatever the global variable "text value" will be when you press any button.

    So the logic Im trying to write is this:

    When any button of the "button family" is clicked on -> Set global variable value to the name of that button's variable (ex: you press the button Year 2024, now the global variable text value is "Year_2024". You press Year 2023, global variable text value now becomes "Year_2023", etc...)

    Then

    For each Sprite in the Sprite Family, "Pick" the value from the variable with the same name as the global variable (e.g. you clicked the button "year 2024", global variable now shows "Year_2024", so you need to pick the variable named "Year_2024" for each sprite in the sprite family)

    Then

    Read the value associated with the correct variable (here, Year_2024, the value will vary depending on postcode) and store that value in another variable on the same sprite, for each of the 54 sprites independently.

    Than finally

    With the correct value taken from the correct variable for each sprite now stored in each sprite independently, change the opacity of each sprite based on their respective values. Higher value, higher visibility.

    I hope this makes some kind of sense to anybody.

    I would really appreciate some help on how to achieve this, and unfortunately, if this really, REALLY have to involve arrays, I'm going to have a massive problem because I don't understand arrays, or how to pick values out of cells etc... so a solution not involving arrays would be preferable.

    Thanks in advance to anyone willing to help.

    I'll keep trying on my end but I don't have much hope.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • What you described is not a great setup, you probably understand it yourself.

    A 2D array or JSON would be much easier to work with, to edit and store data.

    With 18 instance variables there are basically only two solutions:

    1. Use a bit of Javascript to retrieve the variable value by name:

    runtime.objects.spriteName.getFirstPickedInstance().instVars["var_name"])

    2. Check all variables one by one, until you find the right one:

    Let's say you store the selected year in vTime global variable, and want to extract the result into instance varible vResult.

    For Each Sprite
     If vTime="Year_2024" : Sprite set vResult to Self.Year_2024
     Else If vTime="Year_2023" : Sprite set vResult to Self.Year_2023
     Else If vTime="Year_2022" : Sprite set vResult to Self.Year_2022
     Else If vTime="Year_2021" : Sprite set vResult to Self.Year_2021
     .....
    
    

    You can compress that into one action if you use ternary operators:

    For Each Sprite
     Sprite set vResult to (vTime="Year_2024" ? Self.Year_2024 : vTime="Year_2023" ? Self.Year_2023 : vTime="Year_2022" ? Self.Year_2022 : ......)
    
  • Thank you for replying, I eventually figured a slightly longer way to do it, but it works well enough.

    Instead of having one event including the button family (e.g. On any "Button_Family" pressed, do XYZ), I created 18 events, 1 for each button, essentially getting rid of one dimension.

    On Button pressed

    For each "Sprite_Family" -> Set opacity to (Global variable counting total jobs for that time frame / the correct variable on the sprite, which I can now name in the event itself instead of trying to have the logic pick it for me

    Absolutely not efficient, probably going to make people retch, but its for a personal project, not sharing it, not selling it, so thats good enough for me!

    Thanks for you reply again dop2000, surely this will help someone else one day

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