How to transfer local variables between objects?

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

How to transfer local variables between objects?

Postby Ceradel » Mon Dec 28, 2015 11:06 am

Hi everyone! What I'm trying to do is to transfer a value (for example speed: _es , which I calculated as:
Code: Select all
_es = (vel(0) ^ 2 + vel(1) ^ 2))^0.5
from a circle to a thruster which is glued on top of that circle. The problem is that _es is just a local value, just what that circle can use. My question to you guys is how do I tranfer the speed of that circle to the thruster (more specifically to thruster's local value _es ) ?
PS: all in all the goal is not to use designated variables such as scene.my.es or others because i need this combo (circle+thruster) to act according to its own speed and variables ..so i can copy them.
Ceradel
 
Posts: 2
Joined: Mon Dec 28, 2015 10:58 am

Re: How to tranfer local variables between objects?

Postby T'wind_ » Mon Dec 28, 2015 1:43 pm

code in thruster
Code: Select all
    _es = (scene.entityByGeomId((readable(owner)).geom))._es
User avatar
T'wind_
 
Posts: 86
Joined: Wed Jul 08, 2015 5:33 pm
Location: Western Europe

Re: How to transfer local variables between objects?

Postby Ceradel » Mon Dec 28, 2015 2:16 pm

It works, thank you ! :lol:
Ceradel
 
Posts: 2
Joined: Mon Dec 28, 2015 10:58 am

Re: How to transfer local variables between objects?

Postby Kilinich » Mon Dec 28, 2015 2:31 pm

Ceradel wrote:Hi everyone! What I'm trying to do is to transfer a value (for example speed: _es , which I calculated as:
Code: Select all
_es = (vel(0) ^ 2 + vel(1) ^ 2))^0.5
from a circle to a thruster which is glued on top of that circle.


Since cirle don't know anything about thruster, you need to put all code into thruster or add link in circle to thruster on spawn.

1) Code in thruster :
Code: Select all
postStep = (e) => {_es = math.vec.len((scene.entityByGeomID((readable(e.this)).geom)).vel)}


That code is very CPU-intensive, so if it's critical you could try following:

2) Link thruster to circle (thruster code):
Code: Select all
onSpawn = (e) => {geomObj := scene.entityByGeomID((readable(e.this)).geom); geomObj.thruster = e.this}


Now you have "thruster" object inside circle. Don't call it "_thruster" because algodoo can't serialize object variable and probably crash or corrupt scene. That trick allows you to access and change thruster's properties directly from circle's code.
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: How to transfer local variables between objects?

Postby T'wind_ » Mon Dec 28, 2015 7:00 pm

When should readable(owner) or readable(e.this) be used?
User avatar
T'wind_
 
Posts: 86
Joined: Wed Jul 08, 2015 5:33 pm
Location: Western Europe

Re: How to transfer local variables between objects?

Postby FRA32 » Mon Dec 28, 2015 10:30 pm

Well, you can't use e.this. when working inside codeblocks without an 'e' parameter, the (e)=>{} thing.
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm

Re: How to transfer local variables between objects?

Postby T'wind_ » Mon Dec 28, 2015 10:47 pm

Ok, thanks :thumbup:
User avatar
T'wind_
 
Posts: 86
Joined: Wed Jul 08, 2015 5:33 pm
Location: Western Europe

Re: How to transfer local variables between objects?

Postby griggy » Wed Dec 30, 2015 7:28 pm

I have posted a scene to Algobox which demonstrates 3 ways to pass data between geometries. Its name is something like "Inter-entity communication". Try searching for author (griggy).
griggy
 
Posts: 6
Joined: Wed Dec 09, 2015 6:59 pm

Re: How to transfer local variables between objects?

Postby Kilinich » Wed Dec 30, 2015 10:54 pm

griggy wrote:I have posted a scene to Algobox which demonstrates 3 ways to pass data between geometries. Its name is something like "Inter-entity communication". Try searching for author (griggy).

