Several Questions about Displaying Instance Variables and Creating Percentages

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • I've been creating several menus to perform specific tasks for a base management game I've been making. I've run into a few problems though, that have been hard to find a way to fix.

    Firstly, I want to be able to find the percentage of a population based on a boolean instance variable. I want to determine which members of the population the boolean is set to true and turn that into a percentage that I can display for players. For instance, of the population of 20 workers, 2 of them have the plague. Therefore the game would show the player that 10% of workers have the plague.

    I also want to do something similar for String Instance Variables. I want to show the players a percentage of the workers that are unemployed, which is determined by a string.

    I'm also wondering if it would be better to change each individual job the workers can have into boolean values and switch them on and off much easier than parsing a string. Let me know your thoughts, and I appreciate any ideas!

  • I assume the workers are instances of the same sprite?

    I strongly suggest using number variables instead of boolean - they are much easier to work with in Construct. Just assign 0 for false and 1 for true.

    For example, to count the number of workers who have plague, you can use this code

    Local variable n
    Local variable percentage
    
    For each Worker
    Worker hasPlaque=1 : Add 1 to n
    
    Set percentage to int((n/Worker.count)*100)
    

    Another method without the loop:

    Worker hasPlaque=1 : Set percentage to int((Worker.pickedCount/Worker.count)*100)
    

    Basically pick instances by comparing instance variables and count them. When multiple instances are picked, you can use Object.pickedCount expression. For total number of instances use Object.count expression.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You can use the same approach for string variables. If you want to compare a part of the string, use expressions like find(), left(), right(), lowercase() etc.

    But then you will need to pick instances using System events, for example:

    System Pick Workers by evaluating Left(Worker.name,4)="John"

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