How to transfer local variables between objects?
19 posts • Page 1 of 1
How to transfer local variables between objects?
Hi everyone! What I'm trying to do is to transfer a value (for example speed: _es , which I calculated as:
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.
- Code: Select all
_es = (vel(0) ^ 2 + vel(1) ^ 2))^0.5
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?
code in thruster
- Code: Select all
_es = (scene.entityByGeomId((readable(owner)).geom))._es
-
T'wind_ - Posts: 86
- Joined: Wed Jul 08, 2015 5:33 pm
- Location: Western Europe
Re: How to transfer local variables between objects?
It works, thank you !
- Ceradel
- Posts: 2
- Joined: Mon Dec 28, 2015 10:58 am
Re: How to transfer local variables between objects?
Ceradel wrote:Hi everyone! What I'm trying to do is to transfer a value (for example speed: _es , which I calculated as:from a circle to a thruster which is glued on top of that circle.
- Code: Select all
_es = (vel(0) ^ 2 + vel(1) ^ 2))^0.5
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...
-
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?
When should readable(owner) or readable(e.this) be used?
-
T'wind_ - Posts: 86
- Joined: Wed Jul 08, 2015 5:33 pm
- Location: Western Europe
Re: How to transfer local variables between objects?
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?
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?
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:
Dream of Algodoo as game development engine...
-
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?
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...
-
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?
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?
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.
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?
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
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?
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...
-
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?
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?
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.
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?
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.
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?
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?
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
19 posts • Page 1 of 1
Who is online
Users browsing this forum: No registered users and 2 guests