Using value of Variable

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

kev007
Posts: 2
Joined: Tue May 20, 2014 11:37 am
Location: NI, Germany

Using value of Variable

Post by kev007 » Tue May 20, 2014 11:48 am

Hi,
I have a Problem using Variables.

I have al global Variable global bID1
in this I put the ID of an button.
So far it works.
Now I want to do show a button with the id saved in bID1
I used show button "bID1" and also tried it without "".
After a friend tried to help me I used show button with the id value(bID1)
but all these variations produce an error: execution error at line 27 (Chunk: no such object) near "with"
or something like this. For me it seems LiveCode trys to use bID1 as ID not its value.

What I'am doing wrong?
Thanks!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3990
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Using value of Variable

Post by bn » Tue May 20, 2014 12:06 pm

Hi Kev,

try

Code: Select all

 global bID1
show button id bID1
note: if you address a button by id you use

button id theID

where theID is just the number. Don't quote the variable because LC thinks it is a literal, i.e. not a variable.

Kind regards
Bernd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Using value of Variable

Post by dunbarx » Tue May 20, 2014 5:16 pm

Hi.

What Bernd said.

You should practice referencing objects by the several ways open to you. By "name", by "number" and by "ID". There are others as well ("last", me", "third", etc.) t would be worth doing this. Make several buttons and name each of them. Now, maybe in still another button, write a handler that changes the backColor of a button. Something like;

set the backColor of button 4 to "red"
set the backColor of button "someButtonName" to "green"
set the backColor of button ID "IDNumber" to "yellow"

Now write a handler to loop through all the buttons, say by number, and do something else with them. And now a handler that loops through all the buttons, but only does something to a button with a particular name.

You have to get this down pat.

Craig Newman

kev007
Posts: 2
Joined: Tue May 20, 2014 11:37 am
Location: NI, Germany

Re: Using value of Variable

Post by kev007 » Tue May 20, 2014 7:35 pm

Hi there,

Tanks Bernd that was exactly what I was looking for..
so now it works! :D

And Craig thanks for your explications, in this case I needed
to use IDs.

So thanks for helping!

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Using value of Variable

Post by mwieder » Tue May 20, 2014 7:52 pm

...although it's bad form to refer to an object by id if you have another way to reference it. Try setting the name of the button to something unique, storing it in the variable, and using it that way:

Code: Select all

global bID1

put "myButtonName" into bID1
show button bID1

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Using value of Variable

Post by dunbarx » Wed May 21, 2014 5:46 am

Mark.

Why bad form? Certainly less user (or author) friendly, since names and number fit more naturally with human thinking and natural ordering, respectively.

And it is comforting to know that the ID is (except for stacks and images) permanent. This is incredibly useful at times, as you well know. So why say bad form?

Craig

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Using value of Variable

Post by mwieder » Wed May 21, 2014 6:55 am

Craig-

IDs aren't permanent, and haven't been for some five years now. And I should know, since I'm the one who pushed for this change to happen. Immutable ids were the thing that prevented version control from working.

But the use of ids can run into problems in other situations besides just version control. For instance, if you duplicate a card (copy/paste) then you get a new card, and the controls on the new card have new ids. But if the controls on that card refer to each other by id then they will be referring to controls on the original card, which may or may not be accessible at the time. If the controls were referenced by name then there wouldn't be a problem.

FredBeck
Posts: 77
Joined: Fri Nov 01, 2013 3:07 pm
Location: Paris
Contact:

Re: Using value of Variable

Post by FredBeck » Wed May 21, 2014 8:25 am

Interesting, but didn't I read somewhere that accessing by short ID was significantly faster?
What do you mean they're not permanent? They won't change by themselves over time will they?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Using value of Variable

Post by mwieder » Wed May 21, 2014 5:39 pm

Ah, sorry... no, they won't change over time. It's just that a long time ago in a galax... wait...

OK - I'm back. At one point you couldn't change the id of any control except for images. Now you can explicitly set the id of a control to any unused integer. But only if you really, really have to. You can get into trouble otherwise if code depends on referring to conrols by id.

...and I haven't done any benchmarking, but I can't believe that referring to controls by id is noticeably faster than by other means.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Using value of Variable

Post by FourthWorld » Wed May 21, 2014 7:20 pm

mwieder wrote:...and I haven't done any benchmarking, but I can't believe that referring to controls by id is noticeably faster than by other means.
Did someone say "benchmarking"? :)

Code: Select all

