How do I create class in LiveCode?
Posted: Fri May 30, 2014 4:35 pm
How can I create class or something similar like structure (like in C#) in LiveCode?
Questions and answers about the LiveCode platform.
https://forums.livecode.com/
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.
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 wrote:Of course, everything is a data structureSo 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.
Code: Select all
class Child
{
private int age;
private string name;
// functions
public SomeFunction()
{
// actions
}
}
Code: Select all
Martin.Age = 25
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.ChessGredon wrote:It's a lot easier for beginner to make object in C# and it's 100x times easier to read. For example:
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 exampleCode: Select all
class Child { private int age; private string name; // functions public SomeFunction() { // actions } }
. 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-orientedCode: Select all
Martin.Age = 25
), but how?
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.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?