Page 1 of 1

Global problems

Posted: Wed May 04, 2011 1:26 pm
by leo974
Hi everybody, before to start I want you to know I'm a french student, so excuse me for my english.
I get a problem with global variables and global functions (even I don't know if this keyword can be apply for a function).
Here is the problem, I have some buttons with the same script inside, so with the same variable and function names.
I wanted to access to specific button with a script like that:

Code: Select all

answer the value of "variable" of the button "button"
I tried many ways and differents syntax but it still don't work.
So the question is: is it possible to do that (i get the same problem to call a function in an other card) and if it is, how to do it?
Thanks :)

Re: Global problems

Posted: Wed May 04, 2011 1:33 pm
by Klaus
Bonjour Leo,

let me see:
You have a global variable in the script of a button and want to see/check the value of this variable in some other script, right?

Hmmm, since this is a GLOBAL variable, you can obviously do this at any time and in any place:
...
global gWhatever
answer gWhatever
...
But maybe I am misunderstanding you.


Best

Klaus

Re: Global problems

Posted: Wed May 04, 2011 1:39 pm
by leo974
My problem is all of the buttons get a variable with the same name (in the button script).
So when in my main card I want to check the value, I can't only do

Code: Select all

global globalVariabeValue
answer globalVariabeValue
because the script don't know which one to choose!

Re: Global problems

Posted: Wed May 04, 2011 1:48 pm
by Klaus
Hi Leo,

you have more than one global variables with the same name?
That is impossible 8)


Best

Klaus

Re: Global problems

Posted: Wed May 04, 2011 2:00 pm
by leo974
Oh...
That I really do is put automaticly a script in a button with the line:

Code: Select all

set the script of the templateButton to buttonscript 
and thereafter I would to modify the value of these buttons.
It would be more easy if every button would have the same name! The only way to fix is to assign a different variable name for all buttons? :(

Re: Global problems

Posted: Wed May 04, 2011 2:14 pm
by Klaus
Hi Leo,

if you (try to) have many global variables witht he same name, then LiveCode will
see this as ONE global variable, since LC cannot differ variables with the same name.
So can't you 8)

That means, yes, you need to use different names for your variables and I have no idea how to do this.
If the variables need to be GLOBAL, then you have no other choice!

What do your buttons do? Maybe we can check, modify and/or adjust your concept a bit?

Best

Klaus

Re: Global problems

Posted: Wed May 04, 2011 2:33 pm
by leo974
I'm making a game with friends, my button must appear for a while and be deleted.
So in the button card I've a the script (inject with the code in my previous post):

Code: Select all

global TTL // time to live

put 4 into TTL

// TTL down to 0 each second
on deleteTime
   put TTL - 1 into TTL
   send "deleteTime" to me in 1 second
end deleteTime
and in the main card

Code: Select all

global TTL

repeat with x = the number of controls in group "groupe" down to 1  // buttons are in a group
    put TTL of control x of group "groupe" into current_TTL // fail line
    if current_TTL <= 0 then
       delete control x of group "groupe"
    end if
end repeat
Thanks for your help :)

Re: Global problems

Posted: Thu May 05, 2011 10:27 am
by leo974
Up!

Maybe I can solve my problem if it possible to call a function of a card in another one. You know the way to do this?

Re: Global problems

Posted: Thu May 05, 2011 11:20 am
by SparkOut
There are several approaches depending on what you are actually doing, but possibly a simple way is to put the function into the stack script, where it will be available to all the cards below it. You won't then need to do any "calling" to a function. Just refer to it by name in the card script and it will be found in the message path in the stack script above it.

Re: Global problems

Posted: Thu May 05, 2011 11:36 am
by Klaus
Hi Leo,

hmm, I do not see a possibility to solve this with variables!
BUT this a very good candidate for custom properties! :D

Hint:
Custom properties are a bit hard to understand at first, but then you don't want to miss them anymore!

A custom property is almost the same as the "build-in" props (like "witdh", "height" etc.) or a variable,
BUT:
1. we can name them any way we want, as long as we don't use reserved words
2. we can put into them whatever we want, just like a variable!
3. they are tied to a specific object (stack/card/button/field/player etc...)
4. they get saved with the stack they reside in automatically! UNLIKE variables
5. Each object can have an unlimited number of custom properties!

Assigning is easy:
...
set the cWelcomeString of fld "my field or yours" to "Hello sailor!"
## I got used to name my custom properties with a starting c
...

Accessing is, too:
...
answer the cWelcomeString of fld ""my field or yours"
## Please note the "THE"!
...

Another advantage is that you can name the custom props of different object with the same name,
and that is exactly what we need in your case!

Here we go:
The script that counts TTL down, NO need for globals anymore!

Code: Select all

on deleteTime
   ## First get the custom property:
   put the cTTL of me into TTL
   
   ## Maybe this object was not yet used
   if TTL = empty then
      put 4 into TTL
   else
       ## We can count down
      put TTL - 1 into TTL
   end if
  
   ## Now we need to "wriute" the modified custom property back to the objct
   set the cTTL of me to TTL
   send "deleteTime" to me in 1 second
end deleteTime
Now the other handler:

Code: Select all

...
repeat with x = the number of controls in group "groupe" down to 1  // buttons are in a group
    put the cTTL of control x of group "groupe" into current_TTL // NO MORE fail line
    if current_TTL <= 0 then
       delete control x of group "groupe"
    end if
end repeat
...
OK, try this and report back to us! :D


Best

Klaus

Re: Global problems

Posted: Fri May 06, 2011 10:24 am
by leo974
It works!
Thank you all, mostly Klaus :wink:

Re: Global problems

Posted: Fri May 06, 2011 10:41 am
by Klaus
Hi Leo,

GREAT! :D