Did you notice button "copy" in algobox page of your scene? Press it, then add code here and you will get link to scene like this:
no image found for this scene
Rating: rated 5
Filesize: 56.28 kB
Comments: 6
Ratings: 1
download
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: How to transfer local variables between objects?

Postby Kilinich » Wed Dec 30, 2015 11:10 pm

griggy wrote:I have posted a scene to Algobox which demonstrates 3 ways to pass data between geometries. Its name is something like "Inter-entity communication". Try searching for author (griggy).


It's not good idea to store objects in scene.my. container. After save-load you will have something like this:

Code: Select all
// FileVersion 21
// Algodoo scene created by Algodoo v2.1.0

Scene.my.count12 := 0;
Scene.my.count1 := 0;
Scene.my.arrow2colour := 0;
Scene.my.box2Atext := undefined;
Scene.my.arrow12colour := 0;
Scene.my.doAfter := (delay, func)=>{
    scene.addBox({
        timeToLive = delay;
        onDie = func;
        collideSet = 0;
        size = [9.9999997e-005, 9.9999997e-005]
    })
};
Scene.my.label1 := inertiaMultiplier = 1.0000000;
resources = [];
timeToLive = +inf;
textureClamped = [false, false];
adhesion = 0.00000000;
attractionType = 2;
attraction = 0.00000000;
textScale = 0.14000000;
texture = "";
update = (e)=>{};
controllerInvertX = false;
controllerInvertY = false;
showMomentum = false;
textConstrained = true;
vel = [0.00000000, 0.00000000];
...


