Spawning Multiple Objects?

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

Spawning Multiple Objects?

Postby Rideg » Wed Jan 20, 2010 8:39 am

Hello, I need some help with getting an oncollide "happen" ten times when it hits something. (when it hits another object ten circles should be spawned). I've tried pos:=[e.pos, e.pos, e.pos, e.pos, e.pos,.....] but that didn't work :( If You got any ideas please tell me ;) :clap:
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby gradyfitz » Wed Jan 20, 2010 1:15 pm

Rideg wrote:Hello, I need some help with getting an oncollide "happen" ten times when it hits something. (when it hits another object ten circles should be spawned). I've tried pos:=[e.pos, e.pos, e.pos, e.pos, e.pos,.....] but that didn't work :( If You got any ideas please tell me ;) :clap:

A code like this should do it:
Code: Select all
(e)=>{     ! Scene.my.Running ? {         Scene.my.Running = true;         for(10, (x)=>{e.this.onCollide(e)});         Scene.my.Running = false     } : {Scene.my.ExValue = Scene.my.ExValue + 1} }

Declare Scene.my.Running as false to use this (this particular function also requires you to declare Scene.my.ExValue as anything (numerical)).


(e)=>{ ! Scene.my.Running ? { Scene.my.Running = true; for(<REPEATS>, (x)=>{e.this.onCollide(e)}); Scene.my.Running = false } : {<ACTION>} }

Replace <REPEATS> with a number, under ~45 or so (you can stack for functions one after the other but toying with this code can easily crash Algodoo by infinite recursion if you aren't careful), and replace <ACTION> with the action you want to repeat 10 times, this action can include (e) values as it sends the ClassObject into the next onCollide :D. this particular method makes for quick, easy reading, this particular method could be replaced with something less "dangerous" like just copying and pasting the code over and over one after the other :).

This method works by getting the opposite of Scene.my.Running (no particular reason, probably easier to just edit the end brackets than the starting ones or something :D ) and then sends it into the if_then_else infixed operation, if it's true (opposite), it sets Scene.my.Running to true, so, if the script is run while this script is running, it will not enter this loop and begin infinite recursion, it is then told to send the class object e (from the original call) into e.this.onCollide <REPEATS> many times, it then sets Scene.my.Running back to false, allowing the repeats to begin again if the object is hit again, my example script just adds 1 to Scene.my.ExValue 10 times :D.

Good Luck, if you have any troubles I should be able to help :).

--
Grady
Mechanisms: 18 Mechanisms.
Thyme: Tutorial - Variables/Commands List.
Thymechanic
gradyfitz
 
Posts: 174
Joined: Tue Sep 01, 2009 8:33 am
Location: Victoria, Australia

Re: Spawning Multiple Objects?

Postby Rideg » Wed Jan 20, 2010 9:41 pm

wow nice explanation really! but is there a way doing this without using any variables?? :P
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Wed Jan 20, 2010 10:21 pm

Easily.
Code: Select all
for(10, (x)=>{Scene.addCircle({circle stuff here})})

Put that into the onCollide but fill out the "circle stuff here" with your actual parameters you used.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby Rideg » Wed Jan 20, 2010 10:33 pm

i'm typing (10, (e)=>{scene.addCircle({Circle Stuff})}) but it doesn't seem to work :wtf:
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Wed Jan 20, 2010 10:47 pm

Can you please post what you currently have?
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby Rideg » Wed Jan 20, 2010 10:50 pm

Code: Select all
(10, (e)=>{
    scene.addCircle({         radius := 0.2;         color := [0.3, 0.3, 0.3, 1.0];         density := 120;         vel := [0, 20];         pos := e.pos     }) })
here..
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Wed Jan 20, 2010 10:52 pm

:lol: You forgot to put for in front of the first parenthesis.
Code: Select all
for(10, (e)=>{ scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := e.pos }) })

Fixed it.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby Rideg » Wed Jan 20, 2010 10:58 pm

oh..*feels stupid* thank you :P
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby Rideg » Wed Jan 20, 2010 10:59 pm

wtf? it created 10 at the ,middle of the scene before the sim even began to run..?!
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby Rideg » Wed Jan 20, 2010 11:02 pm

plus the script messe up and tells stuff about the circle :wtf:
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Thu Jan 21, 2010 2:24 am

