user objects with fields and methods

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

user objects with fields and methods

Postby ukuru.gejacoya » Fri Jan 31, 2014 1:56 pm

hello!

is there a way to create your own objects, for example,
Code: Select all
scene.my.object = {a=0; b="foo"; twice=(x)=>{x*2}}

and then access its fields and methods like
Code: Select all
scene.my.object.a, scene.my.object.twice(100)
etc.?

there are global objects organized like that, math.* for example.

I tried the above definition, but then if I try to access scene.my.object.a,
it throws an error:
Code: Select all
Failed to evaluate: scene.my.object.a, Object scene.my.object cannot have children; it is not a class object


as far as I understand, a ClassObject can have children, but how do I declare my own ClassObject?
ukuru.gejacoya
 
Posts: 10
Joined: Sun Jun 23, 2013 2:55 pm

Re: user objects with fields and methods

Postby Kilinich » Sat Feb 01, 2014 3:06 pm

There is no way to create such object, but you could use some geom for that.
Foe example make invisible box with onSpawn = (e) => {scene.my.object1 = e.this} and use it in scripts as object, just don't forget to add _ symbol to variables names for saving with scene.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: user objects with fields and methods

Postby ukuru.gejacoya » Sun Feb 02, 2014 7:20 pm

got it! spasibo! :)

update: it's possible to use entity instead of e.this:

Code: Select all
onSpawn = (e) => {scene.my.object1 = entity}
ukuru.gejacoya
 
Posts: 10
Joined: Sun Jun 23, 2013 2:55 pm

Re: user objects with fields and methods

Postby ukuru.gejacoya » Wed Feb 05, 2014 10:33 pm

but then all my _local variables get stuck in the global scope after onSpawn executes, if I close the scene they are still available through console. I really can't grok thyme... :crazy:

step by step example:
1. file > new scene
2. check for _foo variable in global scope: _foo -> undefined
3. create a box
4. right click it > script menu > _foo="bar" //the _foo is added
5. recheck the global scope: console > _foo -> undefined
6. enter onSpawn into the box property window: (e)=>{scene.my.box = entity} //or (e)=>{scene.my.box = e.this}, no diff
7. save the scene
8. reload the saved one
9. console > _foo -> undefined //OK
10. overwrite the scene
11. reload the scene
12. console > _foo -> bar // :crazy: what's just happened? it crawls into global scope upon overwriting the scene
13. new scene
14. console > _foo -> bar // :clap: still there.

this is beyond me.
ukuru.gejacoya
 
Posts: 10
Joined: Sun Jun 23, 2013 2:55 pm

Re: user objects with fields and methods

Postby Xray » Sat Feb 08, 2014 5:07 am

It's even worse than what you had described. After going through those steps that you outlined, I shut down Algodoo and then reopened it, and created a new blank scene. I then checked the console for _foo and it still contained "bar"! I then loaded one of my older saved scenes, and checked the global scope again. _foo was still defined and contained "bar"! :o It seems that no matter what I do, _foo is always defined and contains "bar"! I would say that this bug is definitely FUBAR! :lol:
User avatar
Xray
 
Posts: 501
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: user objects with fields and methods

Postby Kilinich » Sat Feb 08, 2014 12:26 pm

Well then - forget it) :lol:
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: user objects with fields and methods

Postby ukuru.gejacoya » Sat Feb 08, 2014 1:20 pm

thanks for participation, guys!

Xray, it is indeed ... Up Beyond All Recognition :lol: now you have to edit the config.cfg to get rid of it!

Kilinich, ok, trying to forget - but it seemed like a really nice way to incorporate semi-oop techniques into Algodoo :cry:
ukuru.gejacoya
 
Posts: 10
Joined: Sun Jun 23, 2013 2:55 pm

Re: user objects with fields and methods

Postby lololoer16_17 » Wed Feb 19, 2014 1:45 pm

I have tried:

Scene.my.example -> {v=1;z=45;c=564};
Scene.my.v= scene.my.example.v

It gives me an error too
I think readable could work but I haven't tried yet.
lololoer16_17
 
Posts: 18
Joined: Tue Jan 21, 2014 8:55 pm
Location: Spain (South)


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 4 guests