text object

For questions about using Classic.

Post » Sun Dec 09, 2007 10:38 pm

how flexible IS the text object?

i'm trying to setup a hp counter and the action i'm trying to use to achieve this is:

[color=green:1nlq25k2]Text: Set text to RedCastle.Value('HPcurrent') + "/" + RedCastle.Value('HPmax')[/color:1nlq25k2]

the private variables are initialized to 100 each and with my knowledge of programming, should produce this:

[color=green:1nlq25k2]100/100[/color:1nlq25k2]

but i get this instead:

[color=green:1nlq25k2]200.[/color:1nlq25k2]

the period seems to automatically appear

logical error?
B
2
S
2
G
5
Posts: 293
Reputation: 2,236

Post » Sun Dec 09, 2007 10:47 pm

To remove the period, try setting HPcurrent and HPmax to int(); as for the incorrect result, use & instead of + for concatenation.

[color=green:15mrdft9]Text: Set text to int(RedCastle.Value('HPcurrent')) & "/" & int(RedCastle.Value('HPmax'))[/color:15mrdft9]

Or so I would think.
B
4
S
2
G
6
Posts: 161
Reputation: 2,597

Post » Sun Dec 09, 2007 10:56 pm

well, whaddaya know! that works! haha
thanks for the help! :D
B
2
S
2
G
5
Posts: 293
Reputation: 2,236

Post » Mon Dec 10, 2007 12:03 am

Just to shed some light on what happens here:
+ is arithmetic addition, which means adding numbers, or when used between a string and a string, text concatenation. & always means text concatenation eg. if you use 100 & "%" you get 100%. 100 + "%" won't work because it can't perform arithmetic addition.

The type checker allows RedCastle.Value('HPcurrent') + "/" because private variables can hold anything so the expression could be valid - it can't tell in the expression editor, it doesn't know the value of 'HPcurrent' yet.

So it ends up trying to do 100 + "/" at runtime which is invalid, so Construct does nothing (the left operand is unaffected by the addition). Hence 100 + "/" evaluates to 100. Then you add another 100 to it, which gives 200.

Tip: run a debug, and in this situation a message is added to the log.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,600


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 0 guests