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

Get help using Construct 2

Post » Thu Jun 23, 2016 6:08 pm

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.
B
9
S
4
Posts: 22
Reputation: 771

Post » Thu Jun 23, 2016 7:06 pm

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.
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Thu Jun 23, 2016 8:09 pm

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).
B
45
S
18
G
8
Posts: 505
Reputation: 8,505

Post » Fri Jun 24, 2016 12:12 am

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.
B
9
S
4
Posts: 22
Reputation: 771

Post » Fri Jun 24, 2016 12:25 am

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.
Mistakes were made.
B
53
S
27
G
114
Posts: 1,695
Reputation: 64,438

Post » Fri Jun 24, 2016 1:02 am

@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:

Image

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
B
98
S
34
G
20
Posts: 438
Reputation: 17,825

Post » Fri Jun 24, 2016 2:56 am

@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
B
9
S
4
Posts: 22
Reputation: 771

Post » Fri Jun 24, 2016 2:59 am

@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
B
9
S
4
Posts: 22
Reputation: 771

Post » Fri Jun 24, 2016 3:11 am

C2 always runs sequentially, unless you have Waits, which purposely delays code.
ImageImageImage
B
71
S
22
G
247
Posts: 3,753
Reputation: 137,552

Post » Fri Jun 24, 2016 3:31 am

nikolatesla20 wrote: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.
B
45
S
18
G
8
Posts: 505
Reputation: 8,505

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests