Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Avoiding some repetitions

Author
Message
luskos
16
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 2nd Oct 2015 05:15
Hi, guys long time no see!
I have this problem with certain code i have where there is a lot of repetitions and i can not think of a way to put it in a function.
Some legend first:
plain is used to check which plain is in collision with the mouse
rotation is used to check if rotation is in progress
msxdif and msydif are mousex and mousey differences checked agains each other, if msxdif is bigger it's horizontal rotation and vise versa
group_object is selected object
msy and msx defines direction of rotation
rotations are processed by frames every frame rotates the group of objects with 18 degrees, fra is positive 5, frb is -5 frame if the game sees that frame is equal to fra or frb it stops rotation
last_rotation$ points what rotation is occuring for other part of the code
global rotation function rotates the objects in group

I need some elegant solution to the problem as i can't think of one.I can tell for myself that i'm not much refined programmer.And this is some really old code i wish to change but all the game logic revolves around it.
So let's present the problematic piece of code:

Coding is My Kung Fu!
And My Kung Fu is better than Yours!
Green Gandalf
VIP Member
19
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 2nd Oct 2015 12:58
Quote: "I have this problem with certain code i have where there is a lot of repetitions and i can not think of a way to put it in a function."


I would clean it up first, then consider how to simplify it, then put it in a function if necessary.

One thing I would do straightaway is remove any unnecessary if tests. For example, why do you compare the values of msxdif and msydif twice here?


Once you've done that you can consider simplifying the code.

My understanding of your code is that the bulk of the coding logic is trying to decide which group the current value of group_object relates to. Once that is done the rest of the code takes appropriate action. One approach might be to first identify the colour of the group, e.g. define a new variable whose values represent the colour of the group. For example, you might use 1, 2 or 3 to represent orange, red or blue - and other values to represent other colours if there are any (or zero for none if relevant). You can then use select/case statements to code the appropriate actions. So, in pseudocode your logic might be something like:



The use of select/case statements makes your code more readable later and you can add extra cases easily.

Hope this helps.


Powered by Free Banners
luskos
16
Years of Service
User Offline
Joined: 28th Jun 2007
Location:
Posted: 5th Oct 2015 16:32
Well at least the code will look more organized, but the bulk of the code will still be there.Probably this is due to bad design.Maybe there is no other way.The problem with groups is that every object is present in at least 2 groups at the same time, in some cases in 3.Adding one more layer to find the group color while i really need to know with which of the groups the object is in the game need to work with.This does not make sense, or at least to me, and probably you suggested that only because you didn't know about this.Or i'm plain stupid. .But hey, thanks for the suggestion, probably will be better to organize it that way.I'm still think on this issue, and I'll keep you posted in case i find a better structure for this.
Coding is My Kung Fu!
And My Kung Fu is better than Yours!

Login to post a reply

Server time is: 2024-03-28 10:36:15
Your offset time is: 2024-03-28 10:36:15