How do I convert a string to an instance variable?

Get help using Construct 2

Post » Tue Jul 28, 2015 8:19 am

I have an instance variable on my Player object called "Nearby".... it is a text value that records which "House" object is closest (at the current time it's just preset to the only house, "House_01"). The house object also has an instance variable, "Entry", which is a preset number supposed to determine the speed the physics object Player enters the house.

What I am trying to do here is use the combined strings to return the "House_01.Entry" instance variable value as a number... eg. the string (Player.Nearby)&".Entry" should return the text string "House_01.Entry", which when tested in game with a text object is exactly what happens. What I actually want it to return is the numerical value of the instance variable "House_01.Entry" which I have currently preset to 20, but all I can get it to do is provide me with the text name. when I put int or float in front of the string eg. float((Player.Nearby)&".Entry") and test it the only value I get in return is 0 and never the 20 I have preset the instance variable to be. However, whenever I directly input "House_01.Entry" into the equation without using the string it reads it's numerical value.

Is there any way that my string can be read as an instance variable/object expression so I can use it's value in my actions?
Last edited by dirtylook on Tue Jul 28, 2015 10:12 am, edited 2 times in total.
B
4
S
1
Posts: 15
Reputation: 382

Post » Tue Jul 28, 2015 10:00 am

you have to move the last ) to just before the & and remove one pair. Like so;
float(Player.nearby) & ".entry"
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Tue Jul 28, 2015 10:10 am

nimos100 wrote:you have to move the last ) to just before the & and remove one pair. Like so;
float(Player.nearby) & ".entry"


I want the "Player.Nearby" (which is set to House_01) and the ".Entry" string to combine to create "House_01" & ".Entry" which I hope will give me the "House_01.Entry" value of 20... moving the brackets as you suggested (unless I'm doing it wrong) results in the text displaying "0.Entry" and not 20.

I don't know, I'm not a coder so this seemed like a logical way to do things for me, but is it just that you can't add strings together to create a recognized object expression?
B
4
S
1
Posts: 15
Reputation: 382

Post » Tue Jul 28, 2015 10:57 am

dirtylook wrote:
nimos100 wrote:you have to move the last ) to just before the & and remove one pair. Like so;
float(Player.nearby) & ".entry"


I want the "Player.Nearby" (which is set to House_01) and the ".Entry" string to combine to create "House_01" & ".Entry" which I hope will give me the "House_01.Entry" value of 20... moving the brackets as you suggested (unless I'm doing it wrong) results in the text displaying "0.Entry" and not 20.

I don't know, I'm not a coder so this seemed like a logical way to do things for me, but is it just that you can't add strings together to create a recognized object expression?

It seems like you might be over complicating things if what you need is to be able to recognize each individual house from each other. Since you already know that the object in question is a house, and you want to know the entry. Why not just add a variable called "Entry_ID" as a number and then you can just set it to 20. Then you don't need to combine any strings or do any conversion. You can just use this "Entry_ID" to select the correct one.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Tue Jul 28, 2015 11:56 am

nimos100 wrote:
dirtylook wrote:
nimos100 wrote:you have to move the last ) to just before the & and remove one pair. Like so;
float(Player.nearby) & ".entry"


I want the "Player.Nearby" (which is set to House_01) and the ".Entry" string to combine to create "House_01" & ".Entry" which I hope will give me the "House_01.Entry" value of 20... moving the brackets as you suggested (unless I'm doing it wrong) results in the text displaying "0.Entry" and not 20.

I don't know, I'm not a coder so this seemed like a logical way to do things for me, but is it just that you can't add strings together to create a recognized object expression?

It seems like you might be over complicating things if what you need is to be able to recognize each individual house from each other. Since you already know that the object in question is a house, and you want to know the entry. Why not just add a variable called "Entry_ID" as a number and then you can just set it to 20. Then you don't need to combine any strings or do any conversion. You can just use this "Entry_ID" to select the correct one.


I think you may be right. I eventually wanted player object to go into the nearest house and just wanted one event to determine which house was closest and change the player.nearby variable to the name of that house, and then one event to push the player into that house at it's specific .entry speed... so I thought taking this route would be neater, but I guess not. I've in the meantime made some messy looking work arounds, I just wanted to know if it were possible to make strings read as object expressions. Thanks for your help.
B
4
S
1
Posts: 15
Reputation: 382

Post » Tue Jul 28, 2015 12:04 pm

dirtylook wrote:I think you may be right. I eventually wanted player object to go into the nearest house and just wanted one event to determine which house was closest and change the player.nearby variable to the name of that house, and then one event to push the player into that house at it's specific .entry speed... so I thought taking this route would be neater, but I guess not. I've in the meantime made some messy looking work arounds, I just wanted to know if it were possible to make strings read as object expressions. Thanks for your help.

But you can use "Pick Nearest/Furthest House" using the Player.X Player.Y to get it. Then you can get the Entry_ID of that house if you make a variable. That is probably the easiest.
B
44
S
11
G
2
Posts: 1,182
Reputation: 6,848

Post » Tue Jul 28, 2015 12:46 pm

nimos100 wrote:
dirtylook wrote:I think you may be right. I eventually wanted player object to go into the nearest house and just wanted one event to determine which house was closest and change the player.nearby variable to the name of that house, and then one event to push the player into that house at it's specific .entry speed... so I thought taking this route would be neater, but I guess not. I've in the meantime made some messy looking work arounds, I just wanted to know if it were possible to make strings read as object expressions. Thanks for your help.

But you can use "Pick Nearest/Furthest House" using the Player.X Player.Y to get it. Then you can get the Entry_ID of that house if you make a variable. That is probably the easiest.


I think I will do just that. Thanks so much for your help, it's greatly appreciated :)
B
4
S
1
Posts: 15
Reputation: 382


Return to How do I....?

Who is online

Users browsing this forum: Yahoo [Bot] and 28 guests