Page 1 of 1

How to transfer local variables between objects?

PostPosted: Mon Dec 28, 2015 11:06 am
by Ceradel
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.

Re: How to tranfer local variables between objects?

PostPosted: Mon Dec 28, 2015 1:43 pm
by T'wind_
code in thruster
Code: Select all
    _es = (scene.entityByGeomId((readable(owner)).geom))._es

Re: How to transfer local variables between objects?

PostPosted: Mon Dec 28, 2015 2:16 pm
by Ceradel
It works, thank you ! :lol:

Re: How to transfer local variables between objects?

PostPosted: Mon Dec 28, 2015 2:31 pm
by Kilinich
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.

Re: How to transfer local variables between objects?

PostPosted: Mon Dec 28, 2015 7:00 pm
by T'wind_
When should readable(owner) or readable(e.this) be used?

Re: How to transfer local variables between objects?

PostPosted: Mon Dec 28, 2015 10:30 pm
by FRA32
Well, you can't use e.this. when working inside codeblocks without an 'e' parameter, the (e)=>{} thing.

Re: How to transfer local variables between objects?

PostPosted: Mon Dec 28, 2015 10:47 pm
by T'wind_
Ok, thanks :thumbup:

Re: How to transfer local variables between objects?

PostPosted: Wed Dec 30, 2015 7:28 pm
by griggy
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).

Re: How to transfer local variables between objects?

PostPosted: Wed Dec 30, 2015 10:54 pm
by Kilinich
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

Re: How to transfer local variables between objects?

PostPosted: Wed Dec 30, 2015 11:10 pm
by Kilinich
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 :(

Re: How to transfer local variables between objects?

PostPosted: Sun Jan 03, 2016 5:09 pm
by griggy
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?

Re: How to transfer local variables between objects?

PostPosted: Sat Dec 10, 2016 3:41 pm
by FloMoTion
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.

Re: How to transfer local variables between objects?

PostPosted: Sun Dec 11, 2016 5:28 pm
by FRA32
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

Re: How to transfer local variables between objects?

PostPosted: Mon Dec 12, 2016 9:34 am
by Kilinich
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?

Re: How to transfer local variables between objects?

PostPosted: Sat Dec 17, 2016 11:43 am
by FloMoTion
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.

Re: How to transfer local variables between objects?

PostPosted: Sat Dec 17, 2016 1:17 pm
by FloMoTion
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.

Re: How to transfer local variables between objects?

PostPosted: Tue Dec 20, 2016 6:21 pm
by FRA32
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.

Re: How to transfer local variables between objects?

PostPosted: Wed Dec 21, 2016 9:23 pm
by FloMoTion
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.

Re: How to transfer local variables between objects?

PostPosted: Fri Jan 15, 2021 6:35 pm
by Kitesurfer
Thanks a lot! Kilinich's solution to connect the objects with the thruster inside the thrusted object really is of good use to me!