How to "construct" an object name using variables

0 favourites
  • 14 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • The subject doesn't explain it well but I will do my best here. So basically, let's say the user inputs number 75 and this gets saved to a variable called "userSelection". I want to be able to do something to an object called "item&userSelection" (item75). For example, spawn object at "item&userSelection&.X" (item75.X) if the user enters 75. I've tried everything I can think of and can't find any previous forum posts about this. Sorry if it's unclear, I'm not good at explaining stuff sometimes...

    BTW, here is a picture of one of the things I tried and may help explain what I want to do.

    https://drive.google.com/file/d/0BwqZI88ckbEYODFEOHJSU3pSTHM/view?usp=sharing

    Thanks Heaps!

  • Does no one know how to do this? Or can anyone suggest a better way of doing it if you can't?

  • I cant see your image all I get is

    "

    Google Drive

    You need permission

    Want in? Ask the owner for access, or switch to an account with permission. Learn more

    "

  • I've tried regenerating the link, any luck now?

    (https://drive.google.com/file/d/0BwqZI88ckbEYODFEOHJSU3pSTHM/view?usp=sharing)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Lol... uhh that image doesn't make sense to me for the question...

    I would consider doing "Item"&userSelection when referencing it though.. then you can pop it out as a string should read Item## and that can be used to refrence in an array/dic if that's what your going for.

  • and then "Item"&userSelection&".X"

  • Thanks for the suggestions but I've tried them. I just want to grab the X and Y Co-ords of a different sprite depending on the user's input. I've tried many different combinations of "Item"&userSelection&".X" but it doesn't work. I've tried assigning the result of that to a text object and it shows the correct "item75.X".

    When I try to path find using the result of that it says there is an error because it's obviously not a co-ord. When I put "item75.X" straight into the pathfind function it of course works fine. This makes me think construct 2 is treating the concatenated object name like a string and not an object name, how do I get construct 2 to treat it like an object name? A convertStringToObjectName() function would be great!

    EDIT: I forgot to mention that I simplified the image. (Link again: https://drive.google.com/file/d/0BwqZI88ckbEYODFEOHJSU3pSTHM/view?usp=sharing)

  • I hate to seem like a pest but I really need help with this. It's part of a school assignment which is due soon. It seems like a pretty fundamental flaw not being able to "construct an object/variable name based on user input" - I've done it in more than one other programming language.

    I've also tried just using the number, no concatenating or anything but the same thing happens, Construct 2 continues to treat it like a string, not an object name so I can't access any of it's variables like 42.X or 42.Y for example.

  • Construct 2 isn't a programming language. You can't do that sort of thing with it. You'll have to think of an alternative way to store and retrieve your values.

  • I'm afraid the image still doesn't make much sense.

    What you are trying to do it seems is select a specific instance, according to a user input.

    In Construct 2 that's called picking, and is a basic of how events work.

    You can do it by comparing the value of an instance variable, and making sure that each instance of the object type has a unique value.

    Construct 2 is actually it's own sort of programming language I would dare to argue in opposition to the previous commenter.

    But still it has its own ways, and needs a bit of habit to get into.

    In the end, I feel like you are asking the wrong question because of your habit of other programming languages.

    What is it exactly you are trying to achieve in the first place (the general mechanic, not the "construct an object with variable").

    If you're trying to get some selected object to move to a certain position according to a user input, here is an example capx that could do the work.

    Example

    In this example each "Object" instance is create with a unique "ID" instance variable value.

    From there, when the user inputs a number from 0 to 10 (because on start of layout only 10 more instances are created) and click the button, only the instance with the entered ID is selected and its pathfinding behavior set to move it to the EndGoal.

  • [....] I want to be able to do something to an object called "item&userSelection" (item75). For example, spawn object at "item&userSelection&.X" (item75.X) if the user enters 75. [...]

    Ok. So your problem is that you want to pick an object by name which can be "randomly" generated. Here we have bad and good news. The bad news is that you cannot pick an object by name in Construct2. The good news is there is a comfortable workaround. Kyatric already explained that a bit, but lets do it in details.

    First think of C2 object more like of a class in programming. It's not really an instance. The one you have on your layout is an instance already and has some unique ID. But you want to call it by name - no problem. All you have to do is to name it first.

    So go to your object settings and add a variable "instanceName" which should be a text type. Now you can name any instance (new on the fly, or name the one on layout manually). Once you have it named you are ready to pick it using "System -> Pick by comparsion" function. This function requires two parameters which will be compared. So as a first parameter you should type yourObjectName.instanceName and as a second one "item"&userSelection.

    And here you go, you just picked an instance you needed and you can do whatever you want to do with it.

  • Thanks Kyatric that is almost what I want to do but flipped. I have one single object that needs to navigate to many different ones based on user input. (the different objects are all done in the layout editor before runtime)

    Thank-you so much that is almost exactly what I wanted to do. I have a layout with almost 200 objects spread out over it. A single object needs to path find to whichever numbered object the user specifies. I never thought of using an instance variable like that before but it makes so much sense.

    Thank-you so much to everyone who has helped!

  • Glad I could help. Don't forget to show us this game when you're done!

  • I sure will! You know those interactive displays in the shopping centres that tell you how to get to a shop? I'm making one of them for my work but for staff and in the form of a mobile app. I deliver pizza's for Dominos and there are some very large "townhouse" (that's what us Aussies call them) complexes that are difficult to navigate as they are all classed as units with the one street address. So I'm making an app with them all in it so it makes it easier for drivers to get to the customer's door quicker. It also shows the best parking and any tips like how to use the buzzers etc.

    It's still in what I would call alpha at the moment so it's missing lots of things and sometime's I'll accidently break something. You can actually see the latest version here (best on phones): https://jabelone.com.au/dh When it's done I'll post it's link on google play here. My teacher wants me to do another one for the school he likes it that much.

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