Event questions

Get help using Construct 2

Post » Sat Jan 14, 2012 11:04 pm

I just started using Construct2 the other day and so far I am quite impressed. However, the event filtering seems a bit confusing even after reading the manual pages here on the site. Maybe somebody can shed some light on how things are supposed to work.

What I was trying to do is decrement a monster's health counter every tick, check if it's reached 0, then delete the current instance and spawn a couple of new one somewhere else later.

Here's the implementation that works:

(Alive and TimeLeft are instance variables that are initialized to True and 5 respectively. RespawnCount is a global var)

That Foreach loop looks redundant so I tried this approach instead:

The monsters still die after 5 seconds, but even if there were 10 on screen only a single new one will spawn. Why ?

Just for completeness sake- if I were to add a "System- Every tick" condition in front of #10 or as a parent-event of #10 would that have any impact at all ?

(By the way I have a few decades of C++/Java experience- feel free to get as technical as you like- I really want to learn how C2's event loop works)

Some random questions:
The manual mentions local variables, but I don't see an option to insert them. Is that a restriction of the free version ?

There's an "Add Include" command, but how do I create a new Event Sheet for it ?

Exporting to HTML5 (with/without Minify) just shows me a black page when I try to run the index.html in Chrome. Preview from C2 works fine, uploading to Chrome Webstore and running the game from there works as well. However, I would like for this to be an offline-only game; what's a simple,reliable way to achieve that ?

Cross_2012-01-14 23:07:59
B
12
S
3
G
3
Posts: 73
Reputation: 2,647

Post » Sat Jan 14, 2012 11:47 pm

[code]+System: Every tick
-> Monster: Substract dt from TimeLeft
+Monster: TimeLeft <= 0
-> Monster: Destroy
+System: repeat 2 times
    -> System: create Monster on layer 0 at random(layoutwidth),random(layoutheight)[/code]
Should work.
It will spawn 2 monsters anywhere in your whole layout for one monster destroyed.
You don't really need a 'Alive' boolean because the very fact that the instance is not destroyed means that it is alive.

And indeed you don't need any foreach as long as condition on objects only relate to themselves.

Also notice that the repeat loop is nested inside a parent condition.
It's the same idea as

[code]first condition
{
action
second condition
{
    action
}
}[/code]Yann2012-01-14 23:48:51
B
62
S
22
G
14
Posts: 1,481
Reputation: 16,400

Post » Sat Jan 14, 2012 11:52 pm

First part I'm not sure what the problem is if you have it working. I'll need more details or to see the capx.

Local variables are nested under a parent event, if you make a global var and drag it under a parent event it will make it local.

To include a event sheet, on the folder icon in your objects ect bar right click and add another event sheet, ass your events such as controls ect and then on the event sheet you wish to include the other event sheet on right click and select include event sheer.

Welcome to C2 by te way
B
30
S
13
G
7
Posts: 740
Reputation: 7,944

Post » Sat Jan 14, 2012 11:53 pm

Ninja'd by Yann
B
30
S
13
G
7
Posts: 740
Reputation: 7,944

Post » Sun Jan 15, 2012 12:42 am

Thanks smitchell- got the locals & extra event sheets working now.

Is there a way to have something akin to procedures/functions in C2? Let's say every time object A collides with object B I want some complex action to take place. However, when user presses the spacebar I want that same complex action to occur. What's the best way to go about this without copy/pasting everything ?
B
12
S
3
G
3
Posts: 73
Reputation: 2,647

Post » Sun Jan 15, 2012 12:46 am

function plugin

also check out the other plugins, very interesting things therevtrix2012-01-15 00:49:32
ImageImage
B
63
S
19
G
6
Posts: 813
Reputation: 9,112

Post » Sun Jan 15, 2012 1:37 am

[QUOTE=Yann]You don't really need a 'Alive' boolean because the very fact that the instance is not destroyed means that it is alive.[/quote]That variable is used somewhere else in code - once we start the death animation of a monster I set it to false so that no other actions are performed on that instance.

[quote]And indeed you don't need any foreach as long as condition on objects only relate to themselves.[/quote]That's what I thought and which is why I am puzzled about the behavior I described above. The first & second version I posted SHOULD behave the same way in my opinion. Yet the second one never seems to increase that Respawn counter past 1.
B
12
S
3
G
3
Posts: 73
Reputation: 2,647

Post » Sun Jan 15, 2012 1:50 am

Normal, you set Respawn to 0 every tick o.o
B
62
S
22
G
14
Posts: 1,481
Reputation: 16,400

Post » Sun Jan 15, 2012 1:51 am

[QUOTE=Cross_] That variable is used somewhere else in code - once we start the death animation of a monster I set it to false so that no other actions are performed on that instance.[/quote]
You still don't need it as you can check if animation is playing.

[quote]Is playing
True if a given animation is currently set. Animations are identified by their name (case insensitive).[/quote]


Edit: Also for "functions" you can use the plugin, or use groups, activating/deactivating them when you need it.Kyatric2012-01-15 01:53:37
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
258
S
90
G
48
Posts: 7,083
Reputation: 62,882

Post » Sun Jan 15, 2012 3:24 am

[QUOTE=Yann] Normal, you set Respawn to 0 every tick o.o[/QUOTE]
But event sheets run from top to bottom, so it should only be 0 at the very end of the tick.

This is the pseudo-code of what I had in mind:

foreach monster:
Subtract dt from monster.Health

foreach monster:
if monster.alive & monster.Health<=0 :
    destroy monster
    increase RespawnCounter by 1

Repeat RespawnCounter times:
System create new monster

System Set RespawnCounter=0
    
So first we decrease the lifetime counter for all monsters. Then pick the ones that have lost all their health (and have their "Alive" flag set) and kill those. For everyone that got killed increment global variable so we can respawn them later. Next run a loop and create a bunch of new monsters. Once we are done with that reset respawncounter to 0 so no more new monster appear.

I am completely aware of the fact that you can get the same behavior with a dozen different conditions/actions. My question is: why is the example I posted in the OP not behaving as outlined in the pseudo code above? The monsters all disappear but only 1 new monster gets spawned. It's as if the Repeat loop only runs once.
B
12
S
3
G
3
Posts: 73
Reputation: 2,647

Next

Return to How do I....?

Who is online

Users browsing this forum: Myrmex, SnipG and 1 guest