Quick question about instances and instance variables

Get help using Construct 2

Post » Sun Aug 03, 2014 10:57 am

I have the "btn1" sprite object type linked to the "btn1" png image. I want 2 "btn1" instances on my main menu layout. 1 for play (click and it goes to the next layout) and 1 for quit (quit and it closes the browser). I already know what my events will be but what I am trying to figure out is how instance variables come into play. I have 2 instance number variables of the "btn1" sprite object. 1 is named play, the other is quit.

tl;dr Am I supposed to link which instance goes with which instance variable? If so, how would i go about doing that.

I just picked up Construct 2 yesterday and I don't know if Construct Classic had this functionality because I have never tried it.

Thanks for any and all replies.
LMillz
B
6
Posts: 30
Reputation: 292

Post » Sun Aug 03, 2014 11:18 am

All you need to do is add for example text variable (called "state" on image below) to btn1 object. Then when you place two instances of this object on layout you need to set this variable accordingly for each instance.
Events are similar, just compare value you put in "state" variable and do your actions.
You do not have the required permissions to view the files attached to this post.
ImageImageImageImage
B
157
S
66
G
41
Posts: 2,599
Reputation: 34,825

Post » Sun Aug 03, 2014 11:45 am

Thanks for the quick reply!

Oh I think I get it now. So the instance mapping is done through the event sheet? When, in my game's system setup, do I (should I) set the instance variables?

Also, in the actual editor: if i have btn1 sprite set to layer: "buttons", size: "100, 100" and position: "0, 0" and btn1 text set to the exact same size and position on the same layer, how do I select the sprite if it is behind the text without having to like move it out of the way temporarily if I want to edit the properties of that "btn1" sprite's instance

Thanks again!
B
6
Posts: 30
Reputation: 292

Post » Sun Aug 03, 2014 1:20 pm

I (for the most part) figured out a decent workaround for the second question through the z order panel. As for the first question:

I have a "button" family setup with my "btn1" object in it. I also have a family instance variable named "button". I clicked both instances of my object and set the first one's variable value to "play" and the second to "quit". I went into the event editor and tried setting up the following:


Event
Mouse-On left button click on [family] "button"

Condition 1
[family] "button" value = play
Condition 1 Action
Go to "game" layout

Condition 2
[family] "button" value = quit
Condition 2 Action
Close Browser


As soon as I click DONE in the "Parameters for buttons: Compare instance value" dialog box I get an error that reads:

"Unknown Expression 'play' (or 'quit')
This is not a system expression or variable name in this scope"

What am I doing wrong? I can't seem to figure it out. The instances clearly have the text values of "play" and "quit".

Thanks in advance.
LMillz
B
6
Posts: 30
Reputation: 292

Post » Sun Aug 03, 2014 4:12 pm

@discoverthecurse

Put " " around the text/sting value, otherwise it thinks it's a variable e.g. "play" not play
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
33
S
9
G
7
Posts: 450
Reputation: 6,780

Post » Mon Aug 04, 2014 3:46 am

Wow. Simple fix :shock: :oops:
Haha thanks. I don't know why I didn't think about that.

LMillz
B
6
Posts: 30
Reputation: 292

Post » Mon Aug 04, 2014 4:13 am

Actually the best way to do Buttons is call backs.

Button.callback text variable
Button.param text

OnTouch/MouseDown... on Button
--Function.Call( Button.callback, Button.param)

then on your ES you only need to create OnFunction("callback string whatever")

In fact i've built a GameFrameWork that does this to the Nth degree and I almost do nothing with GUI button.
B
90
S
18
G
9
Posts: 2,455
Reputation: 15,018

Post » Mon Aug 04, 2014 4:38 am

Can you explain that one a little better. I feel like I should follow you but at the moment I don't haha.
B
6
Posts: 30
Reputation: 292

Post » Mon Aug 04, 2014 4:45 am

discoverthecurse wrote:Can you explain that one a little better. I feel like I should follow you but at the moment I don't haha.


It's an advanced concept, you might want to wait until you have the basics. But here is a tutorial.

https://www.scirra.com/tutorials/1063/how-to-improve-your-construct-2-code-with-callbacks
B
24
S
9
G
4
Posts: 1,646
Reputation: 6,596

Post » Mon Aug 04, 2014 5:05 am

I mean I am pretty familiar with the program but I haven't really touched on functions and expressions yet. Kind of working on it on an '"as needed" basis. I'm planning on getting into it pretty soon. Just working on fine tuning some of the cosmetics of the game.
I appreciate all the replies and help from everyone!
LMillz
B
6
Posts: 30
Reputation: 292


Return to How do I....?

Who is online

Users browsing this forum: blackhornet, tarek2 and 48 guests