Page 1 of 1

Construct object name from string

Posted: 16 Jan 2017 21:46
by daviddeskew
Can this be done?

Suppose I have 50 labels with names like label1, label2, ... label50

I would like to set an attribute on each label and obviously I would prefer not to write

setattribute....

50 times

Ideally I'd like a for loop (say) where I could create the string name for each object, e.g.

for (i = 1; i<=50; i++)
setattribute('label' + inttostr(i), 'content', 'x'); // Set the caption for each label

However, the basic experiment,

setattribute('label1', 'content', 'a')

fails, presumably because setattribute wants an object name as the first parameter.

So how does one create an object name from a string? (And I assume there's a function that converts an integer to a string as well)


Thanks

Re: Construct object name from string

Posted: 16 Jan 2017 23:38
by oldgearguy
integer to string:

Code: Select all

strNum = '' + 1; 
strNum will now contain '1'

attached is one way to create and reference dynamic object names

One trick is to create the object name as a string and then use findobject() to get the object reference to use in setattribute(). I'm sure I've used other ways as well, but that one is fairly straightforward.

Re: Construct object name from string

Posted: 16 Jan 2017 23:50
by daviddeskew
Got it --- all makes sense.

One last question if I may.....

I haven't been able to figure out the difference between Properties and Attributes as specified for the various objects so I don't know how to change things programatically.

I understand the setattribute function (have used it with the attribute 'content' to change labels) but how do I change the foreground/background color of a knob.

Neither setattribute(knob, 'Foreground', RGB(..)) nor
knob.Foreground = RGB(...)

works

What am I missing here?

Thanks so much for your responses.

Re: Construct object name from string

Posted: 17 Jan 2017 00:35
by oldgearguy
this should work:

Code: Select all

setattribute(Knob, 'color', {RGB(0,1,1),RGB(0,0,1)});
note that you can't set foreground and background separately in most cases; you need to set color to an array of 2 values for foreground and background.

Re: Construct object name from string

Posted: 17 Jan 2017 02:18
by daviddeskew
Wow --- is that syntax documented anywhere? I did not notice that in the documentation anywhere.

Much appreciated.

Re: Construct object name from string

Posted: 17 Jan 2017 11:02
by oldgearguy
daviddeskew wrote:Wow --- is that syntax documented anywhere? I did not notice that in the documentation anywhere.

Much appreciated.
Kind of - taking the Knob as an example, on page 98 the attribute color is shown as an array of 2 values. The earlier text somewhere (don't have it now) talks about arrays (they call them vectors) using curly braces.
I know they call out in the Properties Foreground and Background, but in my experience i haven't always been able to directly address Properties with any success. There are functions for expressions and attributes (get and set) and variables are directly addressed via the dot notation (Knob.x).

Note that in some cases you have a color attribute and a colors attribute. The color is general foreground/background (or on/off) and the colors is an array (vector) of color information for every object of that type defined. For example, if you declare a 2x2 grid of pads you can use the colors attribute to set the off color of each pad to be something different.

Re: Construct object name from string

Posted: 18 Jan 2017 16:35
by Phil999
thank you for this clear description. I somehow manage to do things, but often have to refer to older projects or examples from the library; not knowing exactly how and why things work or don't work. Now it has become more clear to me.

Re: Construct object name from string

Posted: 18 Jan 2017 17:44
by oldgearguy
Phil999 wrote:thank you for this clear description. I somehow manage to do things, but often have to refer to older projects or examples from the library; not knowing exactly how and why things work or don't work. Now it has become more clear to me.
you're welcome. I often try to go back to larger projects I've written looking for examples, but sometimes I get lost in the weeds.
Creating simple examples and then working variations on it until I understand what Lemur is actually doing tends to help it stick in my head.

What I really need to do is create a compendium of small examples showing off these less-than-perfectly documented techniques because they really are building blocks for larger and more complex projects.

Re: Construct object name from string

Posted: 15 Feb 2017 23:50
by nick_liine
oldgearguy wrote:
Phil999 wrote:thank you for this clear description. I somehow manage to do things, but often have to refer to older projects or examples from the library; not knowing exactly how and why things work or don't work. Now it has become more clear to me.
you're welcome. I often try to go back to larger projects I've written looking for examples, but sometimes I get lost in the weeds.
Creating simple examples and then working variations on it until I understand what Lemur is actually doing tends to help it stick in my head.

What I really need to do is create a compendium of small examples showing off these less-than-perfectly documented techniques because they really are building blocks for larger and more complex projects.

Please get in touch if you already have an existing list of these, this could perhaps on the documentation wiki or at least serve as a guideline for which examples are most needed.