Alternative to Get Object By Name

Get help using Construct 2

Post » Thu Mar 07, 2013 5:45 pm

I ran into the need for a getObjectByName() a couple of days ago trying to store spawn point names with doors. I was able to work around it by approximating a point and then using "Pick nearest to" to get the UID. Breakable if I change my layout in any meaningful way, but workable.

But now I'm in a spot where that won't work. I'm storing state data for my game in Dictionaries. 1 for each state/level (dialog lines, object positions, etc) with a naming convention. Since the dictionaries aren't physically in the layout, I can't think of anyway to detect them. Even if I could, I know the exact name of what I want and it seems like I should be able to just get that Dictionary by name.

something like: getObjectByName("Level_010").spawnX

I'm figuring that if there isn't a getObjectByName() function out there, that there must be an easy alternative to do this simple procedure. When I scraped the forums/manual/tutorials, all I could find was "no, you have to use the UID or IID". I'm not seeing how to do that in this particular case.

How do I?! GabeOfThrones2013-03-07 17:48:53
B
8
S
1
G
1
Posts: 37
Reputation: 1,326

Post » Thu Mar 07, 2013 5:58 pm

Wouldn't Dictionary(levelNumber).Get("Stuff") work? That is the IID way. Assuming you have one dictionary representing each level, if you have multiple you could have separate dictionary objects for each dictionary that you need for one level.vee412013-03-07 17:59:15
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Thu Mar 07, 2013 9:25 pm

Thanks for the reply, vee41!

What is levelNumber in your example? It seems like that would still have to be a label of some sort. That's what I'm getting stuck on. If it's the IID, I have no idea what that is since it's created at run time.
B
8
S
1
G
1
Posts: 37
Reputation: 1,326

Post » Thu Mar 07, 2013 9:35 pm

Due to the way minification works, you can't expect to call a name like that.
What you have to do is use picking, to get the object into the selected objects list.
You can use uid, as well as iid, however those are prone to change at runtime. I would suggest the simplest method... using a variable.
Like: If global variable makeObject = 1, system make sprite at x,y.
Its a little long and draw out, but its hard to screw up.
Image Image
B
161
S
48
G
91
Posts: 7,358
Reputation: 67,271

Post » Fri Mar 08, 2013 1:06 am

Thanks, newt. I get what you're saying about minification and the need to cut out the name... I'm not sure I understand your variable example though. Here's exactly what I'm trying to do for more clarity:



I know the Family, I need to get the Dictionary Object so I can set the dialog to the instance variable value. I have been trying to store the Dictionary name to the global variable, which isn't working...obviously. I'd store its UID if I knew what it was...

How are you suggesting I use a variable to solve this? Am I missing a key ingredient? Thanks again for your help!
B
8
S
1
G
1
Posts: 37
Reputation: 1,326

Post » Fri Mar 08, 2013 1:19 am

Give the family an instance variable "name" and set it to "level1", "level2", "level3" etc for each dictionary. Then you can pick the dictionary whose name value = "level" & currentLevel.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Mar 08, 2013 4:58 am

Thanks, Ramones! That makes sense. Although it seems like accessing the name would be basically the same thing (grumble).

For some reason, I can't seem to find the expression to make that work though. I see how to check the key on the family, but not "some generic object". After reading the Families section, this seems like it should work... but it doesn't.



Sorry this thread won't die. I think it's just down to syntax at this point though.
B
8
S
1
G
1
Posts: 37
Reputation: 1,326

Post » Fri Mar 08, 2013 11:25 am

levelName should be a family instance variable and not a dictionary key. And then the condition would be "levels -> compare instance variable".

B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Post » Fri Mar 08, 2013 5:05 pm

OK, got it working. I'm indebted to you on two problems now, Ramones! I have to say that the community on these forums is super helpful and fast to react! They should have a badge for "props for helping" that users could give to their benefactors.

I got it working this time because I had an instance variable called "playerChatter" and just used Levels.playerChatter instead of Levels.Get("playerChatter").

The thing I was missing was that I mistook instance variables for keys in the case of a dictionary for some reason. Dictionaries seemed like a good solution to store static data like an in-engine version of a JSON file. Now that I'm looking at it, it seems that you can only set keys using in-game actions, so I can't set them up offline. And they must be in memory the whole time? Is that right? If that's the case, maybe I'd be better off using JSON or XML and loading them as I need them. Instance variables are easier, but it's a lame way to store dialog, and a waste of memory.
B
8
S
1
G
1
Posts: 37
Reputation: 1,326

Post » Fri Mar 08, 2013 6:56 pm

Yeah you can't add keys to dictionaries in the editor, you'd be better off using project files and JSON/XML like you said.
B
54
S
28
G
18
Posts: 1,520
Reputation: 25,020

Next

Return to How do I....?

Who is online

Users browsing this forum: SpartCrafter and 22 guests