Variable Calls: are they strings or ???

For questions about using Classic.

Post » Mon Oct 26, 2009 1:34 pm

I'm trying to set an X coordinate using a private variable call:

Sex X || player1.X

Since my game can have a few players whose player numbers are set to global variables, and I didn't want to constantly set code for all of them I tried to trick the system with something like this:

Set X || "player"&global('XYZ')&".X"

which didn't work so I tried

Set X || str("player"&global('XYZ')&".X")

which still resulted in nothing. So I'm left to assume "player1.X" is not a string but something else. And I'm also assuming that I can't trick Construct into thinking a returned string "playerXYZ.X" is the same as typing in playerXYZ.X

(much to my chagrin :cry: )

If you need a .cap I can whip one up, but it'll have to be through rapidshare because I can't access getdropbox through my DNS (much to my chagrin... again :cry: )
B
51
S
13
G
8
Posts: 194
Reputation: 7,311

Post » Mon Oct 26, 2009 1:46 pm

no
you can't use variables in that way
they are interpreted differently,
and have a different syntax, 'like this', "instead of this"

you can use arrays for this purpose
or I would recommend using the s plugin
which is designed to allow more logical data organization
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Mon Oct 26, 2009 2:08 pm

Text in quotes never changes (they're called string literals). Construct will never take "text like this" and try to interpret it as an expression. You will literally be passing some text to the Set X function, which happens to say "Player1.X" or something similar. Construct will then try to convert this to a number to set the X co-ordinate, but this fails, so it gets zero.

You basically can't dynamically create expressions. What are you actually trying to do, overall? Maybe families or some other feature is a better solution.
Scirra Founder
B
359
S
214
G
72
Posts: 22,946
Reputation: 178,478

Post » Mon Oct 26, 2009 3:21 pm

Thanks for your detailed description. It makes sense to me now.

I have a 3 player platform game with three types of characters (A, B, C). In the player selection screen, when a player selects a character (say player 3 selects B), a global variable called BPlayerNum is set to 3.

Then, in game, all actions for player type C has the input set to Player global('C')
Which works great. The problem is setting the characters animation to the player sprite with the platform animation which happens every tick (always condition).

I was trying to avoid having 9 conditions in the always condition through my entire game:
If APlayerNum=1 then set A's animations to Player1
If APlayerNum=2 then set A's animations to Player2
If APlayerNum=3 then set A's animations to Player3
If BPlayerNum=1 then set B's animations to Player1
If BPlayerNum=2 then set B's animations to Player2
If BPlayerNum=3 then set B's animations to Player3
If CPlayerNum=1 then set C's animations to Player1
If CPlayerNum=2 then set C's animations to Player2
If CPlayerNum=3 then set C's animations to Player3

I'm trying to make a .cap right now to make my explanation more clear.

I'm trying to be a more elegant coder. Right now I use brute force way to much :)
B
51
S
13
G
8
Posts: 194
Reputation: 7,311

Post » Mon Oct 26, 2009 4:04 pm

Here's a cap.

3 player, 3 character game with character selector
[url:1augag2t]http://rapidshare.com/files/298210349/HELP01.cap.html[/url:1augag2t]

Sorry it's at rapidshare but I don't know what the next best thing is besides DropBox (which doesn't work at my location, both uploading and downloading).
B
51
S
13
G
8
Posts: 194
Reputation: 7,311

Post » Tue Oct 27, 2009 6:22 pm

[quote="Mulkaccino":upgynczg]
If APlayerNum=1 then set A's animations to Player1
If APlayerNum=2 then set A's animations to Player2
If APlayerNum=3 then set A's animations to Player3
If BPlayerNum=1 then set B's animations to Player1
If BPlayerNum=2 then set B's animations to Player2
If BPlayerNum=3 then set B's animations to Player3
If CPlayerNum=1 then set C's animations to Player1
If CPlayerNum=2 then set C's animations to Player2
If CPlayerNum=3 then set C's animations to Player3
[/quote:upgynczg]

I think you may be able to make it this:
Set A's animations to: "Player" & Global('APlayerNum')
or.. Set A's animations to: "Player" & Object.Value('APlayerNum')
Whatever the variable is stored on.
And so on for B and C.
Set B's animations to: "Player" & Global('BPlayerNum')
Set C's animations to: "Player" & Global('CPlayerNum')

There might be a better way to do it all around, but I can't think of anything.
Oh, does it need to be 'always'? Can it be 'trigger once'? Or just done at the start of a layout, or something?
B
3
S
1
G
3
Posts: 82
Reputation: 1,189


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 4 guests