How to make a new class?

About advanced scenes, and the Thyme scripting language used in Algodoo.

How to make a new class?

Postby Fisher » Wed Jul 04, 2018 1:19 am

In my scenes, I am starting to use more and more global variables. I wonder if it is possible to create a new class so they wouldn't clutter up the namespace.
Fisher
 
Posts: 2
Joined: Wed Jul 04, 2018 1:08 am

Re: How to make a new class?

Postby Xray » Wed Aug 01, 2018 2:25 am

scene.my.variableName is the only way I know of to declare and to use global variables. And the odd behavior which is different from C++ and other well known languages is you can store any type of data in any class of variable at any time! So, for example, if an integer is stored in a particular variable, you can write over that integer with a float, a Boolean, or text without complaints from the interpreter! You can also mix data types in arrays.

Hope this helps.
User avatar
Xray
 
Posts: 484
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: How to make a new class?

Postby FRA32 » Tue Aug 21, 2018 9:14 pm

Unfortunally it is impossble to create new classes, HOWEVER you can actually get an existing class, store a copy of it, and then use that copy as your class. The only annoying side effect is that whatever fields were in the class are stuck in there, and I got the feeling that the classes actually collapse if you reload the scene. Try using an object's onDie event and do "scene.my.newclass = e", that will copy the class type object e from the (e)=>{ over to your variable, which can then be used as a kind of namespace. I would recommend also overriding "e.this" with undefined in order to clear the object data, which would just clutter up unneccesary space.
FRA32
 
Posts: 213
Joined: Wed Dec 03, 2014 9:51 pm

Re: How to make a new class?

Postby Xray » Wed Aug 22, 2018 8:07 am

FRA32 -- I understood that "class" was used only in object oriented program languages such as C++. Therefore, it doesn't make sense to try to create classes in Thyme scripting because it is not an object oriented programming language. Am I correct?
User avatar
Xray
 
Posts: 484
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: How to make a new class?

Postby FRA32 » Wed Aug 22, 2018 4:17 pm

That is only partially true. The thing is that there's the variable types that are accessible to us, i.e. int, bool etc., and then there's another variable type only creatable trough intrinsic ways, so in other words from within the source code of algodoo. This variable type is called "class object" according to one of the console errors when trying to create it, and it has the structure you see when you try to store entityByGeomId in a variable. Class object variables contain:

A kind of object description, which is a literal text assigned directly to the variable holding the class.
A key-value kind of data space, containing keys(so other variables) having more values assigned.

The object description can only be seen when converting a class object to a string. When you convert an "e" value to a string, you get the aforementioned description, which is "ClassObject with children:", followed by the contents of e. If you convert e.this/e.geom etc. to a string however, you only get the description, which is "box", "plane" etc., which is the reason why we can use e.geom+"" != "plane" ? {} : {}

I suspect that the toString is defaulted to print only the descriptor(i.e. "Box", "Laser", or "GUI" if you converted app.GUI), and the event variables are additionally configured to print their contents in this case.
ClassObjects are of Intrinsic origin, thus having special properties that are not influenced by thyme, however we get them passed to thyme for us to use in scripting, thus giving us variables with partially perplexing behavior, since the content of these variables is not thyme-defined, but can be interacted with nontheless, including reading/writing to them.
FRA32
 
Posts: 213
Joined: Wed Dec 03, 2014 9:51 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest

cron