Page 1 of 1
using a variable for a custom property name
Posted: Mon Oct 13, 2014 3:25 pm
by Da_Elf
This might seem like a weird one but id like to do something like this but it doesnt work...
Code: Select all
put 1 into advan
repeat for 5 times
put "Item"&advan into customName
set customName of me to "X"
add 1 to advan
end repeat
The result im looking for is 5 custom properties listed as Item1, Item2, Item3, Item4, Item5
Re: using a variable for a custom property name
Posted: Mon Oct 13, 2014 3:29 pm
by Klaus
Hi Da_Elf,
THE, the magic word for creating/accessing c_props is THE, set/get THE custompropname
This works:
Code: Select all
...
put 1 into advan
repeat for 5 times
put "Item" & advan into customName
set THE customName of me to "X"
add 1 to advan
end repeat
...
Best
Klaus
Re: using a variable for a custom property name
Posted: Mon Oct 13, 2014 3:31 pm
by Thierry
Da_Elf wrote:
The result im looking for is 5 custom properties listed as Item1, Item2, Item3, Item4, Item5
What about this?
Code: Select all
repeat with J=1 to 5
get "myCP_" & J
set the IT of me to J
end repeat
Thierry
Re: using a variable for a custom property name
Posted: Mon Oct 13, 2014 4:00 pm
by Da_Elf
actually i went one better on it and created a new set of custom keys so i didnt need to use "THE" (i dont use "the" when setting properties but i use it when getting them)
set uStuff[variablename] of me to "X"
this gives me to power to delete the group of custom properties from "me" when im finished with them without disturbing the other normal custom properties
Re: using a variable for a custom property name
Posted: Mon Oct 13, 2014 4:27 pm
by Klaus
Da_Elf wrote:... (i dont use "the" when setting properties but i use it when getting them)...
Yep, that's what Hermann wrote, but that does not make it officially correct PER SE!

Re: using a variable for a custom property name
Posted: Mon Oct 13, 2014 11:03 pm
by [-hh]
"The" is optional before properties.
That's a law of the engine, not by me. But Klaus, just to please you, I added line 2 in the first script below (uncomment it after using it once).
Seeing this horrible bug I admit, that it is better not to use "empty", "it", "custom" or other popular LC names as custom property names. And to use "the", because it's better to understand. (But not "teh", as I often do; Simon, your influence.)
Code: Select all
-- May behave different with diff LC versions !!
on mouseUp
set customName of me to "I'm your customName."
-- put "Hermann works hard to prepare his next error." into customName
# Bug (?) The line above replaces property customName of me
answer the customName of me # <-- "the" is superfluous but my style
end mouseUp
Another crazy example that works:
Code: Select all
on mouseUp
set 7.0 of me to "I dreamt to be 8.0"
end mouseUp
Look in the object inspector to see, that the property is set.
Because we are in the beginner-subforum I don't say how to get this property, because "get the 7.0 of me" doesn't work.
Anyway, nice trick of LC to "hide" something that we are getting here.
[Edit: Changed example above to avoid the special case of it. I saw too late, that LC behaves different with different versions and dependent on whether properties are set a first time or not, say: Watch what is in RAM while you use 4 different stacks named "Untitled 1". Sorry.]
Re: using a variable for a custom property name
Posted: Mon Oct 13, 2014 11:16 pm
by dunbarx
Hermann.
It works, but it also does not work;
Code: Select all
on mouseUp
set the 7.0 of me to "I dreamt to be 8.0"
answer the 7.0 of me
end mouseUp
Try it. What happens is surprising. Can a line both execute, giving an answer, and also fail to execute? Set aside for a moment that the answer is wrong. I think Schrodinger's cat is hidden in the code.
Craig
Re: using a variable for a custom property name
Posted: Mon Oct 13, 2014 11:39 pm
by [-hh]
Hi Craig.
The engine complains when you try to get the property. Setting depends on LC version.
LC 6.6.3 sets the property only without "the", LC 7 always (because it's dreaming).
Obviously the engine uses different ways to handle the property names when getting and setting.
This is for my advantage, because I often write erraneously "the item 1 of strg" and "the word 3 of strg" when Im testing several approaches. And such "the" are accepted.
Here is moreover the special case of a number that has to be converted to a string (what always implies some evaluation?).
Perhaps one of the engine contributors can explain this, I have no oversight on source.
Craig wrote: ... Schrodinger's cat is hidden in the code ...
Which state would you define as "life" ?
Hermann