Page 1 of 1
Accessing variables across buttons
Posted: Thu Feb 04, 2016 4:01 pm
by jamtart
I'm currently trying to create a card game in LiveCode. I have one button that randomly selects 3 cards (value and suit separately) from a 2D array. These values are then placed into another 2D array for player 1. When I try to display any variables from this button using a second one, the message box appears blank. I have made all of my variables global, and declared them on all relevant buttons. I have created another variable and placed a value in it using a third button, and then displayed the variable on the second button. This worked no problem, but I still can't display variables from the first button.
Let me know if you need me to post any code or any other information.
Any help is appreacated, Thanks!
Re: Accessing variables across buttons
Posted: Thu Feb 04, 2016 4:07 pm
by Klaus
Hi jamtart,
in LC you need to declare GLOBALs in every script they are used!
Example
Stack script:
Code: Select all
global aGlobal
on openstack
put 10 int aGlobal
end openstack
Any button script:
Code: Select all
global aGlobal
on mouseup
compute_something
end mouseup
command compute_something
add 10 to aGlobal
end compute_something
I think this is the reason for your inconvenience
Best
Klaus
Re: Accessing variables across buttons
Posted: Thu Feb 04, 2016 4:27 pm
by jamtart
Thanks for getting back to me.
I have declared the global variables in both buttons (the code for selecting cards is on one button, and the code for displaying the selected card is in another). Any other reason you can think of for a blank message box?
Re: Accessing variables across buttons
Posted: Thu Feb 04, 2016 4:33 pm
by Klaus
Hmmm?
Can you please post your script(s)?
Re: Accessing variables across buttons
Posted: Thu Feb 04, 2016 4:39 pm
by jamtart
This may be slightly overkill, but here is my all of my scripts in their entirety.
Here is one button:
Code: Select all
on mouseUp
set_up
create_card_arrays card_array
shuffle card_array, selected_card_1, selected_card_2, selected_card_3, all_selected_cards
apply_to_p1 selected_card_1, selected_card_2, selected_card_3, p1_cards
shuffle card_array, selected_card_1, selected_card_2, selected_card_3, all_selected_cards
apply_to_p2 selected_card_1, selected_card_2, selected_card_3, p2_cards
display_cards p1_cards, p2_cards
end mouseUp
on set_up
//Create Variables
global card_array
global selected_card_value
global selected_card_suit
global selected_card_1
global selected_card_2
global selected_card_3
global selected_cards
global counter
global p1_cards
global p2_cards
end set_up
on create_card_arrays @card_array
//Put values into card array
put "7" into card_array["value"][1]
put "8" into card_array["value"][2]
put "9" into card_array["value"][3]
put "10" into card_array["value"][4]
put "Jack" into card_array["value"][5]
put "Queen" into card_array["value"][6]
put "King" into card_array["value"][7]
put "Ace" into card_array["value"][8]
//put suits into card arrray
put "Diamonds" into card_array["suit"][1]
put "Hearts" into card_array["suit"][2]
put "Clubs" into card_array["suit"][3]
put "Spades" into card_array["suit"][4]
end create_card_arrays
On shuffle card_array, @selected_card_1, @selected_card_2, @selected_card_3, all_selected_cards
//shuffle deck
//Initialise
put 0 into selected_card_1[1]
put "" into selected_card_1[2]
put 0 into selected_card_2[1]
put "" into selected_card_2[2]
put 0 into selected_card_3[1]
put "" into selected_card_3[2]
//get card 1
put card_array["value"][random(8)] into selected_card_1[1]
put card_array["suit"][random(4)] into selected_card_1[2]
put selected_card_1 into all_selected_cards[1]
//get card 2
put card_array["value"][random(8)] into selected_card_2[1]
put card_array["suit"][random(4)] into selected_card_2[2]
//make sure card 2 is unique
repeat with counter = 1 to 9 until selected_card_2 is not all_selected_cards[counter]
put card_array["value"][random(8)] into selected_card_2[1]
put card_array["suit"][random(4)] into selected_card_2[2]
end repeat
put selected_card_2 into all_selected_cards[2]
//get card 3
put card_array["value"][random(8)] into selected_card_3[1]
put card_array["suit"][random(4)] into selected_card_3[2]
//make sure card 3 is unique
repeat with counter = 1 to 9 until selected_card_3 is not all_selected_cards[counter]
put card_array["value"][random(8)] into selected_card_3[1]
put card_array["suit"][random(4)] into selected_card_3[2]
end repeat
put selected_card_3 into all_selected_cards[3]
end shuffle
On Apply_To_P1 selected_card_1, selected_card_2, selected_card_3, @p1_cards
put selected_card_1[1] into p1_cards[1][1]
put selected_card_1[2] into p1_cards[1][2]
put selected_card_2[1] into p1_cards[2][1]
put selected_card_2[2] into p1_cards[2][2]
put selected_card_3[1] into p1_cards[3][1]
put selected_card_3[2] into p1_cards[3][2]
end Apply_To_P1
On Apply_To_P2 selected_card_1, selected_card_2, selected_card_3, @p2_cards
put selected_card_1[1] into p2_cards[1][1]
put selected_card_1[2] into p2_cards[1][2]
put selected_card_2[1] into p2_cards[2][1]
put selected_card_2[2] into p2_cards[2][2]
put selected_card_3[1] into p2_cards[3][1]
put selected_card_3[2] into p2_cards[3][2]
end Apply_To_P2
On display_cards @p1_cards, @p2_cards
answer p1_cards[1][1] & " of " & p1_cards[1][2]
answer p1_cards[2][1] & " of " & p1_cards[2][2]
answer p1_cards[3][1] & " of " & p1_cards[3][2]
answer p2_cards[1][1] & " of " & p2_cards[1][2]
answer p2_cards[2][1] & " of " & p2_cards[2][2]
answer p2_cards[3][1] & " of " & p2_cards[3][2]
end display_cards
Here is another button:
Code: Select all
on mouseUp
global lava
global card_array
global selected_card_value
global selected_card_suit
global selected_card_1
global selected_card_2
global selected_card_3
global selected_cards
global counter
global p1_cards
global p2_cards
answer lava
answer card_array["value"][1]
end mouseUp
Here is the last button:
Code: Select all
on mouseUp
life
end mouseUp
On life
global lava
put "Bob" into lava
end life
Re: Accessing variables across buttons
Posted: Thu Feb 04, 2016 5:11 pm
by Klaus
Hi jamtart,
as I wrote, you need to define your globals at the top of the script OUTSIDE of any handler!
Code: Select all
global card_array
global selected_card_value
global selected_card_suit
global selected_card_1
global selected_card_2
global selected_card_3
global selected_cards
global counter
global p1_cards
global p2_cards
on mouseUp
## See below
## set_up
create_card_arrays card_array
shuffle card_array, selected_card_1, selected_card_2, selected_card_3, all_selected_cards
apply_to_p1 selected_card_1, selected_card_2, selected_card_3, p1_cards
shuffle card_array, selected_card_1, selected_card_2, selected_card_3, all_selected_cards
apply_to_p2 selected_card_1, selected_card_2, selected_card_3, p2_cards
display_cards p1_cards, p2_cards
end mouseUp
##on set_up
##Create Variables
## The globals are created automatically if you put them at the top of the script!
##end set_up
...
Code: Select all
global lava
on mouseUp
life
end mouseUp
On life
put "Bob" into lava
end life
Best
Klaus
Re: Accessing variables across buttons
Posted: Thu Feb 04, 2016 7:10 pm
by Lagi Pittas
Hi
what Klaus said, but ...
Put all your handlers and globals in the stack or card script so you don't need to keep declaring the variables everywhere, since all the handlers
will be in the same script and the globals will be at the top.
you could be "clever" and have 1 mouseup handler and test for the target but i'd suggest for ease of reading/writing etc each button have a mouseup with a call to its handler
e.g.
Code: Select all
button 1
on MouseUp
StartMeUp
end MouseUp
in the stack ....
on StartMeUp
-- Set_up call not needed
put empty into card_array
create_card_arrays card_array
shuffle card_array, selected_card_1, selected_card_2, selected_card_3, all_selected_cards
apply_to_p1 selected_card_1, selected_card_2, selected_card_3, p1_cards
shuffle card_array, selected_card_1, selected_card_2, selected_card_3, all_selected_cards
apply_to_p2 selected_card_1, selected_card_2, selected_card_3, p2_cards
display_cards p1_cards, p2_cards
end StartmeUp
--- other handlers shuffle, apply.... etc
button 2
on MouseUp
ShowThem
end MouseUp
-- In the stack
on ShowThem
answer lava
answer card_array["value"][1]
end ShowThem
button 3
on MouseUp
life
end MouseUp
in the stack
on Life
put "Bob" into lava -- no globals needed
end life