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 / Problem with subroutine-

Author
Message
Mark Garrett
Reviewed AGK on Steam
18
Years of Service
User Offline
Joined: 10th Jul 2005
Location: California, US
Posted: 12th Jul 2017 23:10
This subroutine only makes it to the first line and then goes to 'endof' below. Why doesn't it go all the way through all the lines?



.

Attachments

Login to view attachments
Markus
Valued Member
19
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 12th Jul 2017 23:40 Edited at: 12th Jul 2017 23:41
because endif is not the line end in agk, and : is similar the semicolon like in c++
we had a discussion with paul about.

try
if x < 12
createsprite (sp, 25)
goto endof
endif
AGK (Steam) V2017.05.15 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 13th Jul 2017 23:58 Edited at: 14th Jul 2017 00:20
It's been known since 1968 (*) that 'goto' is evil, as is his little sibling 'gosub'. Just avoid using them, and embrace the joys of procedural programming.

On a related note, more recent rumblings will have it that 'if' is evil too - at least reckless - as is his sibling, the 'switch-case'. Here the solution is a bit more convoluted, and you can't get entirely rid of conditionals at any rate. You can however cut dramatically down on them by smart use of arrays and in other languages maps/dictionaries. Especially in languages where functions are first class citizens.

So to use your example, keeping both evils in mind, safe code would look something like this:



*) http://dl.acm.org/citation.cfm?id=362947
Markus
Valued Member
19
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 14th Jul 2017 15:44 Edited at: 14th Jul 2017 15:47
nah, goto is not evil. jump around is essential in ever cpu instructions.
but agree using function instead of gosub is better for overview,encapsulate and debugging.

agks compiler is inconsequent, it should expect endif because it not use the lineend as endif.
the conditionals keywords are
if then [elseif] [else] endif
and in some basic the endif was eqal the linebreak.
AGK (Steam) V2017.05.15 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 14th Jul 2017 17:04 Edited at: 14th Jul 2017 17:34
At the fundamental level, down in the machine-language execution of any given code, yes, gotos are what is done all over the place - that is not the point however. A machine can keep track of all that, we as programmers can't. So for safe code, we abstract it away using all the tricks a particular language allow. In a procedural language like AppGameKit, that means wrapping stuff up inside functions to call and use for, while and until loops.

It's like building a house. You do not care much about what the contractor need do to assemble the thing. You do care that the walls, windows, doors, plumbing and whatnot all are planned and placed correctly. When one use goto you essentially start getting involved with how many hammers should be available at the work-site.

As for if statements - and other conditionals, they are another headache and source of bugs. Especially if the same conditional check is done several places on the same variable. So to be able to abstract that out by means of smart use of datatypes and arrays is very useful. Especially in larger code-bases. So might as well plan it out that way to begin with, as it makes the code a whole lot more malleable. Need change or add a check, just update the datastructure. No need to add new conditionals all over the place.

In the code example above, if more limits are needed, add it to the array that defines the limits, up the array-limit and done. In other languages, you need not even worry about setting the array limit, as the definition is conditional upon the entries rather than a hard number pre-defined. And before somebody mentions it, yes I know you can append to an array in AppGameKit assuming it is defined empty. But then you need a loop to fill it with data rather than a straight-out definition.

Though you can work around it like so:



That way, on the higher level of abstraction, you do not care about the length of the array, but down in the nuts and bolts section, you still need define it.
PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 14th Jul 2017 20:24
GoSub I agree is evil and can lead to messy spaghetti code especially when GoSub-ing from a GoSub... yuk!!, GoTo on the other hand when used inside a function can be very handy and save duplicating common code, as for conditionals, well there is no logic without them, for's and while's will only get you so far, I agree the over use of the if statement and duplicate variable checks don't make good code at all but this is a limit of the individual programmer, knowing where and when to use a 'select' over a 'if elseif' and building a coherent 'state machine' to avoid duplicate variable checks only comes from experience and dare I say it, making mistakes.

@Markus, there is no need for a 'endif' when using 'then'

@Bubba, you conditionals need to be more explicit, you are checking numbers all < a target when you should be checking number ranges



Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 14th Jul 2017 22:40
Using cases over elseif make no functional difference - the one just looks more tidy than the other. Depending on language there may also be some difference upon fallthrough or break as condition is met. Which can be confusing.

Anyhow, and the point made by considering if and related conditionals to be evil, is to minimize the use - not eradicate it. It is quite simple really, the more flexible code you got, the less work goes into changing and adding to it. If you got conditionals that are general and not tied to hard values, so much the better.

I know, this topic is not for the rank beginner - it is more for the aspiring to be competent. But as I see it, one may as well get into good habits right from the bat than learn bad habits which need be unlearned later on.

Then again, my first lines of code ever written was probably something like:

