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.

AppGameKit Classic Chat / Which way do you prefer, and why?

Author
Message
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 14th Jun 2015 23:14
Hi Guys,

I find myself spending far too much time wondering which one of these two methods is the best, and why?

What I am doing is using a pair of Booleans to decide which one of four actions to perform.

Method 1:


This method is neater and easier to follow, but seems kind of long winded.

Method 2:


This method is less easy to follow, but I think is a lot more efficient in that it doesn't cause the computer to evaluate so many conditions.

So I thought I'd throw it out here and see what you guys had to say? or if you had any better way of doing it which I hadn't thought of...

Zwarteziel
13
Years of Service
User Offline
Joined: 22nd Jan 2011
Location: Netherlands
Posted: 15th Jun 2015 09:03 Edited at: 15th Jun 2015 09:13
I always try to use the method that involves the least computation. Regarding the selection of conditions, you might want to look into the select-endselect and case-endcase statements AppGameKit provides: with them, you can create code that is both easy to read and efficient to execute.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Jun 2015 09:34 Edited at: 15th Jun 2015 09:38
Elaborating on Zerotown's suggestion, if you use integers for your bools anyway why not assign different values to each. 0 is still false but any other number can be true so:

Effectively you reduce 4 conditions to just 1. From your 2 examples Method 2 does less conditions than Method 1 but a single case statement is much quicker.

Using AppGameKit V2 Tier 1
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 15th Jun 2015 10:08 Edited at: 15th Jun 2015 10:08
Another variant:



----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 15th Jun 2015 11:55
I would go with Baxslash's solution

Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 15th Jun 2015 12:45
I think Baxslash's code can be optimized:



Can be changed to:



----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 15th Jun 2015 14:18
Clever! I like it Impetus73!!

Using AppGameKit V2 Tier 1
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 15th Jun 2015 23:29 Edited at: 16th Jun 2015 01:03
@Zerotown:
I'm well aware of the select/case commands, but I hadn't thought of a way of using 2 variables with one.

@Baxslash:
AGK doesn't have a proper Boolean data type does it? I'm sure it never used to, but AGK2 has a way of sneaking surprise new things on me. I was blown away by he new array system which I didn't discover until about a fortnight ago.

>Edit<
Completely forgot to say thanks for the snippet. I've used it with Impetus73's alteration.

@Impetus73:
Yeah, that's how I probably would have done it if I was originally using a select/case block.

@BatVink:
I have

@Impetus73(again):
That's really cool. I have used that.

Thanks guys, as always.

It's always nice to see how other ppl code things

DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 15th Jun 2015 23:30
I did a test a while back on nesting multiple conditions versus using and's in larger but less nested conditions and was surprised by how little difference performance wise it made. I was expecting the deeper nested conditions to win hands down as computation is stopped early in some cases.

Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 15th Jun 2015 23:39 Edited at: 15th Jun 2015 23:40
Nice Impetus, very nice!

I love this kind of question and the answers that they produce

AGK V2 user - Tier 1 (mostly)
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 16th Jun 2015 10:43
Another nifty one for toggling a switch between -1 and 1. Many people use 0 and 1, but it can be any 2 numbers and 1 / -1 works...

I always used:



This can be replaced by 1 line of code...



Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Jun 2015 12:27
I tend to toggle between 0 and 1:
a = 1 - a

Using AppGameKit V2 Tier 1
Scraggle
Moderator
21
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 16th Jun 2015 12:36 Edited at: 16th Jun 2015 12:36
I often use the same booleans as BatVink (1 and -1), so here is a random generator for those:


AGK V2 user - Tier 1 (mostly)
Impetus73
13
Years of Service
User Offline
Joined: 28th Aug 2011
Location: Volda, Norway
Posted: 16th Jun 2015 13:23
To change any value to the opposite:



----------------
AGK programmer
Did Amiga / AMOS programming in the 90's.
Sph!nx
15
Years of Service
User Offline
Joined: 3rd Dec 2008
Location: The Netherlands
Posted: 16th Jun 2015 16:26
Great topic. Got nothing to add than that I've learned from it!

Regards Sph!nx
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 16th Jun 2015 17:55 Edited at: 16th Jun 2015 17:56
Quote: "I often use the same booleans as BatVink (1 and -1), so here is a random generator for those:
"

RandomSign(1)

Quote: "To change any value to the opposite:
"

value = -value

Using AppGameKit V2 Tier 1
Yodaman Jer
User Banned
Posted: 17th Jun 2015 00:13
If AppGameKit had proper booleans (actually using the words "true" and "false"), one could flip any boolean like so:

If GetPointerPressed(1) = 1 then booleanValue = !booleanValue

I used to do that in Unity all the time when I needed something that could be toggled. A nice little shortcut! Impetus' technique of

value = value * -1
is the workaround for AppGameKit that I have used.


Think YodamanJer, Think Clonkex, GAIN POINTS!
=PRoF=
21
Years of Service
User Offline
Joined: 17th Mar 2003
Location: Milton Keynes, UK
Posted: 17th Jun 2015 00:46
@Yodaman Jer:
As part of the system module for my Game Template, I have these handy constants defined...



I find they certainly help avoiding using arbitrary numbers. I use the Fail value mostly as a return from functions which have failed for whatever reason.

It's really annoying when I start to write a small snippet for whatever reason, I always forget that True/False aren't proper AppGameKit commands.

Jambo B
15
Years of Service
User Offline
Joined: 17th Sep 2009
Location: The Pit
Posted: 22nd Jun 2015 23:12
Quoting Baxslash:

Quote: "Effectively you reduce 4 conditions to just 1. From your 2 examples Method 2 does less conditions than Method 1 but a single case statement is much quicker."


Kind of on a related note - is it still true that with case statements you should put the most-used statement at the top, to speed up processing? Theory is that the other case statements still need to be evaluated, so the least-used case would be at the end.

Or does AppGameKit do something nifty of which I'm not aware, to avoid checking every case?

- James
Yodaman Jer
User Banned
Posted: 23rd Jun 2015 02:23 Edited at: 23rd Jun 2015 02:24
Quote: "value = -value"


...How did I never realize that would do exactly the same thing as value = value *-1?

Hey it is true, you do learn something new each and every day!


Official Forum President from June 20th, 2015 - June 20th, 2016 (when my successor is elected, whomever that may be!).
SoftMotion3D
AGK Developer
19
Years of Service
User Offline
Joined: 24th Aug 2005
Location: Calgary,Alberta
Posted: 24th Jun 2015 04:21 Edited at: 24th Jun 2015 04:25
Quote: "xspeed = xspeed *-1"

Quote: "value = -value"

omg....lol

i made a function that took the value and subtracted it twice to produce the negative value and took the abs(#) of it if it was negative to start with....lol

thanks for the quicker code! (no function required anymore!)

www.sheldonscreations.com

Login to post a reply

Server time is: 2024-11-25 21:50:27
Your offset time is: 2024-11-25 21:50:27