How to "construct" an object name using variables

Get help using Construct 2

Post » Sat Aug 22, 2015 3:37 pm

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!
Last edited by jabelone on Sun Aug 23, 2015 10:17 pm, edited 2 times in total.
B
7
Posts: 27
Reputation: 407

Post » Sun Aug 23, 2015 4:46 am

Does no one know how to do this? Or can anyone suggest a better way of doing it if you can't?
B
7
Posts: 27
Reputation: 407

Post » Sun Aug 23, 2015 7:23 am

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
"
August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,892

Post » Sun Aug 23, 2015 10:16 pm

Last edited by jabelone on Mon Aug 24, 2015 2:09 am, edited 1 time in total.
B
7
Posts: 27
Reputation: 407

Post » Sun Aug 23, 2015 10:26 pm

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.
B
9
S
2
Posts: 54
Reputation: 638

Post » Sun Aug 23, 2015 10:27 pm

and then "Item"&userSelection&".X"
B
9
S
2
Posts: 54
Reputation: 638

Post » Sun Aug 23, 2015 11:30 pm

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)
B
7
Posts: 27
Reputation: 407

Post » Wed Aug 26, 2015 7:27 am

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.
B
7
Posts: 27
Reputation: 407

Post » Wed Aug 26, 2015 7:53 am

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.
B
47
S
22
G
65
Posts: 1,127
Reputation: 38,395

Post » Wed Aug 26, 2015 9:54 am

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.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
294
S
117
G
96
Posts: 7,311
Reputation: 71,086

Next

Return to How do I....?

Who is online

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