Creating custom objects

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
backbone
Posts: 3
Joined: Tue Dec 01, 2009 1:57 pm

Creating custom objects

Post by backbone » Tue Dec 01, 2009 2:03 pm

Hi all,
I just started using Rev.
I read all the tutorial and found the language very smart and exciting.
My first application is fat to be simple but I found that Rev handles all the issues very well.
However I really do not have any idea on how to create a custom object.

For example in all OO languages you can create something like this :
"class Car{
property model;
property color;
.....
method getColor()
method setColor() ....
}

ok ... I know how to create functions but what about the Object and property declaration?
Maybe I missed something? :roll:

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Re: Creating custom objects

Post by Janschenkel » Wed Dec 02, 2009 8:06 am

During the day, I spend time in Progress OpenEdge ABL, Java and revTalk -each language has its own degree of object-orientation. So let me see if I can make a good comparison for you...

revTalk is object-based but not object-oriented. There is a limited set of base objects: button, field, scrollbar, graphic, image, group, card and stack. You can only inherit 1 level deep from these base objects to specialize them, and then you cannot override their paint or draw method. This may sound extremely limiting if you're used to squeezing every last bit out of Java or C# - but the underlying framework with its ChainOfCommand implementation (message path in rev parlance) and plain english programming language more than make up for the limitations.
In fact, given the recent evolution in OO programming towards DCI (Data-Context-Interaction with 'dumb' data objects and 'roles' to express the business logic in an almost-proecdural way) you could say that the way rev works better matches our 'natural' way of thinking than off-the-shelf Java. Once you get past the initial hurdles and learn to appreciate the rev paradigm, you can become very productive.
Anyway, enough programming philosophy :-)

You've already discovered how to add methods in a script, writing on, command, and function handlers. Property handling is quite open: any script can set any property on another object, so if you want to check their content upon set or add a property that returns a combination of fields, you'll have to do that by writing setProp and getProp handlers.
If I give a button "TestObject" the following script:

Code: Select all

on mouseUp
  beep the uBeepTimes of me
  answer FatFirstLine(the uAnswerText of me)
end mouseUp

function FatFirstLine pText
  local tIndex, tReturnText
  put 0 into tIndex
  repeat for each line tLine in pText
    add 1 to tIndex
    if tIndex = 1 then
      put "<p><b>" & tLine & "<b><p>" after tReturnText
    else
      put "<p>" & tLine & "<p>" after tReturnText
    end if
  end repeat
  retutn tReturnText
end function

setProp uBeepTimes pNewBeepTimes
  if pNewBeepTimes is not an integer or pNewBeepTimes is < 1 then
    throw "uBeeptimes must be an integer >= 1"
  end if
  -- it is ok, so allow the property to be set
  pass uBeepTimes
end uBeepTimes
You can change the number of times it beeps, and which text it displays, by setting its uBeepTimes and uAnswerText properties:

Code: Select all

set the uBeepTimes of button "TestObject" to 9
set the uAnswerText of button "TestObject" to "Hello, world!" & return & "Every day that you learn something new, is a wonderful day..."
Click the button and it will obey.
But if you try to set the uBeepTimes property to something wrong:

Code: Select all

set the uBeepTimes of button "TestObject" to "smurf"
an error will be thrown, and the new property content is not set.

So you can always write objects that are, in fact, invisible controls. If you need containment, you can create a group with tis own script, and then other controls inside that group. It is stretching things a bit, as you'll still have to refer to these custom objects as 'group "Order_1"' with its 'button "OrderDetail_12"' but it is one way to accomplish custom objects.
It would take up too much time to explain everything in minute detail, but now you have some ideas on how you can 'specialize' existing objects. Make sure to read this excellent article on the message path. Many people also adopt this scripting style guide for organizing their scripts.

HTH,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

backbone
Posts: 3
Joined: Tue Dec 01, 2009 1:57 pm

Re: Creating custom objects

Post by backbone » Thu Dec 03, 2009 9:42 pm

great response .... many thanks I'll take a look at the articles you linked .. :D

ale870
Posts: 250
Joined: Tue Apr 22, 2008 9:17 am
Contact:

Re: Creating custom objects

Post by ale870 » Fri Dec 04, 2009 9:36 am

Hello,

about objects creation, you can even check the commands "copy", "clone", "create", "delete".
In fact you can imagine that your "OO class" could be a button, that you previously create like a template (your class). Then you can use "copy" command to create an "instance" of that object, and setting its properties as @Janschenkel already told you.

Cheers!
Runtime Revolution Widgets, tutorials, tips & tricks and more!
http://runrevwidgets.com/wiki

backbone
Posts: 3
Joined: Tue Dec 01, 2009 1:57 pm

Re: Creating custom objects

Post by backbone » Fri Dec 04, 2009 11:50 am

Thanks ale .. I'll do that ... however I think the absence of "real objects" will cost me a little "brain effort" in order to adapt as I'm used to program daily OO languages.
But each language has pros and cons and must be used "as is" ;-)
I love this language for other reasons and this is why I bought the EE edition.

Post Reply