• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED] Making several instances appear by clicking the same button

L

Lori

Guest
Hello everbody,

total newbie here, so please go easy on me ;-)

I'm trying to make a round of a quiz game in which every click on the button "Next clue" (an instance of obj_btn) gives you another clue (instance of obj_clue).

I've tried setting it up so that a click on the button will add 1 to a variable called clickCounter.
Then I wanted to use the value of clickCounter in a switch/ if statements to create each new instance.
So if the button had been clicked once, you'd have one extra clue; twice, you'd have two extra clues, etc.

When I run it, one click works and I get one extra clue, but after that, the button becomes useless.

Now, on the topic of mouse_check_button_released(mb_left), the documentation says: "This function will return true if the mouse button being checked has been released or false if it has not. This function will only be triggered once for any mouse button when it is released and to trigger it again the button will need to have been pressed and released again."

So I gather my mouse_check only returns "true" ones and then cannot perform another click.
Is there a way to work around this? Can I manually set the mouse_check to "false" or something?!

Sorry, seems like an easy problem with a hopefully onvious answer but I have not been able to google anything helpful...

Thanks & cheers
Lori

---


Here's one of the ways I tried - though I'm also not quite sure of where to put the code. The room or the obj_btn?

Code:
var nextClickCounter = 0;

if mouse_check_released(mb_left)
{
nextClickCounter += 1;
}

if nextClickCounter = 0
{
        var clue = instance_create(80,240,obj_clue); //creates first instance of the "Next clue" button
        clue.v_clue = "Clue 1";  //prints "Clue 1" on the button
}
           
if nextClickCounter = 1
{
        var clue = instance_create(80+147,240,obj_clue);
        clue.v_clue = "Clue 2";
}
   
if nextClickCounter = 2
{
        var clue = instance_create(80+147*2,240,obj_clue);
        clue.v_clue = "Clue 3";
}

if nextClickCounter >= 3
{
        var clue = instance_create(80+147*3,240,obj_clue);
        clue.v_clue = "Clue 4";
}
using Game Maker Studio v1.4.1757
 
Last edited by a moderator:

jo-thijs

Member
Hi and welcome to the GMC!

It looks like you know exactly how to do it, except for 3 things:
1) This is not true:
Code:
So I gather my mouse_check only returns "true" ones and then cannot perform another click.
Is there a way to work around this? Can I manually set the mouse_check to "false" or something?!
It will work for multiple clicks.
The documentation tries to tell you that when you release the button, mouse_check_button_released will only return true for the duration of 1 step when releasing the button,
afterwards you need to release the button again for it to return true again.
There's no problem there, you can work with it as you would expect to be able to work with it.

2) This part of your code is wrong:
Code:
var nextClickCounter = 0;
You have to remove "var" and put the rest of this line in the create event.

The rest of your code would go in the step event.

var creates a temporary variable that will be deleted at the end of the event or script.

You also don't want to keep resetting the variable to 0, only initialize it as 0.

3) The code you've now put in the step event actually has to be changed slightly from:
Code:
if mouse_check_released(mb_left)
{
nextClickCounter += 1;
}

...
to:
Code:
if mouse_check_released(mb_left)
{
    ...
    
    nextClickCounter += 1;
}
Otherwise, you keep creating instances every step.
 
L

Lori

Guest
Thank you very much!! That worked! I'm so relieved...my whole Sunday was spent (I dare not say "wasted"..) on this.

The (added) trouble was that I had two instances of that obj_button (one with "Next Clue" and one with "Solve") and could not distinguish which of those instances the code addressed with mouse_check_button_released(mb_left).

My workaround (surely not the best way):
Went into the GUI Editor of the room, went to "Creation Code" for each instance because I had no ide how to address them in code and gave each instance a boolean variable storing whether the button had been clicked or not:
Code:
 if mouse_check_button_released(mb_left) then btn_next_pressed = true
Then used that in the LeftRelease Event for my obj_button:
]
Code:
if btn_next_pressed
{
    nextClickCounter += 1;
}

if btn_solve_pressed
{
    ..
}
 
Last edited by a moderator:
Top