Page 1 of 1

Problem with object cloning

Posted: Thu Apr 14, 2011 10:53 am
by kristo
I have created a simple form - one Button - one Image "Image1"
Our project requires that there are objects created dynamically on the card
at runtime.

For now I managed this by cloning an image.

This is my code for the button:

Code: Select all

on mouseUp
   
   local counter=0
   
   repeat with counter=1 to 5
      
      clone image "Image1"
      set the top of it to random(225)+10
      set the left of it to random(225)+10

    end repeat    
   end mouseUp      

It works as expected and creates 5 additional images, each similar to the original one.
The question is this: How can I prevent the clones from being permant. Is there a way
to make revolution clean up when I switch back to design mode?

Is there another way of creating controls at runtime?

Cheers
Kristo

Re: Problem with object cloning

Posted: Thu Apr 14, 2011 11:03 am
by Klaus
Hi Kristo,

"permant"??? You probably mean "permanent", right?

Well, why should LiveCode clean up in "edit" mode???
I wouldn't like that, to say the least :D

LiveCode does not distinguish if you created objects manually (by dragging from the Tools palette)
and creating objecty by script.

No, you need to take care of this, but in a standalone these cloned objects will of course NOT get saved,
unless you do this by script.

There is in fact another way to "create" controls at runtime (and in the IDE!)!
Check "create" in the dictionary and "the templateimage" (or templatebutton, templategraphic etc...)

You can set all supported properties for "the templateXXXX" BEFORE you create an object, so the newly created
object looks and behaves just like you want it to. Very handy :D


Best

Klaus

Re: Problem with object cloning

Posted: Thu Apr 14, 2011 11:12 am
by kristo
Well, I tried this, but when I try to load something into the newly created image, nothing happens.

Code: Select all

 
   create image "Image2"
   put  "c:\temp\wood.jpg" into tImageURL
   put URL tImageURL into image "Image2"
And how on earth do I remove the newly created objects.
Do I have to put them in a group first?
Or is there another way to remove single objects?

Regards
Kristo

Re: Problem with object cloning

Posted: Thu Apr 14, 2011 11:29 am
by Klaus
Hi Kristo,

well, now YOU are the developer and need to take care of EVERYTHING, get used to it :D
There is the "delete" command, which you could use in a repeat loop.

You want to put the BINARY data of the image file into the image,then you need to:
...
create image "Image2"
put "c:/temp/wood.jpg" into tImageURL
put URL("BINFILE:" & tImageURL) into image "Image2"
...

Or just:
...
set the filename of img "Image2" to "c:/temp/wood.jpg"
...

Hint:
Internally LiveCode always uses the UNIX path delimiter SLASH -> /
for crossplatform compatibility.



Best

Klaus

Re: Problem with object cloning

Posted: Fri Apr 22, 2011 2:59 am
by Steve Denney
Hi Kristo, I do this sort of thing all the time. And yep, the answer is a repeat loop which I run at openStack or preOpenStack. If the new objects are going to be created in the build stack or you don't save that stack once it's distributed you can remove the code (or not) when you're ready to do the final build.

Anyway, in this case something like:

repeat with i = the number of imgs down to 2
delete img i
end repeat

So you start with just the template each time you open your stack to start working on it.
Steve

Re: Problem with object cloning

Posted: Fri Apr 22, 2011 11:41 am
by kristo
Thanks guys,

That helped me a lot.
I guess I have to get used a lot more to the way Runrev works before I am really at home with this development system.

Cheers
Kristo

Re: Problem with object cloning

Posted: Sat Apr 23, 2011 5:02 am
by Steve Denney
Yeah, you can get into trouble with a simple number-based reset loop though. Like, you'll find you add stuff, forget to change the loop 'count down to' number then delete the work you've just done, or change the layering and lose your templates. So what I tend to do nowadays is name the permanent template objects according to some scheme i.e. "kObj-" & whatever it's usual name is, (k for keep object :) ) then the loop becomes:

on cleanUp
repeat with i = the number of flds down to 1
if "kObj-" is not in the short name of fld i then
delete fld i -- (or put empty into...)
end if
if "kObj-" is not in the short name of img i then
delete img i
end if
-- and so on
end cleanUp

Perhaps there's a more elegant way of doing this but it works :)

Steve

Re: Problem with object cloning

Posted: Sat Apr 23, 2011 5:06 am
by Steve Denney
Oops, that script was fubar. Clearly, each object type needs it's own repeat loop counting down from the number of objects to 1.
Too much cofee.
Steve

Re: Problem with object cloning

Posted: Sat Apr 23, 2011 9:46 am
by jmburnod
Hi Steve,

You can use also the "control" keyword

Code: Select all

on CleanUp pKeep
   put the num of controls of this cd into nb
   repeat with i = nb down to 1
      if pKeep is not in the short name of control i then
         delete control i of this cd
         wait 1 milliseconds
      end if
   end repeat
end CleanUp
Be careful about group : the name of a group and each control of the group must contain pKeep if you want keep them

All the best

Jean-Marc

Re: Problem with object cloning

Posted: Sat Apr 23, 2011 1:49 pm
by shaosean
For a little more advanced scripting, you can always do your cleanup during the compile time by using the savingStandalone message..

Re: Problem with object cloning

Posted: Sat Apr 23, 2011 9:51 pm
by Steve Denney
Thanks guys, I didn't know about either of those keyWords.
So you can even refer to consecutive controls by number. Needs pKeep in the name of absolutely every control (but why not?) :)
Steve

Re: Problem with object cloning

Posted: Sat Apr 23, 2011 11:58 pm
by shaosean
You can also assign a custom property to the objects if that is cleaner for you..

Re: Problem with object cloning

Posted: Sun Apr 24, 2011 2:17 am
by Steve Denney
Hi shaosean, at the risk of hijacking Kristo's thread, (& only if it's easy to answer) how would one do that? I've been using rev since 2.2 and, must admit, custom properties are still a complete mystery to me.
Steve

Re: Problem with object cloning

Posted: Sun Apr 24, 2011 6:44 am
by shaosean
The use of a custom property is the same as using built-in properties, except you make up the names :-)

Example of built-in property

Code: Select all

set the text of field "hello" to "Hello World"
You are setting the text property of the field to Hello World

Example of custom property

Code: Select all

set the cloned of field "hello1" to "true"
You are setting the cloned property (this is a custom property) of the field to true

So you can test against it just as you would a built-in property

Code: Select all

if (the cloned of field "hello1" = "true") then
  # do something
end if
If you need further assistance, lets make a new topic so it will be easier for people to find in the future

Re: Problem with object cloning

Posted: Sun Apr 24, 2011 9:36 pm
by Steve Denney
Sounds good, thanks, and very useful, (certainly perfect for this sort of reset). I'll check it out :)
Steve