Re-using old code?

For questions about using Classic.

Post » Fri Jul 02, 2010 1:40 am

I'm curious about a couple things in Construct.

First off, on the event sheet, I had a condition that had 5 actions attached to it. Later in my code, under a different condition that needed to be run later, I had 5 actions that were identical to the ones in the previous one. The difference was, I needed the same thing to happen again later under a different condition. Is there a way to re-use the same code more than once, like a function, or do you have to do each condition's actions separately?

Kind of along the same line, I have several test cap files that I've been fooling with, each with different aspects of a possible game. Can I somehow copy this already working, usable code into a new cap file, or do I have to re-write the whole thing?

Thanks,
Z
B
3
G
2
Posts: 35
Reputation: 745

Post » Fri Jul 02, 2010 2:45 am

For the first question, you can use the function object to do what you're asking. I don't have time to give a rundown of how it works right now, but it should be pretty easy to figure out.

As for the second question, you're going to have to rewrite all of the code you did. While it may be a pain in the butt, Construct's abilities in that regard are somewhat limited, so there's not a whole lot of choice.
Moderator
B
98
S
36
G
19
Posts: 1,584
Reputation: 17,717

Post » Fri Jul 02, 2010 3:00 am

Here's a bit of info on the Function Object:

http://sourceforge.net/apps/mediawiki/c ... ion_Object

A tip: If you're using functions that take parameters, take careful note of the Calling from expressions section. It's nicer than setting parameters individually in separate actions.

As linkman said, Construct is somewhat limited in pasting events between projects. It will drop any references to objects in the copied from project that aren't in the project being pasted to. This will usually make a mess of them, leaving very little of the original events.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Fri Jul 02, 2010 4:25 am

Okay, I've been working with the functions, and I did get it working, all except the return value. I've got a function that sets the return value to either a 0 or a 1 depending on the outcome of two sub-events, but how can I use that value in a separate condition, after the function has been called?

EDIT: or, do I have to assign either an object or global variable to whatever the outcome was in order to use that value in a condition?

EDIT again.. I figured out a way to do what I was after through an expression in a compare condition, but I'm still curious if there's another way.
B
3
G
2
Posts: 35
Reputation: 745

Post » Fri Jul 02, 2010 4:53 am

You can create an alias function that you may use as a condition. Your function returns either 0 or 1, so you could setup the alias like this:

+On function "I'mReturningOne"
->Function: Set return value to 1

+On Alias "BeTrueIfOne"
++Function.I'mReturningOne equal to 1
->Function: (Alias) Return true

Then you can call the alias as a condition:
+"BeTrueIfOne" (isolated)
->Do amazing things if return value is one (= alias returns true)
+Else
->Do even more fun stuff if return value is zero (= alias returns false)
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Fri Jul 02, 2010 5:03 am

Hehe, ok I'll give that a shot too.

Last thought for the night: I love right click context menus. Any suggestions on how to approach something like that? I checked out the listbox and such, but it seems those are rather limited, and as far as I could see, items couldn't be added or removed from the list on the fly, unless I'm missing something.

I was hoping for a right click on a unit opening a menu, selecting amazing things to do based on what kind of unit it is, but only using one dynamic context menu that is populated by a function. That's the dream anyway.

Thanks for all the help again today guys, I'm having fun with this :)
B
3
G
2
Posts: 35
Reputation: 745

Post » Sat Jul 03, 2010 3:55 am

Well, my eventual solution to making a context menu would probably end up being a group of objects containing a Text, Box or Panel (for border and background), Sprite (for selection cursor), and perhaps a Dropshadow or another sprite (for optional shadow) object. This would be a bit complex, but have a large amount of flexibility.

But, I was intrigued with the ListBox idea. Actually, the ListBox makes a pretty decent context menu. I made one in nine events. Could be less, depending upon your needs. While it is bit less fleshed-out than I liked, it's rather simple to implement.

I included comments in the .cap

[url:4g5wxlg5]http://dl.dropbox.com/u/5868916/ContextMenu.cap[/url:4g5wxlg5]

If nothing else, it's an interesting exercise :) I may end up making the more complex one at some point. I like those menus, too.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Sat Jul 03, 2010 1:52 pm

Silent,

I did actually get the context menu working. I used the list box, and in fact, it worked very well. I was loading the menus from a file, with plans of later being able to edit them from inside the game through options.

As I worked with it though, I decided that the menu popping up over the play field didn't really feel like it belonged, so I took it out. Instead I started using an approach where selected objects are selected mainly by left click and a selection box, and are given commands by right click. Any command that can't be dealt with in this way will be added to a menu in the HUD.

That's one part I still haven't decided on though. I'm going to have sprite buttons on my hud bar, but they won't always be the same for each unit. I'm trying to figure out how I should group them so I have an easy way of switching from one group to the next. I'm leaning towards families, but I wonder if I'd be better off with them on a different layer.
B
3
G
2
Posts: 35
Reputation: 745

Post » Sat Jul 03, 2010 8:37 pm

Here's an example of context menus using the box and text objects.
download: [url:31v9xhfo]http://dl.dropbox.com/u/5426011/examples/contextmenu.cap[/url:31v9xhfo]

As for a button menu:
[url:31v9xhfo]http://dl.dropbox.com/u/5426011/examples/contexttoolbar.cap[/url:31v9xhfo]
B
79
S
24
G
52
Posts: 4,725
Reputation: 39,713

Post » Sat Jul 03, 2010 10:04 pm

That demo was pretty slick all around, R0J0hound. There are some ideas in there that I hadn't thought of. Thanks for posting them.

I see that the way you visually constructed them was pretty much as I would have. In lieu of an object that does such a thing, these objects allow some nice customization with colors, fonts, opacity, sizing, and more. This would allow them to fit into the theme of the game a bit better than a basic one.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest