Re-using old code?

This forum is currently in read-only mode.
From the Asset Store
5 levels with simple coding Source-code (.c3p) + HTML5 Exported
  • 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

  • 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.

  • 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.

  • 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.

  • 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)

  • 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

  • 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

    http://dl.dropbox.com/u/5868916/ContextMenu.cap

    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.

  • 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.

  • Here's an example of context menus using the box and text objects.

    download: http://dl.dropbox.com/u/5426011/examples/contextmenu.cap

    As for a button menu:

    http://dl.dropbox.com/u/5426011/examples/contexttoolbar.cap

  • 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.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I really like both of those, and will definitely be using some of those ideas in my projects. Thanks R0J0hound

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