Using custom properties in calculations

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

Post Reply
TheSHEEEP
Posts: 8
Joined: Mon Jun 03, 2013 10:13 am

Using custom properties in calculations

Post by TheSHEEEP » Mon Jun 03, 2013 10:26 am

Hey there,

I'm an experienced developer in other languages and currently trying to learn some basic LiveCode, as I'm planning to use it in a programming course for non-coders ;)

Now, I do have some serious problems with variables and scopes in LiveCode not at all behaving like I would expect.

I declare a few custom property of an image like this:

Code: Select all

on newImage
   set the distance of me to 4
   set the initialWidth of me to width of me
   set the initialHeight of me to height of me
end newImage
I call the function manually at the moment as it is not called automatically when the program starts, for some reason.

Now in a function of the same image, I want to use those properties:

Code: Select all

switch distance of me
      case 4
         set width of me to 0.25 * initialWidth of me
         set height of me to 0.25 * initialHeight of me
         break
    .....
end switch
It doesn't even enter the switch! The command it is in is definitely called though, but the switch is skipped.

And when I try to "answer" (btw. LiveCode really needs a console for tracing stuff like that without popups) any of the three custom properties directly after I initialize them, I only get the name of the properties and not a number.

Code: Select all

answer distance of me -- yields "distance"
answer initialWidth of me -- yields "initialWidth"
What's going on here?
Would I be better off using local variables for this? Tbh. I don't really get the difference between local variables and custom properties. The latter does not need to be declared in an extra step, it seems.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Using custom properties in calculations

Post by jmburnod » Mon Jun 03, 2013 11:09 am

Hi TheSheep,

I undersand you want use customprop to resize an image. I din't try your script but I think
you have to use THE before the name of a customprop as you made in the newimage script

you can do the same like that

Code: Select all

-- use round
   put round(the initialWidth of me / the distance of me) into tNewWidth
   put round(the initialHeight of me / the distance of me) into tNewHeight
   set width of me to tNewWidth 
   set height of me to tNewHeight 
Best regards
Jean-Marc
https://alternatic.ch

TheSHEEEP
Posts: 8
Joined: Mon Jun 03, 2013 10:13 am

Re: Using custom properties in calculations

Post by TheSHEEEP » Mon Jun 03, 2013 11:12 am

Hmm, I solved the problem using local variables for now.
It is probably better that way as they appear a lot less like magical black boxes ;)

Maybe I'll have to switch back to custom properties as soon I use multiple copies of the image (which all need different values).

But thanks anyway!

I thought "the" was a purely optional fluff word to beautify the code.

TheSHEEEP
Posts: 8
Joined: Mon Jun 03, 2013 10:13 am

Re: Using custom properties in calculations

Post by TheSHEEEP » Mon Jun 03, 2013 1:07 pm

I was right, I had to change back to custom properties as local variables are not copied when you copy an instance, it seems.
There I have my difference :D

And with the THE, it works.

I find that really unwise and unintuitive, from a language design point of view.
Either make a word like "the" completely optional or completely mandatory, but not a weird mixture like it is now ;)

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Using custom properties in calculations

Post by sturgis » Mon Jun 03, 2013 2:47 pm

Not sure what you mean about make it required or make it optionional.

If you're dealing with a custom property, "the" is required. If you're dealing with a variable, can't use "the".

Rather than "magical boxes" (I like the phrase) Think of them as cubbyholes attached to an object. "the width" is a cubbyhole that has a number in it. "The text" contains the text of an object. The text of field... Is a container/cubbyhole/compartment attached to a field.

Thinking of it this way you can start using properties as part of an "address." Hey livecode, I need the text of field 3 of card 2 of stack 4 can you get it for me? "get the text of field 3 card 2 stack 4"

Properties are very useful. Moving 25 objects around the screen each frame update? Since each object knows its location (which is a property) and size (which is a property) and you can add properties to the object such as vector info, you can always know where an individual object is, and where its going.

You might also look at getprop and setprop which allows some "preprocessing" to be done behind the scenes when a property is set or got.


If you haven't been there yet, lessons.runrev.com has lots of useful information. Quite a bit there about properties that might be helpful to know.

TheSHEEEP
Posts: 8
Joined: Mon Jun 03, 2013 10:13 am

Re: Using custom properties in calculations

Post by TheSHEEEP » Mon Jun 03, 2013 3:43 pm

I know most of that and what properties can be used for, but I was complaining about the inconsistency and confusion.

For example, you can write:

Code: Select all

get text of field "field1"
put it into field "field2"
No problem, everything works.

But you could also write:

Code: Select all

get the text of the field "field1"
put it into the field "field2"
Again, fully legal.
You end up with the impression that "the" is purely optional, for properties as well as anything else.

But suddenly, when dealing with custom properties, things go wrong when not using "the".

Code: Select all

switch distance of me -- won't work without giving an error
....
switch the distance of me -- works
Now, why should there be any difference between "X of" and "the X of"? It's not like there could be many "X of", and even if, which one would "the" specify?

And this is only the first case I encountered. Maybe there are even situations in which something only works without a "the"?

And there's more.
Variables - why distinguish between variables and properties, btw? They are the same in far more complex languages - cannot have a "the" written in front of them.
So you cannot write...

Code: Select all

put the thingVar into field "field1"
... if thingVar is a variable. At least this time, you get a proper error in the IDE.
Again, why? To be able to distinguish between variables and properties by how they are used?
The "of" already does that. Cannot have a "variable of object", correct?
And the "the" is optional even for properties (except custom properties where they are required).

In both the required and the will-break-if-used case, it seems like a very arbitrary decision was made concerning the usage of "the".

Guess this is the wrong forum, though ;)
Made a post here.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Using custom properties in calculations

Post by jacque » Mon Jun 03, 2013 7:14 pm

The proper syntax for your first example is "get the text of fld x". The engine is forgiving enough to figure out what you want if you omit "the" but the omission is one of my pet peeves for the reasons you describe. But you'll see it all the time because people prefer to type less.

If you're teaching, you should teach that "the" is required for all properties, both built-in ones (where the engine will cover for you if you omit it) and custom ones (where it's required.) "The" is never used for variables. The syntax is theoretically consistent, but not everyone follows the rules.

The difference between properties and variables is distinct. Properties are persistent and saved with the stack, variables are transient and exist only in RAM.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply