2 things: Proper If/Then, and dymanic object collections

0 favourites
From the Asset Store
2D graphics of betta fish collections. Get it now!
  • Hi,

    Man, construct 2 is AWESOME. Loving it so far. Many times when I'm in the editor and wondering - "can I do this?" it shows the creator thought about it already! I love for example when you rename something it automatically fixes the names anywhere it's referenced.

    I have two questions though that I wanted to know

    The first one, is how to do a "proper" if / then. The problem I have sometimes is I have a sub event, and I want actions to run there, but I want ONE of the actions to depend on another condition. So essentially, halfway through the list of actions, I want one of those (or up to 3 or 4) to run under a flag. So say I have "FIRSTTIMESTARTED=0"..I want to run "On beginning of Layout", and run some actions, then another set of actions IN SEQUENCE, I want to run only when FIRSTTIMESTARTED=1, then continue running the rest of the actions after that.

    In normal programming that would look like this:

    if (layout started)

    {

    do something

    do something

    if (FIRSTTIMESTARTED==1)

    {

    do some more stuff

    }

    do rest of stuff

    do rest of stuff

    }

    With the "event based" system Construct 2 has I haven't found an easy way to do that other than put the "do rest of stuff" in its own function, so it can be called from two different places ..basically I want things to run in order, but some of those things only run if a second condition is true, below the first condition.

    Another thing I would like to see is the ability to add things to a family at runtime? For example I have an obstacle generator that right now uses "System.PickRandomInstance" and it picks from a family. This works great. But I'd like to make it so over time in my game I can add additional items to the family at runtime, that way it can act sort of like a "level up" type of thing - over time, the obstacles can get more complex.

    I think you can work around both of these things but just wondering if it's already possible in some easy way and I'm not seeing it.

    Thanks.

  • I got to dissapoint you, thinking in IF/THEN will give you problems.

    Plz read this very carefully.

    https://www.scirra.com/manual/75/how-events-work

    It comes down to this ...

    Actions run IF the condition is TRUE and ONLY on the objects picked by that condition AND on all unreferenced objects when adressed directly in that action. When there is a pick condition for a certain object, but none got picked, the actions have no objects to run on, even if the condition is TRUE.

    As you see (the document explains it better, at least its English) ... if/then is 1/3th of the story. Wish someone would write those two line in good English for me, so i can copy and past it for ever. : )

    About creating from a famely. Creating from a family creates a random member of that family.

    The creation/spawning action creates objects that are directly adressed by that create action, independent of wich objects are picked in the condition. Of course, the condition must be true for the action to run.

    About the 'run under a flag'. Just make 2 sub events. One containing an empty block (or an on every tick condition) and one containing the flag conditions. Actions go where they belong.

    I hope you have fun with construct as much as i do. For me, coding is the game, not that interrested in the games coded.

  • Hi nikolatesla20

    Functions are actually the best route to go. I notice a lot of people stay away from them here, but they are great because they encapsulate your logic. This allows for easy re-use elsewhere (as you mentioned), ease of updating, and a lot less re-work if done properly. I don't really see anything wrong with your if/then, perhaps provide a CAPX for better understanding. I rely on if/then type of functions all the time and don't have issues (I heavily use functions, infact, 99% of my logic is contained in functions).

  • Well, I had already solved the first if/then by using a function. Just doing an empty condition doesn't work because it will run "in parallel" to the actions that have the condition, instead of sequentially.

    I actually did read the event based documentation already, just more was checking if someone knew of some quick way to do it that I didn't think of

    And regarding creating from a Family, I don't need help creating from a family, what I wanted to see is if there was some way to add new object TO a family itself during runtime, so that picking randomly from a Family now had a larger range of objects than it did when the game started.

    If not, then I'll just have to code more random % routines is all.

  • Althought not exactly applicable for your situation, I wanted add that the conditional operator ? is useful for a significant amount of if/then logic, and is something a lot of people miss/don't realize exists. https://www.scirra.com/manual/78/expressions

    Regarding the families, I don't think you can modify families at runtime. A workaround is to create all those families ahead of time, and utilize the relevant family at a particular level.

    If what gets added to the family is not predetermined, that won't work. I imagine an array representing the possible choices would be more suitable/flexible.

  • nikolatesla20

    another way to do what you want with the If/Then is to insert a blank sub-event after the If condition block of code:

    to add the blank condition, right-click on the "On start of layout" event and choose Add, then Add blank sub-event. (you can also just press B on the keyboard). Then all actions added there are still part of the parent event, but run after the If event above it

  • @AllanR - I actually had tried that, but it didn't seem to work like I thought it would? Instead of running the actions sequentially it ran them in parallel. So in other words, it ran the block under "FirstTimeStarted=1" AND ran the blank block at the same time, whereas I wanted that blank block to ALWAYS run, but if the conditional ran, then I wanted to blank to run AFTER it..so, sort of like an "inline if" so to speak.

    I'll give it another try though

  • @ossyrag The array sounds like it may be a way to do it, I'll look into that area and see how to handle arrays of sprites, thanks

  • C2 always runs sequentially, unless you have Waits, which purposely delays code.

  • Well, I had already solved the first if/then by using a function. Just doing an empty condition doesn't work because it will run "in parallel" to the actions that have the condition, instead of sequentially.

    .

    huh? Ill need an example. I've never ran into that issue with my conditions, they are always sequential when I use them. Perhaps I am misunderstanding your use.

    I use them like this:

    Some condition

    ---> function 1

    another condition

    ---> function 2

    ----->function 3

    they are all ran sequentially. If I have a global variable "A" and add 1 each time with output, I would get:

    Function 1 output is 1

    Function 2 output is 2

    Function 3 output is 3

    If your functions are running in parallel that is a difference scenario, such as on clauses...but even then used right, you should be fine.

  • Some condition

    ---> wait 0 seconds

    ---> function 1

    another condition

    ---> function 2

    ----->function 3

    Now function 2 & 3 will execute b4 function 1.

  • Some condition

    ---> wait 0 seconds

    ---> function 1

    another condition

    ---> function 2

    ----->function 3

    Now function 2 & 3 will execute b4 function 1.

    That is because you are using wait... don't use wait, problem solved.

  • I never use wait (other then zero), you wanted an example and you did sound disparate. So i provide.

    Altaught this wait zero is very usefull, but i lost interrest to explain you why.

    I lost interrest to explain you how triggers work (would be a good example too), i lost interrest to explain you how timers work (would maybe be even a better example).

    I also could have explained you when objects actual are created, when they are pickable, would be an awsome example, but hey .....

  • And so many things i forgot, the moment an obstacle map is ready, when a path is ready, when all the load/save modules are ready ...... and so on .....

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 99Instances2Go

    I have no idea what you are rambling on about something unrelated to the OP. Neither do I understand why you posted half of what you posted.

    Was there a post deleted that I am not seeing, because your replies make zero sense. There are no references to what you are replying to, or who you are replying too. I don't see how your reply helps OP in any way...

    I am rather confused about your post in general.

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