Page 1 of 1

Naming new objects on runtime

Posted: Tue Apr 23, 2013 4:48 pm
by ColeBKray
Hi,

What's the solution to give unique names to objects created at runtime, so I can easily refer to them?

I have a Button with the following script, but it doesn't work:

Code: Select all

global POV, POVNum
POVNum = 1
POV = "POV" + POVNum

on mouseUp
   move button addnew relative 0,30
   create field "NewField"
   set the name of field "NewField" to POV
   put 1 + POVNum into POVNum
   end mouseUp
The script misses the code that will change the location of the new field, but that's not the point.

Can anybody help me?

Thanks.

Re: Naming new objects on runtime

Posted: Tue Apr 23, 2013 5:25 pm
by FourthWorld
When you use the "create" command to create a new object, the long ID of the new object is in the local variable "it".

Re: Naming new objects on runtime

Posted: Tue Apr 23, 2013 5:39 pm
by ColeBKray
And how can I get access to these long IDs globally, later on?

I mean I have to know how livecode names these long IDs, make them global, and access them later in code.

How do I do that?

Thank you.

Re: Naming new objects on runtime

Posted: Tue Apr 23, 2013 6:16 pm
by jmburnod
Hi coleBKray,

Two ways to do this if I understand correctly
1. using global
2. using function

card script

Code: Select all

global POVNum
on preopencard
    --•• use global
   --put 1 into POVNum -- only for global use
end preopencard

on DebCreateFld
   --move button addnew relative 0,30 -- 
   create field "NewField" 
   
   --•• use global
   --put "POV" & POVNum into tNameFld
   --put 1 + POVNum into POVNum
   
   -- •• use  TheNextNum() function
    put "POV" & TheNextNum("POV") into tNameFld
   set the name of it to tNameFld
end DebCreateFld

function TheNextNum pName
   repeat with i = 1 to the num of flds
      put pName & i into tFld
      if there is not a fld tFld then
         put i into rTheNextNum
         exit repeat
      end if
   end repeat
   return rTheNextNum
end TheNextNum
Button script

Code: Select all

on mouseup
   DebCreateFld
end mouseup
Best regards
Jean-Marc

Re: Naming new objects on runtime

Posted: Tue Apr 23, 2013 10:16 pm
by Simon
Hi ColeBKray,
A couple of things giving you troubles here:

Code: Select all

global POV, POVNum
POVNum = 1
POV = "POV" + POVNum
The first is POV = "POV" + POVNum, you are using an arithmetic function on a string. I think what you wanted is POV = "POV" & POVNum which will give you "POV1"
The second is if you just write this code out and hit the button, POV has nothing in it the first time.
Here is a working script:

Code: Select all

global POV, POVNum

on mouseUp
   --move button addnew relative 0,30
   add 1 to POVNum
   put "POV" & POVNum into POV
   create field "NewField"
   set the name of last field to POV
end mouseUp
About the move... The name is a string so:
move button "addnew" relative 0,30
is better.

Simon

Re: Naming new objects on runtime

Posted: Wed Apr 24, 2013 10:52 am
by ColeBKray
Thanks for both of you :)