All af that crap will be stored in your config (since there is no container in definition).
Also, if you store list of objects, it will totally crash your algodoo so only config reset will help :(
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: How to transfer local variables between objects?

Postby griggy » Sun Jan 03, 2016 5:09 pm

To Kilinich. Thank you for your information. Unfortunately I do not understand it. I am new to Algodoo and do not know how it works. Could you (or someone else) please explain what was meant. Which of the 3 methods is unusable? What Thyme code is wrong?
griggy
 
Posts: 6
Joined: Wed Dec 09, 2015 6:59 pm

Re: How to transfer local variables between objects?

Postby FloMoTion » Sat Dec 10, 2016 3:41 pm

Hey, I tried your example and found it useful for ONE Object. In my case, I need to count 3 objects. Choosing a different Entity Name doesnt work if I choose the same counter variable. Furthermore the text field is deleted after saving and loading the scene.
Do I have to introduce 3 different Counter variables to be able to count 3 different Actions? I thought creating 3 different classes would work.

Some more info:
Shape A hits Rect A and in Rect A's onCollide I have "e.other.counter = e.other.counter + 1" same happens with B and C but I only have one Rectangle. They all hit the same one.
Using "scene.my.A.counter", "scene.my.B.counter", "scene.my.C.counter" doesnt help because the counter increases in all Shapes. I maybe just delete all classes in the current Scene.
FloMoTion
 
Posts: 4
Joined: Sat Dec 10, 2016 3:27 pm

Re: How to transfer local variables between objects?

Postby FRA32 » Sun Dec 11, 2016 5:28 pm

You could store the counters in the 3 objects as _counter, and if you increase them, you say e.other._counter = e.other._counter + 1(The variable needs a _ to get permanently stored);

If you require further help, please provide further information so I can give a more precice answer
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm

Re: How to transfer local variables between objects?

Postby Kilinich » Mon Dec 12, 2016 9:34 am

FloMoTion wrote:Some more info:
Shape A hits Rect A and in Rect A's onCollide I have "e.other.counter = e.other.counter + 1" same happens with B and C but I only have one Rectangle. They all hit the same one.
Using "scene.my.A.counter", "scene.my.B.counter", "scene.my.C.counter" doesnt help because the counter increases in all Shapes. I maybe just delete all classes in the current Scene.

I didn't get your goal. What you want to get as result? Could you explain it easier way?
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: How to transfer local variables between objects?

Postby FloMoTion » Sat Dec 17, 2016 11:43 am

Kilinich wrote:
FloMoTion wrote:Some more info:
Shape A hits Rect A and in Rect A's onCollide I have "e.other.counter = e.other.counter + 1" same happens with B and C but I only have one Rectangle. They all hit the same one.
Using "scene.my.A.counter", "scene.my.B.counter", "scene.my.C.counter" doesnt help because the counter increases in all Shapes. I maybe just delete all classes in the current Scene.

I didn't get your goal. What you want to get as result? Could you explain it easier way?


Ok. Im doing a marble race and want to count the number of rounds a marble has done. THerefore I have one rectangle which should increases the "counter" of each marble within a race and teleports thenm to the beginning of the track. In another Box next to the track I want the "round progress number" to be shown. Of course, each marble should have it s own counter and be independent of the others. I ll try to upload a scene asap.

I already tried the _counter possibility, but I guess I did sth wrong. Will try again.
FloMoTion
 
Posts: 4
Joined: Sat Dec 10, 2016 3:27 pm

Re: How to transfer local variables between objects?

Postby FloMoTion » Sat Dec 17, 2016 1:17 pm

Here is an example, which doesnt work. All "_counters" are increased simultaneously and not independently. I thought putting 'scene.my.ID._counter = 'scene.my.ID._counter + 1' to increase a local counter does the trick.

Please have a look and tell me what I did wrong here. I want all counters to go up only if the ball of the same color reaches the bottom of the track.
Attachments
multiple_counter.phz
(210.54 KiB) Downloaded 394 times
FloMoTion
 
Posts: 4
Joined: Sat Dec 10, 2016 3:27 pm

Re: How to transfer local variables between objects?

Postby FRA32 » Tue Dec 20, 2016 6:21 pm

You can do this fairly easily then. Add a _counter variable to all marbles set to 0 at the beginning(Open the script menu of all marbles at once and type in the black box in the top corner _counter = 0). When colliding with the round goal, use the box to increase e.other._counter by one. At the same time ,you can set the text of your display. Thatfor you should use scene.my.variables called scene.my.lastScore = ""
and scene.my.isScore = false
When a marble collides, use it's materialname, which should contain it's color/motif for reference, to identify the score that just updated:
scene.my.lastScore = e.other.materialname;
scene.my.isScore = true;
Inside your Scoreboard you can then change you'r scores by updating depending on the first value:
scene.my.isScore ? {
scene.my.lastScore == "red" ? {_red = _red +1} : {};
scene.my.lastScore == "blue" ? {_blue = _blue +1} : {};
scene.my.lastScore == "green" ? {_green = _green +1} : {};
scene.my.lastScore == "smiley" ? {_smiley = _smiley +1} : {}
and so on...
}:{}

Finally you update your scoreboard like this:
text = "Red: "+_red+ "\n Blue: "+_blue+"\n Green: ".......


Note: Don't use scene.my.variables for every task you need. Only use them for global or communication variables. For locally needed values, use the object's variables instead.
FRA32
 
Posts: 229
Joined: Wed Dec 03, 2014 9:51 pm

Re: How to transfer local variables between objects?

Postby FloMoTion » Wed Dec 21, 2016 9:23 pm

Thanks for the inspiration. I chose a different approach and created a Score Vector because I only got 3 marbles. But I used the materialname option to distinguish the index of the Score Vector which is to be increased.
FloMoTion
 
Posts: 4
Joined: Sat Dec 10, 2016 3:27 pm

Re: How to transfer local variables between objects?

Postby Kitesurfer » Fri Jan 15, 2021 6:35 pm

Thanks a lot! Kilinich's solution to connect the objects with the thruster inside the thrusted object really is of good use to me!
Kitesurfer
 
Posts: 10
Joined: Thu Jan 07, 2021 5:27 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 2 guests