• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

SOLVED I cant get the choose function to work?

Coffeli

Member
When i press the mouse button it uses both of my choose values and not just one. I am trying to call scripts

step event:
if mouse_check_button_pressed(mb_left)
{
choose(scrattack1(),scrattack2());
}
 

chamaeleon

Member
When i press the mouse button it uses both of my choose values and not just one. I am trying to call scripts

step event:
if mouse_check_button_pressed(mb_left)
{
choose(scrattack1(),scrattack2());
}
choose takes values and returns a value from them. It does not take actions and then only invokes one of the actions. In your code choose is being given the return values of the two functions which will have been both called before choose returns one of those two return values.

Assuming GMS 2.3 functions you could do
GML:
var f = choose(scrattack1, scrattack2);
f();
 

Coffeli

Member
choose takes values and returns a value from them. It does not take actions and then only invokes one of the actions. In your code choose is being given the return values of the two functions which will have been both called before choose returns one of those two return values.

Assuming GMS 2.3 functions you could do
GML:
var f = choose(scrattack1, scrattack2);
f();
it worked but would does the f mean in that?
 

chamaeleon

Member
it worked but would does the f mean in that?
var f is just a local variable whose name you could make whatever you want (perhaps something more representative of the nature of your scripts).
GML:
var chosen_attack = choose(scrattack1, scrattack2);
chosen_attack();
If you want to take the action right there and then it is also possible to write
GML:
choose(scrattack1, scrattack2)();
and avoid the use of an temporary variable. But the concept remains the same, don't call the functions you want to choose between, rather invoke the function returned by choose().
 

Coffeli

Member
var f is just a local variable whose name you could make whatever you want (perhaps something more representative of the nature of your scripts).
GML:
var chosen_attack = choose(scrattack1, scrattack2);
chosen_attack();
If you want to take the action right there and then it is also possible to write
GML:
choose(scrattack1, scrattack2)();
and avoid the use of an temporary variable. But the concept remains the same, don't call the functions you want to choose between, rather invoke the function returned by choose().
ohhhh okay, thank you for your help!
 
Top