Compare Instance Variable Problem

Get help using Construct 2

Post » Fri Apr 19, 2013 7:53 pm

Hello! I'm having issues with a series of events that check an Instance Variable. Basically, for an endless runner type of game, I'm spawning objects at random locations along a path so long as that object isn't already on screen.

To do this, I'm checking an Instance Variable named "Active" for an object, and if the variable is 0 (which is its default setting), spawning the object and then setting its variable to 1. When the object is later destroyed, its variable is set back to 0. Here's what the event sheet looks like at the moment:



It doesn't work. None of the background objects (that require Instance Variable checking) spawn. The foreground objects work perfectly.

Disabling this didn't change anything. Still no background objects spawned:



But disabling this does allow the background objects to spawn. However, it spawns loads of them, since there's nothing to tell C2 to stop spawning the objects:



Finally worth noting - I had originally tried setting the events up like this (using a sub event), but this doesn't spawn any background objects either:



Sorry for using so many images, but I wanted to be as thorough as possible so you know what I've tried Any idea where I'm going wrong?

Many thanks for reading -Silver-2013-04-19 19:54:58
B
15
S
7
G
7
Posts: 250
Reputation: 5,298

Post » Fri Apr 19, 2013 8:21 pm

It`s pretty hard to say what is going on without seeing the whole .capx, as there is lots more going on in your project than those comparisons :-)
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Fri Apr 19, 2013 8:40 pm

@-Silver-   Just guessing here, but if you're destroying the object, and then setting isActive to 0, you're not actually applying the variable to anything (because the object doesn't exist). So the only object that will have isActive = 0 is the one you've initially placed in your layout until it becomes active.

Not sure if this would work for you, but if you only ever want to have one instance of an object on screen at a time, could you not just keep reusing the same object? So instead of destroying the object, set isActive to 0 when it moves off the left side of the screen. Then when it's time to use it again, just move it to the right side?

Not sure if any of that makes any sense in your situation, though ;)rogueNoodle2013-04-19 20:41:11
B
9
S
3
G
4
Posts: 63
Reputation: 3,083

Post » Fri Apr 19, 2013 8:53 pm

@vee41 - There is much more going on, but not in relation to these Objects. That's all there is to them. Other than the part where they get destroyed when they move off to the left of the screen.

@rogueNoodle - I'm setting the variable to 0 first, and then destroying it. Sorry, I should have made that part clearer. Though, even if that part was wrong, they'd need to be created before they can be destroyed anyway, so they'd at least be spawned once before the variable can be set to 1.

You're right about the second part though! I'll try having C2 simply move them, instead of creating a new object and destroying it every time. It just means there will be slightly more object bloat (and each object has a bullet behaviour being acted on every tick too). I'll see how it works though

Still leaves me confused about the above events, because I genuinely can't understand why they wouldn't work.
B
15
S
7
G
7
Posts: 250
Reputation: 5,298

Post » Fri Apr 19, 2013 9:48 pm

[QUOTE=-Silver-] @vee41 - There is much more going on, but not in relation to these Objects. That's all there is to them. Other than the part where they get destroyed when they move off to the left of the screen.
[/QUOTE]

Things like what objects you have in the layout, initial values and stuff actually impact how one would solve this problem, it's hard to say without the view to whole project.
B
24
S
8
G
7
Posts: 756
Reputation: 7,202

Post » Fri Apr 19, 2013 10:03 pm

@vee41 - As I said in the OP, 0 is the initial value, and it isn't changed to 1 until it's spawned (then back to zero before destroying it). Other objects in the layout won't impact the spawning of new, unrelated objects. Other than the destroy section, I've posted and screenshooted everything related to it.

@rogueNoodle - Worked perfectly, thanks! Events now look like this, and there's no noticeable performance impact:



If anyone knows why the original event setup might not have been working, I'd love to hear from you. It's great having a solution, but I'm still not sure where I was going wrong before
B
15
S
7
G
7
Posts: 250
Reputation: 5,298


Return to How do I....?

Who is online

Users browsing this forum: Justin1967, vodka and 20 guests