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 / Faster way to compute long If ElseIf list...

Author
Message
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 6th Nov 2019 11:51
Hi All,

I've got some code which doesn't feel right below - can anyone suggest a better route?



The list in my code is actually much longer (up to 43 so far). You can see the repetition - so wondering if you can reference functions programatically rather than hard coding the "SpriteAction??(Cycle)" lines
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 6th Nov 2019 12:00
fubarpk
fubarpk on Itch...………...https://fubarpk.itch.io/
fubarpk on googleplay..https://play.google.com/store/apps/developer?id=fubarpk
smallg
Valued Member
18
Years of Service
User Offline
Joined: 8th Dec 2005
Location: steam
Posted: 6th Nov 2019 20:20
don't know what code is in the actual functions but do they need to be named differently? can you not pass in the desired action?
i.e.
for a = 1 to 43
If Sprite[Cycle].Action = a
SpriteAction(Cycle,a)
endif
next a

depending how different the functions are you will still need a way to check the required function variable inside though unless it can also use the value you pass in
life's one big game
spec= i5 4ghz, 16gb ram, Nvidia 1070ti gpu
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 7th Nov 2019 09:02
smallg - They're all totally different - it controls the individual behaviours of the monsters and platforms in my game.

fubarpk - Had no idea you could use Select like that - wonder which is fastest - I'll do a quick benchmark later. So... how does it know that the case relates to "Cycle" - is it that "Cycle" is effectively the only thing that can change?
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 7th Nov 2019 17:22
Hi fubarpk,

Surely that won't work? How does it select the correct function to run, eg SpriteAction04(Cycle) as opposed to SpriteAction03(Cycle) etc?

Sorry, realised I've not been clear - the "Cycle" is from a For.. Next.. loop that the code I've pasted sits inside of. So it's more like



I guess I was wondering if you can access/process a function by calling its name programmatically (ie you create the name of the function to call using code).... or failing that... if there's just a more elegant/faster way than doing it as shown in my example...
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
hendron
8
Years of Service
User Offline
Joined: 20th Dec 2015
Location:
Posted: 8th Nov 2019 01:15 Edited at: 8th Nov 2019 01:37
Quote: "I guess I was wondering if you can access/process a function by calling its name programmatically (ie you create the name of the function to call using code).... or failing that... if there's just a more elegant/faster way than doing it as shown in my example..."


No way to do that in Tier1 (would be cool to have, though). You could perhaps approach it by taking all of your SpriteAction functions and putting them into one function that contains a select statement, such as:



Either way I think you'll have to use a large select statement somewhere.
Qugurun
Valued Member
9
Years of Service
User Offline
Joined: 8th Dec 2014
Playing: AppGameKit
Posted: 8th Nov 2019 02:53 Edited at: 8th Nov 2019 02:55
https://www.instagram.com/qugurun/
Qugurun
Valued Member
9
Years of Service
User Offline
Joined: 8th Dec 2014
Playing: AppGameKit
Posted: 8th Nov 2019 03:55 Edited at: 8th Nov 2019 08:13
https://www.instagram.com/qugurun/
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 8th Nov 2019 17:08
Is this posted in the firght forum - I'm not seeing the connection to my issue?!
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 9th Nov 2019 18:22
Is there no way you can combine those all into the same function and use variables to control how it reacts? Then it'd just be a simple matter of passing the right variables from an array lookup into the function.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
blink0k
Moderator
11
Years of Service
User Offline
Joined: 22nd Feb 2013
Location: the land of oz
Posted: 9th Nov 2019 23:48
Quote: "Is this posted in the right forum - I'm not seeing the connection to my issue?!"

Phaelax and Qugurun are suggesting: Maybe you could externalize the data that drives the different actions.
Then you could simply have a table of data describing the different actions and feed that data into the one function.
DavidAGK
AGK Developer
10
Years of Service
User Offline
Joined: 1st Jan 2014
Location:
Posted: 14th Nov 2019 12:53
Ah, right yes... It's tricky because the actions (functions) are pretty big - the define monster behaviour so accept about 30+ variables. Reckon I'll have to break it down another way or put up with a crazy long if else statement!

Thanks for all help.
Using Tier 1 AppGameKit V2
Started coding with AMOS (Thanks Francois Lionet)
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 14th Nov 2019 13:22 Edited at: 14th Nov 2019 13:42
Hi David

Id suggest changing it to select case and moving each case onto a single line

select Sprite[Cycle].Action

case 1: SpriteAction01(Cycle): Endcase
case 2: SpriteAction02(Cycle): Endcase
case 3: SpriteAction03(Cycle): Endcase
case 4: SpriteAction04(Cycle): Endcase
case 5: SpriteAction05(Cycle): Endcase
case 6: SpriteAction06(Cycle): Endcase

// add more lines or functions....
endselect

Just that its easy to read the code and to add lines one by one for each function you add. It does the exact same thing as the if else original code though. We dont have function pointers in AppGameKit unfortunately.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th Nov 2019 15:35
Case is more efficient, because I believe having that many ELSE IFs so many layers deep would build up on the stack too much. On the other hand, I believe the SELECT runs through every CASE check unless you put in a break.
Tiled TMX Importer V.2
XML Parser V.2
Base64 Encoder/Decoder
Purple Token - Free online hi-score database
Legend of Zelda

"I like offending people, because I think people who get offended should be offended." - Linus Torvalds
Bengismo
7
Years of Service
User Offline
Joined: 20th Nov 2017
Location: Yorkshire, England
Posted: 17th Nov 2019 16:59
The If/Else's dont add to the stack ...its just a logical test, not dropping into a function or creating any stack variables. It just does the comparison and moves on.

The Case statement checks each case until it finds the one that matches then performs the code between the case and end case (there is no break in AppGameKit basic language). This is pretty much the same as what the if/elseif code does.

Having timed a few million iterations of both versions...the code speed is pretty much identical for either version of the code. Id use CASE mainly as its simply easier to read and maintain but either works

Login to post a reply

Server time is: 2024-11-22 10:46:17
Your offset time is: 2024-11-22 10:46:17