Sprite objects vs. instances

Discussion and feedback on Construct 2

Post » Fri May 31, 2013 5:56 pm

Hi guys,

First post here as I'm going through the system and coming up with ideas, best practices, and generally getting my head around choices when building games. As I primarily come from a programming background I tend to lean towards that for approach. So this begs the question of objects vs. instances.

Obviously if I have 100 enemies in the game I'm going to spawn an instance through the system. However what about the Player? In many tutorials people are just adding a sprite to the game, calling it player and putting it on the board. Even though I have one player in the entire game I wonder is it better to create an instance (so I set all my dynamic properties like health, etc.) instead of using the sprite object?

I realize the name object and instance here are Construct terms and don't necessarily adhere to an object and instance in programming languages but just wondering what the best approach here is (and if there is a performance hit between the two).

Thanks!
B
17
S
5
G
2
Posts: 39
Reputation: 1,846

Post » Fri May 31, 2013 6:35 pm

I'm looking best practices like this thread too.

I doubt we can compare the performance between 100 objects as sprites with different animations against one object with 100 instances with folder of different animations.
B
99
S
35
G
29
Posts: 3,139
Reputation: 28,421

Post » Fri May 31, 2013 6:49 pm

@Joannesalfa I agree the comparison might be moot. I just see everyone using the sprite object as their player for example and wonder about the whole object vs. instance. Again, it might be negligible with only a few objects but 1000s of instances vs. 1000s of objects? Maybe something there.
B
17
S
5
G
2
Posts: 39
Reputation: 1,846

Post » Fri May 31, 2013 8:40 pm

Objects and instances are the same in Construct as in a programming language.When you add a sprite to a project you are adding a object type and one instance of the sprite to the layout. You can have as many sprite object types as you want but generally I find it easier to make the events for a lower amount of object types.
B
92
S
32
G
109
Posts: 5,291
Reputation: 70,993

Post » Fri May 31, 2013 8:59 pm

[QUOTE=R0J0hound] Objects and instances are the same in Construct as in a programming language.When you add a sprite to a project you are adding a object type and one instance of the sprite to the layout. You can have as many sprite object types as you want but generally I find it easier to make the events for a lower amount of object types.[/QUOTE]

So if I read this correctly, objects in Construct are not classes like in a programming language and Construct instances are not like programming objects? When I create an object (through the Spawn object) I'm always creating a new object with it's own set of instance properties? So every object just has a unique set of instance properties but otherwise is an object?

The manual instances have the properties, the objects do not, but I don't see how you create an instance at runtime (there's no Create instance action in the System but there is a Create object).

To summarize if I had say a sprite defined as Enemy and an instance variable of Health on the Enemy if I spawn 1000 objects it's the same memory footprint (1 byte [Health] X 1000 items) as 1000 instances of Enemy (regardless of how they were created)

Thanks for the clarification.
B
17
S
5
G
2
Posts: 39
Reputation: 1,846

Post » Fri May 31, 2013 10:36 pm

@bsimer

I read somewhere in the forums that having more sprite objects is more CPU intensive then having more instances of one sprite object. So having 1000 different sprite objects affects the memory usage more then having 1000 instances of one sprite object that is recopied and reused.

Also I would think an object is equivalent to a class in programming. As for instances, well an instance is just the amount of copies you have for one particular sprite object. So lets say I created an object called "enemy", I can then have 10 instances of that sprite object by spawning/creating it 10 times on the map.

You can also specifically choose an instance of a set of objects.

Im not sure what you mean by manual instances, but objects do have properties as well.

If you want to create an instance variable for an object at runtime you have to click on your sprite object then look to your left menu to where it shows "Instance Variables" and assign them from there.

If you want to set a value to that particular instance variable you have assigned, then select the sprite object you assigned your instance variable to in the event sheet and use the "add actions" command to select your object and select the instance variable command.

You can then set your values (such as 100) to those instance variables by creating an event, for example

Event---> on start of layout,
        Action-------->Set "Hero" instance variable "health" to 100


retrodude2013-05-31 22:48:03
B
18
S
4
G
3
Posts: 436
Reputation: 4,801


Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 23 guests