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 / Optional parameters in functions

Author
Message
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 23rd Nov 2016 13:11 Edited at: 23rd Nov 2016 13:12
Hi all, is there currently a way to put in optional parameters on functions? For example, if you had a function declared something like this:

function TestFunction(a as integer, b as integer, optional c as integer)
endfunction


So that the function can legitimately be called with EITHER

TestFunction(10, 20)

OR

TestFunction(10, 20, 15)

And not have it break.
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 23rd Nov 2016 13:20
I'd go out on a limb and say no... Say you had b and c as optional, and called TestFunction(10, 20), how'd it know whether the 20 was for b or c?
Using AppGameKit V2 Tier 1
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 23rd Nov 2016 13:21
Simple order of calling, just like in visual studio
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 23rd Nov 2016 13:45
Are you saying that if you have multiple optional parameters, you must either provide all optional values, or none at all? I guess that'd work...

So in my example above, you wouldn't be able to call TestFunction(10, 20), you'd have to do either TestFunction(10), or TestFunction(10, 20, 30), no other combination would be allowed. Not sure how it works in Visual Studio...
Using AppGameKit V2 Tier 1
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 23rd Nov 2016 13:53
I'm thinking more along the lines of how AppGameKit itself does it. When filling out the parameters of a function, you have to fill it out according to one of several formats. For example, LoadImage can be called either with:

LoadImage(ImageNumber, SourceString)

or

LoadImage(SourceString)

So I guess the layout of those would be pre-determined. That's how VS does it. When declaring the function you can lay out various different ways it can be done, and depending on how the user calls the function depends on which pass-in variables are used and which are not.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 23rd Nov 2016 14:03
Not it Tier 1 unless Paul can add it but definitely in Tier 2 - that is standard operating procedure for C++
AGK V2 user - Tier 1 (mostly)
damothegreat
User Banned
Posted: 23rd Nov 2016 14:04 Edited at: 23rd Nov 2016 14:07
Functions cant do it.

But a workaround - bit messy



Or something along those lines, really messy coding

Damo
Using Tier 1 AppGameKit V2
Started coding with AMOS
Anything is possible if put mind to it
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 23rd Nov 2016 14:05
No you can't override methods like you can in C++, C# etc.
You have to create different function names.

I often stack my functions, such as the basic function being

updateEntity(entityid)

and another function

updateAllEntities()

which calls the first one many times. It's not quite the same thing, but it helps to rationalise your code.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Quidquid latine dictum sit, altum sonatur
TutCity is being rebuilt
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 23rd Nov 2016 18:01 Edited at: 23rd Nov 2016 18:01
i think the best way in tier1 is using a type for one or more functions.
AGK (Steam) V2.0.21 : Windows 10 Pro 64 Bit : AMD (16.6) Radeon R7 265 : Mac mini OS Sierra (10.12)
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 28th Nov 2016 12:47
I guess I'm just paranoid about stacking functions like that, Richard, as I actually hit DBPro's limit in terms of number of lines of code any one project can hold while making Malevolence... Not sure if AppGameKit would suffer from that same issue or not... Not that I'm ever likely to make a mobile app as big as Malevolence was in DBPro, but I guess I'm kind of a stickler for neat code now...

I guess maybe if Paul sees this it might make for a good addition to Tier 1 if he reckons it's possible?

In the meantime, Damo and Markus' system should do the trick. It's messier than I'd like but hey, if the shoe fits!
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 28th Nov 2016 15:26
the difference is just the name so this provided by agk
LoadImage(ImageNumber, SourceString)
LoadImage(SourceString)

can also be this in tier1 basic
LoadWhatever2(ImageNumber, SourceString)
LoadWhatever1(SourceString)

i think it is less messier than using a type for every function.



AGK (Steam) V2.0.21 : Windows 10 Pro 64 Bit : AMD (16.6) Radeon R7 265 : Mac mini OS Sierra (10.12)
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 29th Nov 2016 11:30
It's ok if a function only has two parameters, but if you have a fairly complex function with, say, 8 or so optionals, you'd have to re-write the function many, many times. Then, if you wanted to change something in that function, you'd have to change it in the same way in each of the different variants. It just gets messy and inefficient for more complex programs.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 29th Nov 2016 12:45 Edited at: 29th Nov 2016 12:46
Quote: "you'd have to change it in the same way in each of the different variants"

Not really. You could do something like this:


Any changes to the function only need to happen in the last one.
AGK V2 user - Tier 1 (mostly)
Carharttguy
7
Years of Service
User Offline
Joined: 8th Jul 2016
Location: Belgium
Posted: 29th Nov 2016 13:37
This is a good idea. Most languages have optional parameters, so why not AppGameKit?
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 29th Nov 2016 14:20
Scraggle, while this would work, why not cut out the middle man and just call the main DoStuff function? This is kind of how I do things now. Instead of optional parameters, I set them to -1 if they're not being used and I have code in there to recognise that. It's a sloppy workaround but it does the job for now.
Scraggle
Moderator
20
Years of Service
User Offline
Joined: 10th Jul 2003
Location: Yorkshire
Posted: 29th Nov 2016 14:50
That's how I do it as well. I was simply pointing out a way of not having to write a function many, many times and then change each one as you suggested.
I wasn't saying it was a good option.
AGK V2 user - Tier 1 (mostly)
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 29th Nov 2016 16:29
Quote: "This is a good idea. Most languages have optional parameters, so why not AppGameKit?"

i wrote a feature request. other languages are not made by one people
paul will know the effort for this.
AGK (Steam) V2.0.21 : Windows 10 Pro 64 Bit : AMD (16.6) Radeon R7 265 : Mac mini OS Sierra (10.12)
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 29th Nov 2016 23:21
@Scraggle - Fair enough!

@Markus - Good idea! I think it'll be handy for those more advanced applications!
MikeHart
AGK Bronze Backer
20
Years of Service
User Offline
Joined: 9th Jun 2003
Location:
Posted: 30th Nov 2016 08:45
While I certainly would welcome optional parameters in a way like



the advise I give to myself these days is: "Don't blame missing features, work with the ones that are there and get the job done.
Isee great games made with AGK2 in Steam and the appstores. For sure their creators concentrated on the game and not
on AGK2 itself. AGK2 is what it is. It has the most powerful and complete API I see these days. Teh language is simple but you can
get things done once you stop comparing it to your favorite tool/language."

Get creative, work on your apps/games. You will be hard pressed to find a more complete game programming language out there.
-----------------------------------------------------------
Using AGK2 Tier 1
CumQuaT
AGK Master
13
Years of Service
User Offline
Joined: 28th Apr 2010
Location: Tasmania, Australia
Posted: 30th Nov 2016 10:21
Not sure where you got the idea that I was "blaming missing features". I'm quite a competent coder and work around them just fine. I'm merely asking if there was a way to make my code neater by having optional parameters on functions...

Login to post a reply

Server time is: 2024-04-26 18:29:47
Your offset time is: 2024-04-26 18:29:47