How do I create class in LiveCode?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

How do I create class in LiveCode?

Post by ChessGredon » Fri May 30, 2014 4:35 pm

How can I create class or something similar like structure (like in C#) in LiveCode?

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: How do I create class in LiveCode?

Post by magice » Fri May 30, 2014 4:58 pm

Livecode is much simpler than that. If I am not reading too much into your question, then I assume what you are really needing is to create new objects that inherit properties from another. Look up the clone command in the dictionary.

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: How do I create class in LiveCode?

Post by ChessGredon » Fri May 30, 2014 5:24 pm

And? How do I create new object? I want to do something very simple like to have a structure/class named "Players" with properties "name" "points" "age" and so on and to have some functions in this class. I looked up "clone" in the dictionary, but it's definitely not what I want to do.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: How do I create class in LiveCode?

Post by magice » Fri May 30, 2014 5:32 pm

ChessGredon wrote:And? How do I create new object? I want to do something very simple like to have a structure/class named "Players" with properties "name" "points" "age" and so on and to have some functions in this class. I looked up "clone" in the dictionary, but it's definitely not what I want to do.

are you referring to just a data structure? You could create an array that fits that description. Something like tPlayers[playerName][age]. You could also save this data as a custom property of the stack, card, group or object.

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: How do I create class in LiveCode?

Post by ChessGredon » Fri May 30, 2014 5:41 pm

Of course, everything is a data structure :) So arrays in LiveCode are not with usual index, but can be a string as well? I will read some information on arrays, but it seems to me that I can't write functions this way (like constructor, destructor etc.) Still, thanks a lot, man.

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: How do I create class in LiveCode?

Post by magice » Fri May 30, 2014 5:54 pm

ChessGredon wrote:Of course, everything is a data structure :) So arrays in LiveCode are not with usual index, but can be a string as well? I will read some information on arrays, but it seems to me that I can't write functions this way (like constructor, destructor etc.) Still, thanks a lot, man.
When it comes to functions in LC there is no reason to put them in a class. all code is already shared with all objects. If a function is called by an object, it first looks for the code in the object, then it's group (if grouped) then it's card and finally the stack until that function is found. Coming from C++ can be frustrating, because LC is so simple in comparison that it never feels quite like you did everything you should have to done. I still write things "the hard way" as I am often told by the LC experts.

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: How do I create class in LiveCode?

Post by ChessGredon » Fri May 30, 2014 6:46 pm

It's a lot easier for beginner to make object in C# and it's 100x times easier to read. For example:

Code: Select all

class Child
{
    private int age;
    private string name;

    // functions
    public SomeFunction()
    {
// actions
    }

}
It's extremely easy both to write and read, and you can easily access it. Now I can create Child with names for example "Martin" "Ivan" "Petar" and so on and if I want to sort Martin, I write Martin.Sort(). If I want to assign Martin's age, I write for example

Code: Select all

Martin.Age = 25
. Extremely simple. This way I know I sorted Martin. Using arrays is haotic and extremely difficult to read. This is one of the very few things where LiveCode is extremely weird and 100 times more complex than all other object-oriented languages (Java, C/C++ etc). If there is really no way to create an object and one should use an array, this is a huge drawback. I mean creating objects is one of the first things we learn about programming, no matter what programming language do we use. I hope someone who is really really into LiveCode explain how to create objects with properties set by the user. I'm sure there is a way to do this (after all, LiveCode's paradigm is object-oriented :) ), but how?

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: How do I create class in LiveCode?

Post by magice » Fri May 30, 2014 7:16 pm

ChessGredon wrote:It's a lot easier for beginner to make object in C# and it's 100x times easier to read. For example:

Code: Select all

class Child
{
    private int age;
    private string name;

    // functions
    public SomeFunction()
    {
// actions
    }

}
It's extremely easy both to write and read, and you can easily access it. Now I can create Child with names for example "Martin" "Ivan" "Petar" and so on and if I want to sort Martin, I write Martin.Sort(). If I want to assign Martin's age, I write for example

Code: Select all

Martin.Age = 25
. Extremely simple. This way I know I sorted Martin. Using arrays is haotic and extremely difficult to read. This is one of the very few things where LiveCode is extremely weird and 100 times more complex than all other object-oriented languages (Java, C/C++ etc). If there is really no way to create an object and one should use an array, this is a huge drawback. I mean creating objects is one of the first things we learn about programming, no matter what programming language do we use. I hope someone who is really really into LiveCode explain how to create objects with properties set by the user. I'm sure there is a way to do this (after all, LiveCode's paradigm is object-oriented :) ), but how?
I agree with you to an extent, but where LC differs, is that rather than think about the data structure first, you should think about what you want to do with it. For example, if you are looking for a simple record keeping app, you might make a card with text boxes "name", "age", "points" etc. laid out as you would want them displayed. Then you could clone that card. The card is essentially the class. Name the card the name of the player, and you could navigate the data by going to the card. Reference that data as field "Age" of cd "LittleJohny". This is only one very simplistic example, but the point is, with LC I find it easier to think about and build the interface first, and the data structure second.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: How do I create class in LiveCode?