on mouseUp
   put 5000 into M -- number of objects 
   put 500  into N -- number of object references
   --
   --create test objects:
   create stack "AccessTimes"
   set the destroyStack of stack "AccessTimes" to true
   set the defaultstack to "AccessTimes"
   lock screen
   lock messages
   repeat with i = 1 to m
      create btn "SomeButton"& i
   end repeat
   --
   -- Get test set:
   repeat N
      put random(M) into x
      put the short name of btn X &cr after tNames
      put the short id of btn X &cr after tIDs
      put the number of btn X & cr after tOrds 
   end repeat
   --
   -- Test 1: names
   put the millisecs into t
   repeat for each line tLine in tNames
      put the long id of btn tLine &cr after r1
   end repeat
   put the millisecs - t into t1
   --
   -- Test 2: IDs
   put the millisecs into t
   repeat for each line tLine in tIDs
      put the long id of btn id tLine &cr after r2
   end repeat
   put the millisecs - t into t2
   --
   -- Test 13 Ordinals
   put the millisecs into t
   repeat for each line tLine in tOrds
      put the long id of btn tLine &cr after r3
   end repeat
   put the millisecs - t into t3
   --
   put "Names: "&t1 &cr&"IDs:  "& t2 &cr& "Ordinals: "& t3 &cr& (r1=r2) && (r2=r3)
end mouseUp
You may want to double-check that it's testing accurately; here the outcomes are typically:

Names: 148
IDs: 40
Ordinals: 35
true true

This is understandable given the assumption that objects are stored as linked lists, and string comparisons are relatively expensive.

But it's also worth noting a few details:

1. These times are in milliseconds, for the total of 500 object references across a set of 5,000 objects. So:
a) We're asking the engine to work harder than most normal layouts would require.
b) Even under these very unusual circumstances, the slowest resolution time is 0.296 milliseconds.
c) Programmer time is almost as valuable as execution time, and the mnemonic value of names makes them an excellent choice for most circumstances; ordinals are fastest, but most volatile during the workflow, and IDs have no mnemonic value.

2. Object long IDs are said to be cached in recent versions to boost reference resolutions times. I've read those release notes several times and haven't arrived at a clear enough understanding of what that means to try to test it, and when I've asked on the use-livecode list I came up empty there as well. But if anyone is able to figure out what that means enough to test it, I'd be very interested in what you find.

3. While HyperTalk and LiveCode are superficially similar, the underlying data structures are very, very different. Many things we might have taken for granted in HyperTalk can benefit from re-testing in LiveCode. One of the biggest structural differences is that HyperCard stacks were paged from disk, while LC stacks are always read into RAM in their entirety. Among other things, this requires very different means for the engine to locate objects. Like many complex disk stores, I believe HC used hashes internally for object packaging, and perhaps the IDs it assigned were related to those hashes (anyone remember if those were sequential? I have a vague recollection of them seeming somewhat random, explainable if they're also used to determine the storage bucket from the hash).
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Using value of Variable

Post by dunbarx » Wed May 21, 2014 7:53 pm

Mark.

Brain problem. I was actually part of the thread last year that discovered and tested this, that all controls now have settable ID's.

I see the fact that an ID reference to a control after a card copy will refer back to the parent control as a feature, not a trap, though it certainly must be managed carefully. The name would be shared by the two controls, which might more easily be a trap, whereas the ID's are distinct. This gives options, I think, albeit with requisite caution if you reference as you point out. But still a feature, I think.

The dictionary needs updating for sure; I am using 6.5.

Craig

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Using value of Variable

Post by mwieder » Wed May 21, 2014 9:49 pm

Craig-

Point taken. The use of ids as control references can indeed be a feature or a trap for the unwary. And there are still some cases (fortunately rare) where the language syntax forces you to use the id form. I think if you don't care about version control and you're not going to go about changing object ids then it's safe to use them, but it's still my last resort as an object reference.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Using value of Variable

Post by mwieder » Wed May 21, 2014 9:58 pm

Richard-

I knew as soon as I said "benchmarking" you'd come around. Thanks for doing the legwork. However, I think the random() function may be a bit out of place since it won't eliminate duplicate entries and not every button in the set may get accessed. This may or may not make a difference: if a button appears twice in the list the its previous reference may be cached and throw off the timing. But it would throw off all three loops by the same amount, so it may just be a wash.

...and it's interesting the dereferencing an id is so much faster than dereferncing a name, even though there's string manipulation going on in both cases. My guess from looking at the timings is that the engine is doing a string-to-string comparison of the name, while the id is stored in memory as an integer.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Using value of Variable

Post by [-hh] » Wed May 21, 2014 11:00 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 2:53 pm, edited 1 time in total.
shiftLock happens

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9802
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Using value of Variable

Post by FourthWorld » Wed May 21, 2014 11:40 pm

Good thought, Hermann. You could check the overhead rate by adding a second loop to the test that does no meaningful task ("get 1+1"), and then deduct that time from the total time to get the net difference.

If someone here has time to do that it may prove interesting.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”