Programming best (and worst) practices

Discussion and feedback on Construct 2

Post » Fri Feb 15, 2013 3:50 am

Hey guys, these are some great tips! Thanks ;) I've started using some of them and it's helping a lot.
B
7
Posts: 38
Reputation: 476

Post » Fri Feb 15, 2013 8:42 am

If you sit down to write you code with a pen and paper, I find the best method is to work with 'states'.

This is because C2 runs events every tick you need to know what state the program is in at any one time. It just simplifies the workflow.

I create global static variables with each possible state and a global variable containing the current state. I also use states for individual objects.

So instead of activating/deactivating groups use states in my opinion.

Otherwise all the comments above sound good.
B
12
S
3
G
4
Posts: 57
Reputation: 3,186

Post » Fri Feb 15, 2013 10:56 pm

@Kyatric , You should add this to the FAQ or sticky it !
There's some valuable pratics around here !
B
35
S
16
G
16
Posts: 2,222
Reputation: 16,589

Post » Fri Feb 22, 2013 9:15 am

@Yann I agree mostly on everything, but the event sheets vs groups.
Of course it's just a personal opinion, but I think to event sheets like they were classes in OOP. With this in mind, I put everything that concerns a subject (e.g. "Deck" or "Grid") in the specific event sheet, so I know where to find the function I need.
Nothing says, of course, you cannot combine the two when the code in the single "class" becomes messy or too long.

I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
29
S
12
G
4
Posts: 130
Reputation: 4,735

Post » Fri Feb 22, 2013 10:22 am

@Jeff Skyrunner
Interesting comparaison. I tried a bit of Java some month ago, and work a bit with ActionScript and they indeed split every class into files.
But in that case, OOP is enforced by the language, and you tend to build your application around the concept. Whereas in C2, even if you have OOP concepts driving how C2 works, the event sheet itself doesn't really use those concepts (no encapsulation, no real polymorphism,...).

As far as I'm concerned, after working on some big projects, I noticed that I was losing tonnes of time looking for pieces of code in all my event sheets.
And for a practical example: in one of my last project I had a createPolygon function. It was all fine, but at some point I wanted to add an angle parameter. I was a bit unsure, since I didn't remember where I used this function. But I just did a simple search on all my event sheet and voil.
If I had split everything in event sheet I probably would have had about 10 of them, and it's super easy to overlook one event sheet amonst 10 while you are searching.
And finding such bug in my 1005 events would have been a real pain :D
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Feb 22, 2013 12:35 pm

@Yann

Yes, I'm speaking having not yet finished my first project in C2, so I don't pretend to be right... in any case, I work using the "uncompressed" project (so no capx, but all the folders "exposed") and the events sheets opened in the notepad++, to speed up searches and cut&paste operations.

In any case, probably with bigger projects I will came to your point of view, but at the moment I don't find any particular issue in projecting the workspace in this way :)
I'm not a insane, my mother had me tested

I'm an engineer. To save time, just assume I'm never wrong
B
29
S
12
G
4
Posts: 130
Reputation: 4,735

Post » Fri Feb 22, 2013 1:14 pm

@Jeff Skyrunner
Yeah I'm still a bit shy as far as playing with the xml files.
For now I just use .bat to commit some changes when I work on game series.
I just split my event sheet into a common event sheet for the engine, and a specific event sheet for specific differences per games in the series.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Feb 22, 2013 6:29 pm

[QUOTE=Curious Mike] Hi all,

- Save a new version of the project often so you can go back if you mess something up
[/QUOTE]

Instead of doing that, use Git / SVN, way more professional, but mostly important, a bazillion times safer and manageable "There is no software development without a control version system".
B
7
Posts: 11
Reputation: 932

Post » Fri Feb 22, 2013 6:59 pm

[QUOTE=Captain] If you sit down to write you code with a pen and paper, I find the best method is to work with 'states'.

This is because C2 runs events every tick you need to know what state the program is in at any one time. It just simplifies the workflow.

I create global static variables with each possible state and a global variable containing the current state. I also use states for individual objects.

So instead of activating/deactivating groups use states in my opinion.

Otherwise all the comments above sound good.[/QUOTE]

Having an outline on paper is certainly a good idea. Some of my best code is done in psuedo code first.


As for global state variables. I disagree.
* you can potentially clutter up the global variable space.
* the state is checked every tick.

Personally a model I'm working with is this as a design scope. Which is why i disagree with the idea of global variable based states. This is just a sample and not a literal. By enable and disable group. I am enabling and disabling the groups of functions that can be bound to a state. Where as if I'm working with state checking; the design would require an added level of that state check. This way there is no state check. it's just doing.

every tick
-> PLAYER.EVERYTICK

button pressed
-> PLAYER.ATTACK
----------------------------
Ground Group
On Function - PLAYER.ATTACK
On Function - PLAYER.EVERYTICK
----------------------------
Air Group
On Function - PLAYER.ATTACK
On Function - PLAYER.EVERTICk
----------------------------







B
87
S
18
G
9
Posts: 2,455
Reputation: 14,834

Post » Mon Feb 25, 2013 10:27 pm

Exacly the kind of topic i was looking for. I asked a similar question. You nailed some very good practice and confirmed my approachs as good ones.

I also believe this topic, maybe as a tutorial, should be sticked somewhere.
I found myself coding in similar fashion as Yann :)
I see many "big names" here. Hopely some absent respected members could throw their two cents.
B
31
S
4
G
4
Posts: 110
Reputation: 4,588

PreviousNext

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 14 guests