Page 1 of 2
Create a class
Posted: Fri Nov 20, 2015 11:32 am
by problème
Hello,
I would like to create a class "Personnage" with as parameters "lp,xp"
Code: Select all
global lifePoint
function Personnage lp,xp
put lp into lifePoint
put xp into expérience
end Personnage
then i create a getter for lp
but now how i create a object "Character" ?
i do this but i got a error at line three, (i think i must not use a function like this)
Code: Select all
on preOpenStack
create field "lp"
put Personnage("Yolo,100,5") into héros
put the getLp of the Personnage "héros" into the field "lp"
end preOpenStack
Re: Create a class
Posted: Fri Nov 20, 2015 12:55 pm
by Klaus
Bonjour problème,
1. "character" is a reserved word in LC!
2.
function Character lp,xp
...
end Personnage
A handler/function must have the same ending name as its beginning!
3.
...
put the getLp of the Personnage "héros" into the field "pv"
...
Sorry, does not make much sense?
4. Do not use THE when addressing objects in LC, use it only for properties:
...
put xyz into fld "xzy"
set THE cCustomProperty of fld "xzy" to "abc"
...
Sorry, not being familiar with OOP, I cannot be of more help...
Best
Klaus
Re: Create a class
Posted: Fri Nov 20, 2015 3:03 pm
by dunbarx
Probleme.
You are moving very quickly through LiveCode. This is good, but as Klaus pointed out, there are missing or misapplied details in almost all the points you made in your post. These involve the very basics of the language.
It is a good way to learn, to try and to fail, and see what went wrong. Please write back here as often as you need to. Each problem will be cleared up, and you will soon learn how to drive.
Craig Newman
Re: Create a class
Posted: Fri Nov 20, 2015 5:01 pm
by problème
Sorry i did some careless mistake ^^
but how i create object Personnage
:héros, i don't understand it.
Code: Select all
global lifePoint
put Personnage("Yolo,100,5") into héros
put lifePoint into the field "lp"
there is nothing displayed into field "lp"
(Can i do something like that : put héros.lifePoint and it prints 100)
Re: Create a class
Posted: Fri Nov 20, 2015 5:44 pm
by dunbarx
Hmmm.
put Personnage("Yolo,100,5") into héros
Here, "Personnage" is formed as a function call. Do you know that? And because you have placed the parameters of the call in quotes, you would have to separate them in the function handler. It is not likely that this is what you intended. So did you mean something like this?:
Code: Select all
on mouseUp
put Personnage(Yolo,100,5) into héros
answer héros
end mouseUp
function personnage param1,param2,param3
return param1 && param2 + param3
end personnage
And I am still not sure what you mean by "create object...." Do you? Creating an object is entirely different than putting data (the variable "lifePoint") into a field. This could be just a language issue.
there is nothing displayed into field "lp"
There should be. Is the variable "lifePoint" empty? Try this:
Code: Select all
put "XYZ" into lifePoint
put lifePoint into fld "lp"
Anyway, please do not use "the" before an object reference, as Klaus pointed out. This is a much smaller issue, however. Write back...
Craig
Re: Create a class
Posted: Fri Nov 20, 2015 6:30 pm
by Klaus
problème wrote:but how i create object Personnage :héros, i don't understand it.
As I wrote, I have only a vague idea of what OOP can do, so I don't know what you are trying to do,
but this is not how Livecode works!
Re: Create a class
Posted: Fri Nov 20, 2015 6:57 pm
by FourthWorld
LiveCode is object-based, but not object-oriented in any pure OOP sense. It provides many of the same benefits as OOP, but doesn't provide all of them and accomplishes things through different means.
For example, you can use LiveCode objects to create a sort of class-like control set, and using behavior scripts you can define all of the handlers ("methods" in OOP parlance) in one central script to govern all instance objects, and the objects have their own instance variables and custom properties. And like OOP behaviors can be nested to effectively allow you to define things similarly to subclasses in OOP. But among the things LiveCode doesn't currently support is abstract classes, classes that have no physical on-screen representation.
Since you've already started down the road of using a field to represent your "class", your desires and LiveCode's capabilities may be suited for each other well enough once you get a few syntactic details under your belt (like using "the" to specify properties but not when referring to objects).
These lessons on using behaviors may help get you going - feel free to chime in here with questions as you explore them:
http://lessons.livecode.com/m/4071/c/16766
Re: Create a class
Posted: Fri Nov 20, 2015 7:47 pm
by Newbie4
If you want to draw parallels to typical object oriented languages (e.g. Java), then consider the template"objects" (e.g. templateButton, templateGraphic, etc) equal to the "class" and the "create" commands as the "constructors".
Look them up in the dictionary, see if that makes sense
Re: Create a class
Posted: Fri Nov 20, 2015 8:39 pm
by quailcreek
problème,
Perhaps you could provide an explanation of what you're trying to accomplish rather than how you want it done. That way we could point you in the direction as to how Livecode can get you there.
Re: Create a class
Posted: Fri Nov 20, 2015 9:43 pm
by dunbarx
QuailCreek.
Well said. Probleme is deeply immersed in both learning and doing. Pulling back a bit for reflection will get both moving more smoothly.
Craig
Re: Create a class
Posted: Fri Nov 20, 2015 11:14 pm
by Newbie4
In many object-oriented languages, a class is a template that is loaded when the program starts up. It is an abstract entity. The program uses that template to create new objects and initialize the properties.
In LiveCode, you have templates which are like classes. They are also abstract and are only used to create new objects. There are templates for every object/control (eg. templateButton, templateGraphic, templateCard, templateStack, templateGroup, etc). They do not exist but they have all the properties of real ones. By setting the properties of a template, you specify how the objects are created and initialized.
This may be what you were looking for:
Code: Select all
on openCard
// set the default values of the template (class)
set the fillcolor of templategraphic to red
set the lifepoint of templateGraphic to "xx"
set the experience of templateGraphic to 0
end openCard
command Personnage lp,xp
//this creates the object from the template (class)
set the lifepoint of templateGraphic to lp
set the experience of templateGraphic to xp
end Personnage
on mouseDown
// create a Graphic from the template
create graphic normal
// create a Graphic from the template, with new values
Personnage "Yolo" ,5
create graphic heros
// create another Graphic with the same properties
create graphic heros2
create graphic heros3
//change the template back to the original defaults
reset the templateGraphic
end mouseDown
notes:
- I used graphic as an example. You can use any LiveCode object or controls (See the dictionary for more details)
- fillcolor is a normal property of Graphics
- lifePoint and experience are custom properties (yours)
If this is not what you were looking for, then follow quailcreek's advice above and give us a more general idea of what you want to accomplish
Re: Create a class
Posted: Sat Nov 21, 2015 12:02 am
by dunbarx
I know less about OOP than Klaus.
But I initially thought that Probleme was trying to create an entirely new object class, a la widgets, sort of.
Craig
Re: Create a class
Posted: Sat Nov 21, 2015 2:41 am
by Newbie4
Wouldn't that just be a group?
I do not know that much about widgets yet, to comment about them. I would think that they would still have properties and behaviors.
You may be right.
Re: Create a class
Posted: Sat Nov 21, 2015 4:04 pm
by richmond62
Vos idées préconçues d'autres langues sont font les choses difficiles.
Re: Create a class
Posted: Sat Nov 21, 2015 4:44 pm
by dunbarx
Justement, Richmond.
Craig