Problem with an 'if/then statement

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

Problem with an 'if/then statement

Postby eeieeio » Thu Feb 15, 2018 10:45 pm

Hi,
I want to increment a count of all circles with texture "nickel.png" that collide with a box. Only random circles have that texture. Here is what I have done which doesn't work:

I set a variable scene.my.nickelcount = 0 in the console
I created a box and set onCollide (e)=>e.other.texture = "nickel.png? {scene.my.nickelcount = scene.my.nickelcount + 1}: {}
I created a box with text set to {scene.my.nickelcount}

Thyme rejects the onCollide code above.

Any help would be appreciated.

EEIEEIO
Dave
eeieeio
 
Posts: 25
Joined: Thu Nov 30, 2017 7:02 pm

Re: Problem with an 'if/then statement

Postby Kilinich » Fri Feb 16, 2018 8:49 pm

Should be:
Code: Select all
onCollide = (e) => {e.other.texture == "nickel.png" ? {scene.my.nickelcount = scene.my.nickelcount + 1} : {}}


And I must warn you - there could be multiple collisions and onCollide calls per one coin so you might need to handle this.
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: Problem with an 'if/then statement

Postby Xray » Sun Mar 18, 2018 3:14 am

eeieeio -- It appears that you are not using "=" (equal sign) correctly. Here are examples of its usage in scripts:

Use "=" by itself in order to store something into a variable. "something" can be a numeric value, it can be a Boolean operator (such as true or false), or it can be "text". The right side of the equation gets stored into the left side. For example: scene.my.var = 3.14, or scene.my.var = false, or scene.my.var = "This is text".
Use "==" for testing a variable in order to check its value, and to make a decision based on the result of that test. For example:
scene.my.var == 3.14 ? {scene.my.var = 0}:{scene.my.var = scene.my.var * 2} ; In this example, you are testing the variable named "scene.my.var" to determine if the value stored in it is 3.14. If the value IS 3.14, then set scene.my.var to 0. If scene.my.var is NOT 3.14, then multiply the value in that variable times 2.

Here is a List of Tutorials which you will find helpful.

The code in the previous comment by kilinich is an example of a script that should work for what you are trying to do.

Let us know if you have any other questions....

Xray
User avatar
Xray
 
Posts: 500
Joined: Sun Jun 17, 2012 6:12 am
Location: USA

Re: Problem with an 'if/then statement

Postby eeieeio » Tue Mar 20, 2018 4:31 pm

Thank you Xray, that helped a lot
eeieeio
 
Posts: 25
Joined: Thu Nov 30, 2017 7:02 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 1 guest

cron