10 PRINT "Hello! ";
20 GOTO 10

It is a steep mountain to climb, but very worthwhile
Markus
Valued Member
19
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 15th Jul 2017 09:03 Edited at: 19th Jul 2017 10:47
Quote: "@Markus, there is no need for a 'endif' when using 'then'"

my point was that we can not use a whole line with 80 to 200 chars in agk and need to break up into rows.

the agk compiler not understand this syntax.
if a=b then x=5555555:y=1000000:z=8888888: if b=c then a=5 elseif c=a then x=0:y=0:z=0
if a=b then x=5555555:y=1000000:z=8888888: endif : if b=c then a=5 elseif c=a then x=0:y=0:z=0
AGK (Steam) V2017.05.15 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 19th Jul 2017 09:09
If you need to use gosub or goto in your code, then your code is structured wrong.
Signature removed by mod because it's larger than 600x120... please resize and try again.
Markus
Valued Member
19
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 19th Jul 2017 10:52
Quote: "If you need to use gosub or goto in your code, then your code is structured wrong."


in this case without goto can be like this. but as soon you can see duplicated code parts its often possible to simplify.
if x < 928
createsprite (sp, 2)
elseif x < 1250
createsprite (sp, 1)
endif
endof:
AGK (Steam) V2017.05.15 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
TaskMaster
6
Years of Service
User Offline
Joined: 25th Jul 2017
Location:
Posted: 26th Jul 2017 19:37
Hmmm...

Since AppGameKit does not have classes, so you cannot have multiple functions that can access common variable, there is a use for gosub .

In the main loop of the program I am writing, if I want to break my logic into functions, then I need to pass a bunch of variables, which is a mess; or use globals; which is also a mess; the only other solution is to use gosub as far as I can tell.


PartTimeCoder
AGK Tool Maker
9
Years of Service
User Offline
Joined: 9th Mar 2015
Location: London UK
Posted: 26th Jul 2017 20:07
Quote: "my point was that we can not use a whole line with 80 to 200 chars in agk and need to break up into rows.

the agk compiler not understand this syntax."


oh I see, its an ugly way to write code anyway, not only agk compiler that can not understand it lol, code without proper line breaks and indentation gives me a headache!
Markus
Valued Member
19
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 26th Jul 2017 20:44 Edited at: 26th Jul 2017 20:44
Quote: " then I need to pass a bunch of variables"

if you have pass much variables you can use a type that also supports by reference.
other technic is having a struct in an array and accessing the array by index so you need only one variable.
i not need new and null but classes would be nice to have a separate scope and also because better syntax in code and shorter command names.
AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Dybing
12
Years of Service
User Offline
Joined: 12th Sep 2011
Location: Bergen, Norway
Posted: 27th Jul 2017 03:59
Meh, we do not need classes. We could need some way to break up into namespaces though. Be it the C# way with namespace spanning or the functionally similar Go way with package spanning.

As for passing a bunch of variables into functions, is just bad design. Wrap them up into types, and just pass those around. No need to complicate matters by wanting to introduce OOP into the mix. If you don't know how to compose good data-types, you don't know how to compose good classes either. I'll grant there is a certain art to it - but experience helps identifying what belongs where.
Mobiius
Valued Member
21
Years of Service
User Offline
Joined: 27th Feb 2003
Location: The Cold North
Posted: 28th Jul 2017 09:03
Quote: "Since AppGameKit does not have classes, so you cannot have multiple functions that can access common variable, there is a use for gosub ."

I personally believe that that attitude is just lazy coding and a bad habit to get into. There is never a use for gosub in modern languages, it's just a crutch for poor developers to lean on because they can't/don't want to do it properly.

Quote: "[...] I need to pass a bunch of variables, [...]; or use globals; [...]"

Isn't that how modules work? You know, the concept of abstraction and data hiding? You only pass that data you need into a function. You can pass types into a function quite easily, it's a close to passing objects as AppGameKit can get.
Signature removed by mod because it's larger than 600x120... please resize and try again.
Markus
Valued Member
19
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 28th Jul 2017 13:45
Quote: "oh I see, its an ugly way to write code anyway, not only agk compiler that can not understand it lol, code without proper line breaks and indentation gives me a headache!"

i don't like wasting 9/10 of my monitor width because then you need more scrolling up and down. i agree that source code should look understandable.

Quote: "namespaces "

a issue with namespaces is when you read a part of code and this namespace word is missing you not know which modul the command belongs.
that often happens where people share code for help in community and they NOT copying the part at top with using.
especially at c# where it normal looks like a.b.c.d.DoSomething and you just see simplified d.DoSomething

AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)

Login to post a reply

Server time is: 2024-03-28 11:38:28
Your offset time is: 2024-03-28 11:38:28