Naming new objects on runtime

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Naming new objects on runtime

Post by ColeBKray » Tue Apr 23, 2013 4:48 pm

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.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Naming new objects on runtime

Post by FourthWorld » Tue Apr 23, 2013 5:25 pm

When you use the "create" command to create a new object, the long ID of the new object is in the local variable "it".
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Re: Naming new objects on runtime

Post by ColeBKray » Tue Apr 23, 2013 5:39 pm

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.

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

Re: Naming new objects on runtime

Post by jmburnod » Tue Apr 23, 2013 6:16 pm

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
https://alternatic.ch

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Naming new objects on runtime

Post by Simon » Tue Apr 23, 2013 10:16 pm

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

ColeBKray
Posts: 9
Joined: Tue Oct 16, 2012 5:39 pm

Re: Naming new objects on runtime

Post by ColeBKray » Wed Apr 24, 2013 10:52 am

Thanks for both of you :)

Post Reply