How do I address an object in an expression?

Get help using Construct 2

Post » Sat Jul 16, 2016 9:15 am

Hi!

I'm trying to access the "Text" from an input field via it's instance variable.
But it doesn't work as expected.

I have two input fields (same object, but a diffrent Instance Variable ("InputType").

Image

If I press login it logs following:

Code: Select all
"Email: " & input(input.InputType = "Email").Text & " / Password: " & input(input.InputType = "Password").Text


But the output is like this.
Image

So the
Code: Select all
input(input.InputType = "Email").Text
returns the password and the
Code: Select all
& input(input.InputType = "Password").Text
returns the email.
I double checked my input fields. I do not know what to do at this point. Maybe @ashley has an idea?

I uploaded the capx file on my dropbox account.
https://www.dropbox.com/s/5zn7pfwyad5mx ... .capx?dl=1

Thank you!
Image Image iTunes: Match3 | Google Play: Match3
B
95
S
33
G
30
Posts: 1,099
Reputation: 27,670

Post » Sat Jul 16, 2016 10:39 am

Most value evaluating conditions pair up with expressions.
There is the condition Sprite > Compare X and its pairing expression Sprite.X.

So, u can use Sprite > Compare X .. or System > Compare two values (using Sprite.X).
The first one will filter the picklist (SOL), de second one will not filter the picklist.

There is a reason why i started like this.

Because in the same logic ....
You have the condition System > Pick nth Instance and its pairing expression sprite(nth).xxxxx

'nth' is in fact the IID for an instance of an object in a certain picklist (SOL), it is a zero based index, and it is a number.

If you have all instances of an object picked, this IID represents also the order that they are created.

And now some more background info.

Some conditions (but also expressions) have only a meaning when used with a certain condition.
The condition 'pick nth instance' has no meaning without a condition that actual picks items (make a SOL), so sprite(nth).xxxxx has no meaning without a pick.
The expresssion loopindex("i") has not meaning without a loop condition.
The expression array.curX has no meaning without a 'for each element' condition.

So, for you to adress an nth instance by instance(nth).xxxx, you should use a 'pick all' (althaught for only 1 line of code as in your example this is redunctant).
Then using the expression goes like this ...
"Email: " & input(0).Text & " / Password: " & input(1).Text
Beeing 0 the IDD of the first instance of the sprite input, 1 the IDD of the second sprite input.

You can use logical expressions. Like :
input.InputType = "Email" ? 1 : 0
But it is a bit weird to use this to return an IID.
B
33
S
18
G
28
Posts: 2,455
Reputation: 20,874

Post » Sat Jul 16, 2016 11:21 am

99Instances2Go wrote:Most value evaluating conditions pair up with expressions.
There is the condition Sprite > Compare X and its pairing expression Sprite.X.

So, u can use Sprite > Compare X .. or System > Compare two values (using Sprite.X).
The first one will filter the picklist (SOL), de second one will not filter the picklist.

There is a reason why i started like this.

Because in the same logic ....
You have the condition System > Pick nth Instance and its pairing expression sprite(nth).xxxxx

'nth' is in fact the IID for an instance of an object in a certain picklist (SOL), it is a zero based index, and it is a number.

If you have all instances of an object picked, this IID represents also the order that they are created.

And now some more background info.

Some conditions (but also expressions) have only a meaning when used with a certain condition.
The condition 'pick nth instance' has no meaning without a condition that actual picks items (make a SOL), so sprite(nth).xxxxx has no meaning without a pick.
The expresssion loopindex("i") has not meaning without a loop condition.
The expression array.curX has no meaning without a 'for each element' condition.

So, for you to adress an nth instance by instance(nth).xxxx, you should use a 'pick all' (althaught for only 1 line of code as in your example this is redunctant).
Then using the expression goes like this ...
"Email: " & input(0).Text & " / Password: " & input(1).Text
Beeing 0 the IDD of the first instance of the sprite input, 1 the IDD of the second sprite input.

You can use logical expressions. Like :
input.InputType = "Email" ? 1 : 0
But it is a bit weird to use this to return an IID.


Hi @99Instances2Go

Thank you very much for your detailed respone.
I know that I easily can get it via it's IID. But the game generates input fields depending on the current form (register or login). So I wanted to pick the corresponding input field depending on its instance variable.
So I thought I could pick an instance by the value of an instance variable of it.

Now I altered my code to this.
Code: Select all
"Email: " & input(input.InputType = "Email" ? input.IID : -1).Text & " / Password: " & input(input.InputType = "Password" ? input.IID : -1).Text


This worked out for me ;) Thank you for your help!

Regards
Andy
Image Image iTunes: Match3 | Google Play: Match3
B
95
S
33
G
30
Posts: 1,099
Reputation: 27,670

Post » Sat Jul 16, 2016 11:25 am

@99Instances2Go is pretty much spot on. The form Object(n).Expression uses 'n' as an instance index. In this case you are passing a comparison of the input type. This is completely unrelated to the rest of the expression, it will just evaluate 'n' as a number to find the instance index. Since you pass a comparison it returns a boolean, which in C2 is just 0 or 1. If C2 had stricter type rules, this would not be allowed at all, since often you're not allowed to pass a boolean in place of a number. Anyway this produces an instance index which has nothing to do with the original comparison! Your example is probably confusing you because it is obvious what you want it to do, but it doesn't do that.

TBH it's easier to just pick the instances you want with conditions - I'm not convinced the Object(n).Expression feature is worth the complexity...
Scirra Founder
B
387
S
230
G
87
Posts: 24,249
Reputation: 192,250


Return to How do I....?

Who is online

Users browsing this forum: adamebastin, ketramtu18 and 8 guests