Can Function make my code more efficient?

0 favourites
From the Asset Store
Two levels with two sistem Wave. with Source-code (.c3p) + HTML5 Exported.
  • The above code is working just fine. However, I'm wondering how I can make it more efficient. I'm still learning about functions, and I'm curious if I can enhance their efficiency using a function. Below is how I started. Any suggestions?

  • You can usually start refactoring code/events by describing what you are trying to do in as few words as possible.

    For me at least, it's not entirely clear what your desired result is just from looking at the events, so it's difficult to give advice about how to make it more compact.

  • Giving advice is a thankless task.

    I would follow the wish "if it works you don't need to get in the way".

  • Hi, oosyrag, Thanks for your response,

    I think the overall code feels a bit lengthy. For example, there's a repetitive pattern eg.

    "+ System: Pick BUTTON where BUTTON.AnimationName = String_LV"

    However, some of these events also include a "Trigger once" condition. I'm not certain whether we can refactor these into a function to improve efficiency.

    Edit 1.

    Speaking about the code, its purpose is to auto Spawn icons and text on a button on the run time. When the animation name of the button matches the value of "String_LV",

    and for the text, "Button_Name.Button_Name_iv" matches the value of "String_LV", it will Spawn and reposition itself to the button.

    (TextVisibility_iv, Clickable_iv & ICONVisibility_iv are Booleans, if it is true, then it will be executed, in the given code, all are true)

    Having said that the same code will be used in multiple buttons (same button sprite but different instance of button), with only variations in local variables, I posted this question to explore avenues for enhancing efficiency.

  • Giving advice is a thankless task.

    I would follow the wish "if it works you don't need to get in the way".

    😅😂

  • I still have no idea what your buttons are for.

    Why are you picking buttons? Buttons are usually for pressing/clicking. When you click a button it is automatically picked. An on touch or on click is already a trigger action and doesn't need trigger once.

    If you're using a single button object for multiple instances of buttons that are identified with instance variables or otherwise, and those buttons do different things, then yes you'll need a separate event for each buttons' actions.

    Also you have a wait for previous action to complete in there, which has no function without an asynchronous action before it. Which you don't, so you're probably misunderstanding something there. All actions are processed in order from top to bottom unless its specified as an asynchronous action.

  • oosyrag

    I still have no idea what your buttons are for.

    Sorry, I have updated my previous response with more details, could you please check that, Thank you.

    Also you have a wait for previous action to complete in there, which has no function without an asynchronous action before it. Which you don't, so you're probably misunderstanding something there. All actions are processed in order from top to bottom unless its specified as an asynchronous action.

    Yeah, I have an Array that is converted from a JSON array. At the start of the layout, when that condition is true, it's unable to fetch the Array, because for it needs some more time. So, I thought that instead of use wait 1 sec action, I would use this "wait for previous action ", but it turned out it is unnecessary, thanks for pointing it out.

  • So all you’re doing is creating icons and text on buttons when they are created depending on some Boolean instance variables?

    Any reason why the text is pulled from an array instead of just being put in an instance variable of the button?

    Here’s a possible way you could do it.

    button: on created
    -- button: boolTextVisible
    -- -- create text at button
    -- -- text: set text to button.text
    -- button: boolIconVisible
    -- -- create icon at button
    
    on clicked on button
    -- call shimmyAndShake
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks, R0J0hound, thanks for your suggestions.

    The three areas from which I could extract the text, the Array, Button Animation Name, and the local variable "String_LV"—all contain the same string. That's why I didn't create an instance variable, though I can understand why you're recommending that. I'll attempt to implement your suggestion to streamline the code.

    Initially, I believed it could be accomplished using a function due to the repetitive pattern, such as:

    "+ System: Pick BUTTON where BUTTON.AnimationName = String_LV"

    However, it appears to me now I might have overcomplicated things. I'll try to simplify first and then utilize a function. Thank you to everyone for your time.

    :)

  • Construct 3 is really fun.

  • I'm simplifying my question now. This differs from my initial query in this thread. Rather than starting a new thread, I'm posting it here.

    The logic I applied here is to create buttons, place them dynamically side by side with padding, and adjust their positions according to the base. The aim is to center their alignment in real-time, similar to how we handle the text alignment, but here, instead of text, it is the sprite.

    Please download the C3P file and preview it. This will make it easier for you to understand what I'm trying to say.

    The below code I've written is functioning,

    I attempted it using a function, but it didn't work

    Also, this part

    Is there a way to further simplify this? Currently, for each of the n numbers of buttons, I need to apply the compare logic separately. Is there a more efficient approach?

  • Use loop and the loopindex system expression to repeat a set of events with an incrementing number value as a variable.

    Specifically if you're creating the buttons dynamically:

    Repeat n times

    Create button at margin+loopindex*(button.width+padding), y

    If they're existing on layout already, you can use for each instead of repeat. But then you might as well just use the layout editor align options to position your buttons.

  • Hi oosyrag, I'm still learning the loop part. Could you please be more specific?

  • My opinion.

    -Code should be written in such a way that you will understand it in a year.

    -Code should be written so that it only takes the right amount of resources.

    All this repetition 60 times a second of the same actions I do not understand.

    -It would also be nice if the code could be extended without redesigning it.

    Here is my solution to this problem.

    dropmefiles.com.ua/en/mZEfP4c6

  • igortyhon's example is solid.

    I just wanted to emphasize what he said about not running events every tick (60 times a second on most monitors). If something isn't expected to change every tick, then it's event should not run every tick. Only run the event as needed when something needs to be changed. Try to use triggered events for everything.

    In this example specifically, you don't need to update the buttons until you click to make a new button.

    Also in general, you don't need to use functions for most things in an event based system like Construct. They definitely have their uses, such as setting return values for what basically amounts to a custom expression, or repeating the exact same set of actions under different conditions, but when starting out I recommend working with the basic condition-action event blocks until you run into a situation you really NEED functions. Otherwise, it is just added complexity that you can get unnecessarily stuck on while learning.

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