Using ints inside variables in functions

Started by selmiak, Wed 09/11/2011 16:07:59

Previous topic - Next topic

selmiak

and another problem... I want to send an int to a function and want to use it as a part of a variable name. The variables may be bools or even object names. Here is the code, explanation below:
Code: ags

function buttoninteract(int buttonpressed) {
  if (button[buttonpressed]==false) { oButton[buttonpressed].Transparency=0;button[buttonpressed]=true;PlaySound(10); }
   else { oButton[buttonpressed].Transparency=100;button[buttonpressed]=false; PlaySound(11);}
}


I have a lot of buttons and they all look the same, are numbered and can either be on or off. Of course I want to store the state somewhere. Each button object has 2 images too.
so for example I press button 9 and call on
Code: ags

function oButton9_Interact()
{
  buttoninteract(9);
}


then I want the game to understand within the function the following
Code: ags

function buttoninteract(int buttonpressed) { // here the 9 comes in
  if (button9==false) { oButton9.Transparency=0;button9=true;PlaySound(10); }
   else { oButton9.Transparency=100;button9=false; PlaySound(11);}
}


the thing is, button# are global variables (bool). When compiling my code AGS tells me 'button' is no array or sometimes 'undefined symbol 'button''. Well, can I somehow write button(buttonpressed) or button{buttonpressed} or something inside the function like in php? And what about the object. I hope this is somehow possible, or otherwise I have a lot of lines for a lot of buttons and a lot of changes for one change within all the functions. That's what functions are for, right?
Should I send a string for that? Is this possible and how?

pcj

#1
The first problem is I don't see anywhere where the button array is declared.  (You can't use the Global Variables dialog to create an array)

If all your buttons interact with the same format script, you can redirect all their events to one function, which will remove one layer of complexity:

Presuming you're using the latest version of AGS, substitute the number of buttons you have below where it says # of buttons:

Code: ags
bool button[# of buttons];
function buttoninteract(GUIControl *control, MouseButton b) {
 Button* thisButton = control.AsButton;
 if (button[thisButton.ID] == false) {
    thisButton.Transparency = 0;
    button[thisButton.ID] = true;
    aSound10.Play();
 }
 else {
    thisButton.Transparency = 100;
    button[thisButton.ID] = false;
    aSound11.Play();
 }
}
Space Quest: Vohaul Strikes Back is now available to download!

selmiak

there is no button array declared as I have no array declared because I don't use an array yet. But it's a good idea. Gonna try my way around this way now :)
Guess I didn't think of arrays as I never used them in AGS and the buttons were already labelled this way by now >_>

pcj

Oh, I see, you were using separate variables for each of the buttons.  Then yeah, an array would make much more sense.
Space Quest: Vohaul Strikes Back is now available to download!

Khris

#4
To be clear about this: it's not possible in AGS to get from the contents of a variable to the name of another (without using many ifs). Variable names and contents are entirely separate.

Regarding this specific issue, there's a much shorter way:

Code: ags
function buttoninteract() {

  Object*o = Object.GetAtScreenXY(mouse.x, mouse.y);
  if (o == null) return;  // just to be safe

  o.Transparency = 100 - o.Transparency;
  PlaySound(10 + o.Transparency/100);
}


Now put "buttoninteract" in every button's "interact with object" event field.

As you can see, we didn't need a bool at all, we can simply use the transparency value.
The same is true when we want to check whether a button is activated or not:
  if (!oButton1.Transparency) Display("Button 1 active");
  else Display("Button 1 not active");


In general, it's a good idea to plan stuff like this beforehand; what you could do is create the button objects so that you can use their ID (oButton1 has ID 1, oButton2 has ID 2, etc.)
Now you can get from an int to the object by using the existing object[] array. As long as the room is loaded, the global pointer object[1] will now point to oButton1.

selmiak

Quote from: Khris on Thu 10/11/2011 00:38:06
In general, it's a good idea to plan stuff like this beforehand

This is so true, but atm I'm all into gonzo gamemaking :D

but I'll try your code :D

SMF spam blocked by CleanTalk