what is this??????????????????????????????

Discussion and feedback on Construct 2

Post » Sun May 11, 2014 1:11 am

I saw a demo about this
whatisthis.png


may i ask what is this??
seems very nice! :mrgreen:
i guees this will help me Make the code shorter

any link let me know more about this plzzz
You do not have the required permissions to view the files attached to this post.
B
17
S
7
Posts: 360
Reputation: 2,202

Post » Sun May 11, 2014 2:15 am

Conditionals.
If touch.y is greater than gold then set the variable to touch.y-gold*gspeed, else set it to touch.y+gold*gspeed.

With the ? indicating the if, and the : indicating the else.
Basically test if something is true, if it is, then it sets it to the value following the question mark. If its not then it sets it to the value following the :.
In this case it determines if you are going up, or down.

At least I think that's how it works, the script does seem familiar. :roll:
Image ImageImage
B
171
S
50
G
179
Posts: 8,382
Reputation: 113,458

Post » Sun May 11, 2014 3:18 am

if (Touch.Y>gold)
gnew=Touch.Y-(gold*gspeed)
else
gnew=Touch.Y+(gold*gspeed)
B
83
S
23
G
14
Posts: 389
Reputation: 12,529

Post » Sun May 11, 2014 6:20 am

Thankyou
This is awesome!
B
17
S
7
Posts: 360
Reputation: 2,202

Post » Tue Aug 12, 2014 2:45 pm

@newt
@tumira
Is it possible to nest it like:

if (Touch.Y=gold)
gnew=Touch.Y-(gold*gspeed)
else if (Touch.Y>gold)
gnew=Touch.Y+(gold*gspeed)+a
else if (Touch.Y<gold)
gnew=Touch.Y+(gold*gspeed)+b
else
gnew=Touch.Y+(gold*gspeed)+c

How do I write this using ?: operators? Can't seems to do this like I do it in MS Excel.
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Tue Aug 12, 2014 2:58 pm

You don't want to do that.. The ternary ("?:") operator can be confusing to read, and nesting several of them is error prone and generally to be avoided. But yes it is possible. It will have many nested parentheses and be much less readable than the above. Some coding guidelines actually prohibit the use of this operator.
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Tue Aug 12, 2014 3:02 pm

codah wrote:You don't want to do that.. The ternary ("?:") operator can be confusing to read, and nesting several of them is error prone and generally to be avoided. But yes it is possible. It will have many nested parentheses and be much less readable than the above. Some coding guidelines actually prohibit the use of this operator.

Is that so? Hmm guess I have to use a lot of pickings then for a workaround on my current problem. @codah can you show a sample of writing it correctly since you said it is possible, for the sake of knowledge. :D
B
28
S
8
G
4
Posts: 553
Reputation: 4,924

Post » Tue Aug 12, 2014 4:28 pm

Just check the syntax of ternary operators
Condition?valueIfTrue:valueIfFalse

so if you want to nest ternary operators, you can use parenthesis:

Condition?(ConditionIfTrue?valueIfTrue:valueIfFalse):(ConditionIfFalse?valueIfTrue:valueIfFalse)

and so on and so forth. Keep in mind you can also nest in the "condition" parameter, and you don't have to nest in both value (true/false).

Code: Select all
if (Touch.Y=gold)
gnew=Touch.Y-(gold*gspeed)
else if (Touch.Y>gold)
gnew=Touch.Y+(gold*gspeed)+a
else if (Touch.Y<gold)
gnew=Touch.Y+(gold*gspeed)+b
else
gnew=Touch.Y+(gold*gspeed)+c

can be written (just to make it easier to visualise) as
Code: Select all
(Touch.Y=gold)?
   true= gnew=Touch.Y-(gold*gspeed)
   false=(Touch.Y>gold)?
      true= gnew=Touch.Y+(gold*gspeed)+a
      false=(Touch.Y<gold)?
         true= gnew=Touch.Y+(gold*gspeed)+b
         false= gnew=Touch.Y+(gold*gspeed)+c

which then becomes:
Code: Select all
gnew=(Touch.Y=gold)?(gnew=Touch.Y-(gold*gspeed)):((Touch.Y>gold)?(Touch.Y+(gold*gspeed)+a):((Touch.Y<gold)?(Touch.Y+(gold*gspeed)+b):(Touch.Y+(gold*gspeed)+c)))


As you can see, this is a nightmare to write, and outright impossible to understand. Avoid nesting ternary operators if at all possible.
Last edited by Fimbul on Tue Aug 12, 2014 4:37 pm, edited 2 times in total.
B
36
S
8
G
8
Posts: 532
Reputation: 6,903

Post » Tue Aug 12, 2014 4:36 pm

Anybody gotten the inline or operator to work right yet?
Image ImageImage
B
171
S
50
G
179
Posts: 8,382
Reputation: 113,458

Post » Tue Aug 12, 2014 4:52 pm

This works
Code: Select all
(time<5 | time>10)?"not now":"now"
B
94
S
33
G
113
Posts: 5,356
Reputation: 73,273

Next

Return to Construct 2 General

Who is online

Users browsing this forum: maddy467 and 6 guests