Page 1 of 1

Question about local variables

Posted: Fri Mar 21, 2014 11:27 pm
by seanmiller

First off I want to say I've been extremely impressed with LiveCode and with the support the company has provided. Although I've been in IT for many years, I'm excited to get my hands dirty with software development with the help of LiveCode.

I'm working on an app that essentially is a quiz with multiple choice answers. As a beginner, I've been making steady progress, but I've reached an impasse. Thus far, my app has 2 cards, one with a user interface and the other for storing the quiz data--in a field. In a nutshell, there's a line in the data field with a value for the correct answer to a given quiz question. When the user hits a button to confirm her answer, a handler compares the answer selected to the correct answer. Based on the result, it puts a text response into another field on the user interface card. But I can't get the local variable to work.

Would you mind taking a look at my stack to see what I'm missing? I've emailed the stack to support.


Re: Question about local variables

Posted: Mon Mar 24, 2014 1:21 pm
by LCNeil
Hi Sean,

Thank you for sending in your stack.

You were almost there with your variables, but there are a few things that you need to be wary of when using variable across cards. It will probably be beneficial to explain how the various variable types working in LiveCode.

There are various variable types in LiveCode-

-Temporary Variable (tVar)
-Local Variable (sVar)
-Global Variables (gVar)

Temporary variable are variable that are used within the context of a handler and can only be accessed when the handler they are present in is being executed-

Code: Select all

on mouseUp
put "test" into tTest
answer tTest
end mouseUp
After the handler has finished executing, the variable is discarded

Local Variables can be accessed throughout all handlers within a specific object (e.g. card script). You must declare these before they can be used

(code placed on card script)

Code: Select all

Local sTest

on openCard
put "test" into sTest
send "showMessage" to me in 3 seconds"
end openCard

on showMessage
answer sTest
end showMessage
Global Variables can be accessed throughout your whole stack (e.g. across multiple cards) and like Locals they must be declared, but this time everywhere they are being called

(following is global assigned on one card and then answered on another card)

Code: Select all

(card 1)

global gTest

on openCard
put "test" into gTest
end openCard


gloabl gTest

on showMessage
answer gTest
end showMessage

--show message called from a mouseUp on a button

You should notice that we have an internal naming structure for our variables, this just makes it easier to see which variable is of what type (t = tempoary, s = local, g = global)

Now for your stack, since you are placing a value into a variable when you select the correct answer (from a field) and you want this value to be checked on your card script, this value will need to be placed into a global variable and not local variable.

So for your field your fields you would declare your global variable with

Code: Select all

global selectedAnswer
outside the mouseUp handler at the top of the field script and you would do the same on the card script-

Code: Select all

local theQuiz, correctAnswer
global selectedAnswer
After I made these changes, the answeresponse appeared as expected.

I hope the above information gives you some leads.

Kind Regards,

Neil Roger
RunRev Support Team ~

Re: Question about local variables

Posted: Tue Mar 25, 2014 3:39 am
by seanmiller
Thanks, Neil. That certainly did the trick! I think have a better understanding of variables in LiveCode now. It's much appreciated.