Don't DO This:

Discussion and feedback on Construct 2

Post » Tue Sep 09, 2014 7:09 pm

Hey everyone,

I have a story and something you should avoid.

When writing events in construct, you can refer to an object that has no instances. This is fine if you want to read some default instance variable or behavior value off it - such as max speed or gravity power. But what you can't do is change something that doesn't exist. For example:

On start of layout -> set playerObject physics stepping mode to framerate independent...

more events, more events, more events... blah blah blah...

Create playerObject at playerSpawnpoint.

The result of this is that you do not have framerate independent physics because you couldn't change the physics behavior from an object that doesn't exist. In a large project with thousands of events, errors such as these can be easy to create, nearly impossible to find and worse may remain hidden for weeks.

Don't spend hours of debug time trying to figure out why something doesn't work only to find a mistake like this. The worst part is construct is happy to go along with bad events like this... same thing for calling functions that don't exist. Its like if you told an employee to bring the client in the waiting room some coffee. The employee goes to the coffee room and finds no coffee. Instead of telling you, he just abandons all other subsequent instructions and asks for the next set of directives... Well, he thinks to himself, you are the boss and you must know there is no coffee. What a bizarre instruction. Oh well, best to carry on.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Tue Sep 09, 2014 8:40 pm

I do not see exactly the problem, of course you cannot reference a non yet existing instance (could you get me a kfvoenxlexkov at the coffee?... a what?)

However if you can reference values out of it, this is something I would consider as a bug.
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Tue Sep 09, 2014 8:56 pm

I think he's implying that you can accidentally try to set values to non-existent items and nothing happens... I presume. I've done this and it's a pain trying to chase down exactly what went wrong. I find it happens a lot for me in sequenced statements with conditionals.. you spawned in one sub-event but set parameters in another, etc.

C2 does not handle non-fatal errors well.
B
7
S
3
G
1
Posts: 184
Reputation: 1,352

Post » Tue Sep 09, 2014 9:06 pm

@Aphrodite - but that's just it - you can reference a non existing item in construct. You shouldn't, but you can... and construct 2 doesn't tell you when you do... I nice log file in debug mode might be nice... Like "Hey, I called this function, but it doesn't exist, just so you know".

@rho - that's it in a nut shell.
Image
B
33
S
11
G
2
Posts: 564
Reputation: 5,153

Post » Tue Sep 09, 2014 9:13 pm

Try removing those objects from the layout.
Image ImageImage
B
169
S
50
G
169
Posts: 8,285
Reputation: 108,214

Post » Tue Sep 09, 2014 9:45 pm

@Aphrodite and @newt: I don't think you guys get the issue:

He is accessing properties of an object that exists, but has no instances at the time of the referencing.

think of it like this:
  • <There are zero balls>
  • Make all balls be blue
  • Create a ball
  • Ball unexpectedly comes out white

He was writing to the phantom "default instance" and expecting newly spawned instances to be created with whatever properties he set to be the default. This behavior would make sense, think of it as changing the defaults of an object.

However, construct doesn't work that way. You cannot "write" default properties at runtime. Thus, you get a logic error that is hard to spot.

I don't consider this a "bug" per se, but @Ashley might want to do something about this. Either make it so that you can write defaults, or make construct give an error if the object you're writing to has no instances (I think this last approach would be very hard to implement).
B
36
S
8
G
8
Posts: 532
Reputation: 6,903

Post » Tue Sep 09, 2014 9:52 pm

I do not think writing default values would be good... and that would be a breaking change.

Also, that implies that C2 does something to an object when no instances are picked, which I think would be pretty hard to implement correctly, since that goes against the basic way C2 actually works, nobody wants actions to occur to all newly created objects if none are picked or exists I think, they would rather skip that.

The on created event exists for this purpose already. As well as the .count value.

@Fimbul
Game design is all about decomposing the core of your game so it becomes simple instructions.
B
53
S
22
G
18
Posts: 2,122
Reputation: 17,123

Post » Tue Sep 09, 2014 11:06 pm

This sounds like in a normal computer language where you are using a ForEach and then wondering why the code inside the ForEach loop didn't run when the list of objects in the ForEach had a count of zero ... the loop never runs if the count is zero because it only runs For Each Object.

Each object type in C2 has a Count property... you can do something called "assert" which is what software developers do to check to see if things are in the state that their code will expect them to be.

For example you can add an event that checks the Sprite.Count before setting values to Sprites and if it is zero then do a Browser.Log to alert yourself that the code you thought would make some change to a list of Sprites has no Sprites to act on.

This is all just normal coding practices done all the time.
B
21
S
5
Posts: 196
Reputation: 1,974

Post » Tue Sep 09, 2014 11:11 pm

It has to be open ended, otherwise you would not be able to make events that reference an object which may not exist yet.
If I were to make an event that said every x seconds sprite set y to self.y+1, as it is now it wouldn't do anything until one existed.
Error reporting really wouldn't add to ease of use in the long run, as you would have to have some silly event that asks, hey sprite do you exist?
But the kicker is you couldn't do that either because you want to make it so that you can't reference a nonexistent object.
Image ImageImage
B
169
S
50
G
169
Posts: 8,285
Reputation: 108,214

Post » Tue Sep 09, 2014 11:47 pm

I don't think anyone should be surprised that trying to modify an object that doesn't exist does nothing. However the Physics example in the OP is slightly different because it modifies a global state that affects everything, but still needs an instance to exist in order to do anything. This is a shortcoming of the event system, and currently there's no architecture in place that could make that work, so I don't think we could fix it easily.
Scirra Founder
B
395
S
232
G
88
Posts: 24,368
Reputation: 193,746

Next

Return to Construct 2 General

Who is online

Users browsing this forum: Artpunk, jobel and 5 guests