How do I convert a string to an instance variable?

0 favourites
  • 7 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • 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?

  • you have to move the last ) to just before the & and remove one pair. Like so;

    float(Player.nearby) & ".entry"

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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?

  • > 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.

  • >

    > > 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.

  • 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 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)