Could you please explain further. I can't quite understand what you mean.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby Rideg » Thu Jan 21, 2010 8:45 am

well i typed in "for" as you said then when I pressed enter, 10 Circles was spawned when the scene was paused and at the position of [0,0] and when I checked the script it had info about the spawned circle, Major bug or am I doing something wrong?? :?
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby gradyfitz » Thu Jan 21, 2010 11:35 am

Rideg wrote:well i typed in "for" as you said then when I pressed enter, 10 Circles was spawned when the scene was paused and at the position of [0,0] and when I checked the script it had info about the spawned circle, Major bug or am I doing something wrong?? :?

Code: Select all
for(10, (e)=>{ scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := e.pos }) })

Is a risky affair because you are sending "e" into the for structure, thus, scopes are confused, a similar effect can be seen if you put color = [1,1,1,1] into the console, and try to load a scene, though this isn't the same sort of problem, it has the same cause, "e" is sent into onCollide (this is changable, but all your "e".pos scripts would have to be changed to whatever you changed to as well), the solution is to use a different thing other than e for the for structure

z for example.
Code: Select all
for(10, (z)=>{ scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := e.pos }) })


You could use any name you wanted for it as it isn't used in the calculations at all. just best not to use "e" as this may or may not override the onCollide scope (it sometimes works and sometimes doesn't). It is difficult for the Thyme parser to know that you want the "e" sent into onCollide not the "e" sent into the for structure.

I imagine choosing "e" as the argument placeholder was because of so many scripts not mattering if you used "e" or another letter, but using for structures do require you to use a different letter than e for reasons of scope.

Hopefully this helped. :)
Mechanisms: 18 Mechanisms.
Thyme: Tutorial - Variables/Commands List.
Thymechanic
gradyfitz
 
Posts: 174
Joined: Tue Sep 01, 2009 8:33 am
Location: Victoria, Australia

Re: Spawning Multiple Objects?

Postby Rideg » Thu Jan 21, 2010 5:33 pm

no changes :(
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Thu Jan 21, 2010 6:03 pm

I just tested it in Algodoo and Phun. They both worked perfectly. I don't see what you're getting.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby KarateBrot » Thu Jan 21, 2010 6:15 pm

When looking at the code it looks fine but after reading it's not working for you I wanted to be sure if it's not only working for standardtoaster. And yeah, I tested it and it works just as expected:
10 grey circles get spawned at the collision event with a velocity of 20m/s upwards.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Spawning Multiple Objects?

Postby Rideg » Thu Jan 21, 2010 7:06 pm

Why am I getting wrong readings?
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Thu Jan 21, 2010 7:20 pm

Are you using the exact code that is posted above? Did you click on select all, press control-c or command-c, go back to phun/algodoo, click in the onCollide text box and put the insertion point between the curly brackets, and then paste?
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby Rideg » Thu Jan 21, 2010 7:42 pm

yes did that wierd thing.. :P
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Thu Jan 21, 2010 8:40 pm

Very odd. Have you tried making a new scene with this script? Have you deleted your config.cfg yet?
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby Rideg » Thu Jan 21, 2010 11:11 pm

oh it works thank you! Is there a way getting an objects pos with something like "e.geom.pos"?
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Thu Jan 21, 2010 11:19 pm

Not really. You could attach a beacon to the center of the object to track its position.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm

Re: Spawning Multiple Objects?

Postby Rideg » Fri Jan 22, 2010 5:28 pm

okey is it possible to make number 2, 6, 5, 9 hetero collide of the ten spawned circles???
Image
make sure to check out my work.
User avatar
Rideg
 
Posts: 948
Joined: Tue Dec 15, 2009 5:17 pm
Location: Östersund, Sweden

Re: Spawning Multiple Objects?

Postby standardtoaster » Fri Jan 22, 2010 8:34 pm

Code: Select all
for(10, (z)=>{ z == 1 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { z == 4 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { z == 5 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { z == 8 ? { scene.addCircle({ heteroCollide := true; radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } : { scene.addCircle({ radius := 0.2; color := [0.3, 0.3, 0.3, 1.0]; density := 120; vel := [0, 20]; pos := App.mousePos }) } } } } })

Hopefully this is that you want.
User avatar
standardtoaster
 
Posts: 606
Joined: Mon Aug 31, 2009 7:57 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 15 guests

cron