How do I have Construct interpret string as a variable

0 favourites
  • 4 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • When combining two strings to create a variable, Construct 2 sees it as if it were a string, rather than a variable.

    If I'm not clear, I have an example.

    (Replace int with float, that's probably a mistake)

    Focus on the first action, Set Position, specifically on the X axis part of the statement. This is what I see when I read the line.

    • Combine the strings, "BlockMoveable(21)" and ".BBoxLeft" to create "BlockMoveable(21).BBoxLeft"
    • Convert BlockMoveable(21).BBoxLeft into a float value
    • Subtract 2 from the output

    I'm expecting a number (X coordinate) to come out of this line but instead, only -2 comes out.

    After some staring and thinking, I have concluded that the issue is that "BlockMoveable(21).BBoxLeft" was seen as a string, and therefore equates to nothing when converted to a float value.

    So what I'd like to happen is to have "BlockMoveable(21).BBoxLeft" be seen as a variable, rather than a string. Even if what I thought was the issue is wrong, I still want this end result.

  • Just remove the quotes:

    Set position to (BlockMoveable(21).BBoxLeft-2, .....

    As for the second part (int(selectedObject & ".BBoxTop")), this will not work. You can't refer to objects in C2 by their string name. Same applies to expressions.

    It's hard to tell what are you trying to do from the screenshot. If multiple objects/sprites can be selected, then one possible solution would be to add all such objects to a family. Use SelectedObject variable to store UID of the selected instance.

    Then do this:

    MyFamily -> Pick by unique ID = SelectedObject

    .........HighlightBox Set position to (BlockMoveable(21).BBoxLeft-2, MyFamily.BBoxTop-2)

  • It says BlockMoveable(21) on the first action because that's what the global variable, SelectedObject, contains.

    To keep things simple, I store the name and UID of the object that the player clicks/touches. That way, the last clicked object can easily be referred to. That's why "BlockMoveable(21)" and ".BBoxLeft" are separated, and not just simply written as BlockMoveable(21).BBoxLeft. It would be helpful to just replace the left part to get a different value I'm looking for, such as Player.BBoxLeft, or ButtonPressure.BBoxLeft.

    For the other parts, I'm not planning to allow the selection of multiple objects at once. Otherwise, I would have used the RTS Template as a guideline.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Well, what you are trying to do is not possible, because you can't refer objects by their name.

    By selecting multiple objects I didn't mean "at once", I meant selecting instances of different objects.

    Say, you can select either Block sprite, or Tree sprite or Rock sprite.

    And understandably you don't want to create 3 almost identical events. (if selectedObject="Block" then set position to block.. if selectedObject="Tree" then set position to Tree.. if selectedObject="Rock" .....)

    So the workaround would be adding all three sprites to a family SelectableSprites. And then do what I suggested in my previous comment - pick family member by UID and set position to family instance.

    And one more thing - BlockMoveable(21) means instance with IID=21, not with UID=21

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