grouping boolenean variables?

Started by miguel, Fri 17/02/2012 10:53:44

Previous topic - Next topic

miguel

Hi,

is there a way to group bool variables and assign their values to false or true when needed?

A custom-function would work if I wanted ALL of the variables true or false but it's not the case.

Imagine 4 switches, (a,b,c & d), if a is on (true) all of the others should be off (false), and if b was on all others should be off and so on...

To code custom functions for4 switches would be easy but I've got 22 bool variables that act like switches!

I've been reading for arrays and structs but can't make it, and the "search" isn't working.

Thank you
Working on a RON game!!!!!

Khris

Code: ags
bool switch[22];

void turn_on(int index) {
  int i;
  while (i < 22) {
    switch[i] = (i == index);
    i++;
  }
}

miguel

I read it but can't understand it,

with that code how can I declare that if (i=3 means it is true) then all others are false?
Working on a RON game!!!!!

cat

That's exaclty what Khris' code does. It is very compact and efficient so i try to write it in a different, better understandable form:

Code: ags

bool switch[22]; // define the array of your switches

void turn_on(int index) {
  int i;
  while (i < 22) { // we iterate through the array, looking at every single item
    if (i == index) // we check if we are currently at the item we want to turn on
        switch[i] = true; // it is our selected switch, turn it on
    else
        switch[i] = false; // it is another switch, turn it off
    i++; // move to the next item in the array
  }
}

miguel

Okay, now I understand the concept of the thing, Khris example was what I've been reading and couldn't understand, yours is more beginner friendly.
It is still complicated for me and my brain has still to click, I'll dive into it and get back if I can't implement what I want to achieve.

In the meantime thank you both Cat and Khris
Working on a RON game!!!!!

Khris

I used a boolean which has only two possible values, true and false.

As you know,
Code: ags
  if (3 > 5) Display("I won't show up.");

evaluates to
Code: ags
  if (false) Display(...);


Whenever you use a condition in code, it is turned into its actual value. Like a boolean, a condition is either true or false. This allows you to store the truth value of a condition into a boolean.

Another cool thing about booleans is that the value gets converted to 0 (false) or 1 (true) if it's used in the context of ints.
The following is valid code:

Code: ags
  bool cond = (player.y > 100 && gMessage.Visible);
  int y = cond*50 + !cond*100;


This code does exactly the same as
Code: ags
  int y;
  if (player.y > 100 && gMessage.Visible) y = 50;
  else y = 100;


Not really useful in this example, but makes for neat code when several conditions are involved.

miguel

Yes, the potential is there for me to understand it and I can see that it would help me a lot. I just wish the manual had more stuff about it or the search engine was working.
Arrays and Structs are there to discover as well, it would make my gigantic codes so much simple.
Assigning variables to specific characters would be a major breakthrough to me as well.
But the effort is rewarding to me, it feels great to improve with coding.

thanks Khris
Working on a RON game!!!!!

Khris

Quote from: miguel on Fri 17/02/2012 14:31:08Assigning variables to specific characters would be a major breakthrough to me as well.

The simplest way to do that:

Code: ags
// header of new script

import void SetAge(this Character*, int age);
import int GetAge(this Character*);

// body
int CharacterAge[];

void game_start() {
  CharacterAge = new int[Game.CharacterCount];
}

void SetAge(this Character*, int age) {
  CharacterAge[this.ID] = age;
}

int GetAge(this Character*) {
  return CharacterAge[this.ID];
}


Now you can do:
  player.SetAge(35);
  Display("You are %d!", player.GetAge());

SMF spam blocked by CleanTalk