Create a class

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

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Create a class

Post by problème » Fri Nov 20, 2015 11:32 am

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

Code: Select all

getProp getLp
   return pv
end getLp
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
Last edited by problème on Fri Nov 20, 2015 4:34 pm, edited 3 times in total.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Create a class

Post by Klaus » Fri Nov 20, 2015 12:55 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Create a class

Post by dunbarx » Fri Nov 20, 2015 3:03 pm

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

problème
Posts: 77
Joined: Fri Oct 23, 2015 12:03 am

Re: Create a class

Post by problème » Fri Nov 20, 2015 5:01 pm

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)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Create a class

Post by dunbarx » Fri Nov 20, 2015 5:44 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Create a class

Post by Klaus » Fri Nov 20, 2015 6:30 pm

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!

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Create a class

Post by FourthWorld » Fri Nov 20, 2015 6:57 pm

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
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Create a class

Post by Newbie4 » Fri Nov 20, 2015 7:47 pm

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
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

quailcreek
Posts: 746
Joined: Sun Feb 04, 2007 11:01 pm

Re: Create a class

Post by quailcreek » Fri Nov 20, 2015 8:39 pm

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.
Tom
MacBook Pro OS Mojave 10.14

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Create a class

Post by dunbarx » Fri Nov 20, 2015 9:43 pm

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

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Create a class

Post by Newbie4 » Fri Nov 20, 2015 11:14 pm

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
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Create a class

Post by dunbarx » Sat Nov 21, 2015 12:02 am

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

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: Create a class

Post by Newbie4 » Sat Nov 21, 2015 2:41 am

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.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 10100
Joined: Fri Feb 19, 2010 10:17 am

Re: Create a class

Post by richmond62 » Sat Nov 21, 2015 4:04 pm

Vos idées préconçues d'autres langues sont font les choses difficiles.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Create a class

Post by dunbarx » Sat Nov 21, 2015 4:44 pm

Justement, Richmond.

Craig

Post Reply