Post by dunbarx » Fri May 30, 2014 8:16 pm

I am wondering if there is just a semantic issue here.

Objects can be created at any time, and any or all of their properties set either by script or via the appropriate templates.

But LC has a fixed number of object classes, and you would have to construct a "native" entirely new one with more low-level tools, like C. Do I understand you correctly? In other words, are you asking if you could create a rotating beanie as a native control?

Because if it is merely a matter of the first part of my post, this is straightforward in LC, utterly simple, and, forgive me, far easier to read:

set the backColor of btn "yourButton" to "red"
set the loc of btn "yourButton: to "100,100"

Craig Newman

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: How do I create class in LiveCode?

Post by ChessGredon » Fri May 30, 2014 8:41 pm

No, no, this is simply a change of a property. You are answering only to concrete situations. My question is simple: How do I create a class/structure in LiveCode like in every OOP language, which makes it easier to structure and manipulate data? Not how I can replace it in some rare situations like if I don't need methods (as magice suggested with arrays), but how do I do it in general case (or maybe I should ask is it possible at all).

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9834
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: How do I create class in LiveCode?

Post by FourthWorld » Fri May 30, 2014 10:08 pm

ChessGredon wrote:My question is simple: How do I create a class/structure in LiveCode like in every OOP language, which makes it easier to structure and manipulate data?
LiveCode is a procedural language rather than an OOP one, so while it does have many things in common with OOPs it doesn't attempt to provide them all.

This article explains some techniques for simulating OOP-like behavior, but personally I find it a bit optimistic for the ardent OOP fan looking for true OOP:
http://newsletters.livecode.com/novembe ... etter2.php

The author expand on those ideas at this Web site:
http://www.exformedia.se/downloads/oopengine

That said, there are ways to work with LiveCode that provide many of the same benefits of OOP, just through different means.

One of these is behaviors - you can see them at work in this Lesson:
http://lessons.runrev.com/m/4071/l/6501 ... -behaviors

Like OOP classes, behaviors can be chained to allow a form of subclassing, support overriding and overloading methods (which we Livecoders call "handlers"), and provide instance variables.

Unlike OOP, LiveCode doesn't (yet) support anything that can be used for abstract classes. LiveCode behaviors are attached to physical objects, which can be useful if you need persistence (just save the stack file they're in).

A useful thing about LC is that all objects have an unlimited number of custom property sets each of which has an unlimited set of custom properties. Custom properties are name-value pairs that work like associative arrays, in which the value can be textual or even binary, so you get rich, flexible storage options with LiveCode objects.

Custom properties are made even more flexible through getProp and setProp messages, triggers for overloading property access or even completely virtualizing access behavior or values.

The requirement to use physical objects for class instances makes them ideally suited for custom controls, but you can also use them for more conceptual entities, provided the number of such entities isn't going to overload memory requirements (several hundred or even a few thousand on a card can be fine, but tens of thousands suggest paging in an out as needed from a database, or using arrays).

Another difference with behaviors in LC over a true OOP language is that in an OOP you'd expect to have constructors and destructors defined within the class, but in LC a behavior script is only available to an already-instantiated object, and objects can't delete themselves if their own script is attempting the deletion. You can work around this by dispatching to the behavior object if you really need to keep constructors/destructors in the same place as the code your instances will use, but more commonly object creation and deletion is done elsewhere, such as a globally-available library or backscript.

For more on libraries, backscripts, behaviors, and how they all fit together in LC's message path, this may be helpful:

Extending the LiveCode Message Path
An introduction to using Libraries, FrontScripts, BackScripts and Behaviors
in RunRev's LiveCode Programming Language
http://www.fourthworld.com/embassy/arti ... _path.html


Bottom line: if you absolutely need OOP, any procedural language is likely disappoint. But if instead you merely like OOP and are also willing to explore procedural options within a flexible framework, you may find LiveCode unusually satisfying.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: How do I create class in LiveCode?

Post by ChessGredon » Sat May 31, 2014 7:44 am

This is really cool: http://newsletters.livecode.com/novembe ... etter2.php This replacement of OOP kinda reminds me of how I did it when I used JavaScript(by creating a function). Perhanps LiveCode is a prototype-based programming language http://en.wikipedia.org/wiki/Prototype- ... rogramming, unlike C for example which is class-based, which is still satisfying and won't change the fact that I like LiveCode. Thanks to all of you guys who spent time to answer me, it was not wasted time - I learned a lot about the more powerful arrays (compared to other languages) and about the philosophy of LiveCode :))

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”