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 / [SOLVED] [Tier 2] switch statement won't work within the Loop() function

Author
Message
Jerry McGuire
7
Years of Service
User Offline
Joined: 25th Mar 2017
Location:
Posted: 28th Jul 2017 17:29 Edited at: 28th Jul 2017 18:37
Hi,
I've been trying to use a switch statement within the Loop() function, like



when running it, it will start with "case 0" fine. But weird things happen, for example if I called agk::createsprite in a case that runs only once, it will still run more than one time. What is going on? Am I missing something?
iMac Book Pro, MacOS 10.12.4, Xcode 8.3.3;
iPhone 6, iOS 9.35; iPhone 5s, iOS 9.35; iPad (3rd gen), iOS 9.35;

Dell Precision T7400, Windows 7 Professional 64bit, Visual Studio Community 2015;

The author of this post has marked a post as an answer.

Go to answer

Fluorescent
FPSC Reloaded TGC Backer
18
Years of Service
User Offline
Joined: 1st Aug 2005
Location: Stockholm, Sweden
Posted: 28th Jul 2017 18:48
If you change your switch to a couple of if's will it still be weird?
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 28th Jul 2017 18:54
This post has been marked by the post author as the answer.
try print out values at screen Print(Str(level))
and print something at beginning and before end function
AGK (Steam) V2017.07.19 : Windows 10 Pro 64 Bit : AMD (17.4.4) Radeon R7 265 : Mac mini OS Sierra (10.12.2)
Jerry McGuire
7
Years of Service
User Offline
Joined: 25th Mar 2017
Location:
Posted: 29th Jul 2017 01:41
@Fluorescent: yes it would become very confusing and awful. I have far more than three cases. But you are completely right, the problem does not appear if I use if-statements.

@Markus: I guess I can do something along those lines, and then come back with a more specific example/behavior; will do

Thank you both!
iMac Book Pro, MacOS 10.12.4, Xcode 8.3.3;
iPhone 6, iOS 9.35; iPhone 5s, iOS 9.35; iPad (3rd gen), iOS 9.35;

Dell Precision T7400, Windows 7 Professional 64bit, Visual Studio Community 2015;
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 29th Jul 2017 09:45
Sounds a bit weird (subroutines are a bit of a nono)

Instead of a subroutine you could try a function
and use the exitfunction

GOODLUCK
fubar
Jerry McGuire
7
Years of Service
User Offline
Joined: 25th Mar 2017
Location:
Posted: 1st Aug 2017 17:27 Edited at: 1st Aug 2017 17:29
@fubarpk: thanks for the advice, I am not very familiar with programming best practices. So it's good to know that putting a switch within the loop statement is not the best way to go. The switch statement will have to go within the function, right?
I will try doing so and check if the problem vanishes.
iMac Book Pro, MacOS 10.12.4, Xcode 8.3.3;
iPhone 6, iOS 9.35; iPhone 5s, iOS 9.35; iPad (3rd gen), iOS 9.35;

Dell Precision T7400, Windows 7 Professional 64bit, Visual Studio Community 2015;
fubarpk
Retired Moderator
19
Years of Service
User Offline
Joined: 11th Jan 2005
Playing: AGK is my friend
Posted: 5th Aug 2017 19:36
Its more a problem with subroutines, I was brought up believe goto's are a nonono and gosub routines are just as bad
so its best to use a function when you can even if the function is void and has nothing passed to it

Oh I'm sorry your writing for tier2 I had a glimpse and thought it was tier1 subroutine oh dear I mustve needed sleep

in tier1 it looks like this but theory still same


in tier2 I noticed no default


The problem might not be the code here it may be how it is returning from the called routines by not exiting properly or always the same
for example exiting a non void function with a void parameter but compiler interprets it as 0 or something hard to tell without out seeing more code
when I look aqk your code again it really looks ok I just was lacking sleep and thought you was in tier1 using subroutines but add a default to the case that also may help
fubar
Jerry McGuire
7
Years of Service
User Offline
Joined: 25th Mar 2017
Location:
Posted: 15th Aug 2017 15:20
@fubarpk: thanks! Sorry for the late reply . Been busy with my day job . I'll try this option that you are proposing as soon as I get a chance. And I will let you know if it solves the issue.
iMac Book Pro, MacOS 10.12.4, Xcode 8.3.3;
iPhone 6, iOS 9.35; iPhone 5s, iOS 9.35; iPad (3rd gen), iOS 9.35;

Dell Precision T7400, Windows 7 Professional 64bit, Visual Studio Community 2015;
Jerry McGuire
7
Years of Service
User Offline
Joined: 25th Mar 2017
Location:
Posted: 31st Aug 2017 17:28
OK, I'm just writing this post because I feel like it's my duty to wrap it up. I was making a mistake and thanks to Markus's suggestion I found it out.
Lesson: use the print command to find out where your program get's stuck. It's a very good way to debug.
Thanks Fluorescent, Markus, and Fubarpk!
iMac Book Pro, MacOS 10.12.4, Xcode 8.3.3;
iPhone 6, iOS 9.35; iPhone 5s, iOS 9.35; iPad (3rd gen), iOS 9.35;

Dell Precision T7400, Windows 7 Professional 64bit, Visual Studio Community 2015;
Markus
Valued Member
20
Years of Service
User Offline
Joined: 10th Apr 2004
Location: Germany
Posted: 31st Aug 2017 19:24
ok, i am happy that you find the cause / reason.
AGK (Steam) V2017.08.16 : Windows 10 Pro 64 Bit : AMD (17.7.2) Radeon R7 265 : Mac mini OS Sierra (10.12.2)

Login to post a reply

Server time is: 2024-04-26 12:55:11
Your offset time is: 2024-04-26 12:55:11