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 / Function, gosub and variable visibility

Author
Message
CodeTrasher
11
Years of Service
User Offline
Joined: 18th Jul 2012
Location: Tampere, Finland
Posted: 24th Jul 2012 08:46
Good morning everybody!

I've been going through some AppGameKit tutorials and the last one I did was this: http://www.appgamekit.com/bitesize-2.php. In the tutorial there's a subroutine which handles pointer pressing and mouse joints with a ball sprite.

Here's the problem: I wanted to change the gosub method 'mouseGrab' into a 'mouseGrab' function because I'm much more comfortable with the functions. After the change I couldn't move the sprite anymore. I tried to pass this sprite as a variable to the function but this didn't help.

So, can someone who knows more about differencies between DarkBASIC/AGK subroutines and functions give a helping hand here? Any help or explanations will be much appreciated.
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 24th Jul 2012 17:21 Edited at: 24th Jul 2012 17:28
Assuming you are in Tier 1, at the start of the mouseGrab routine, you have 'mouseGrab:', replace that with 'function mouseGrab()'. At the bottom of the mouseGrab routine you have 'return' replace that with 'endfunction'. Like so;



And then simply change this;



With this;




That should do it...

EDIT: Re-read your post... When using a function I believe you have to make sure all variables are global. I'm assuming this is not the case for routines, not having them declared as global could be why its not working for you as a function...
Digital Awakening
AGK Developer
21
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Sweden
Posted: 24th Jul 2012 17:32
Functions have their own local variables. If you want it to access variables outside the function they have to be declared as global. All arrays are global.

Dar13
15
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 24th Jul 2012 19:18
Quote: "Functions have their own local variables. If you want it to access variables outside the function they have to be declared as global."

Or pass the required variables into the function. Which IMHO increases code maintainability, as you know exactly what the function requires.

CodeTrasher
11
Years of Service
User Offline
Joined: 18th Jul 2012
Location: Tampere, Finland
Posted: 25th Jul 2012 07:49
Thank you everyone for your replies!

I was aware by the fact that function's variables won't be visible outside. I tried already the solution which Funnell7 brought out but it didn't work. I tried to pass that one variable as a parameter the function needs from outside, didn't work either. I changed that same variable as global (but didn't send it in as a parameter), didn't have any impact whatsoever.

This is not a big issue since it's a tutorial I was trying to do, but it bothers me. If I can't get it working it's ok. When I'm off to my own projects I intended to use functions, not routines, I hope I can find a way around this issue =) Thanks anyway for all of you.
Ancient Lady
Valued Member
20
Years of Service
User Offline
Joined: 17th Mar 2004
Location: Anchorage, Alaska, USA
Posted: 25th Jul 2012 20:09
It looks like the two variables you need to be concerned about are 'rock' and 'mousejoint'.

The rock variable is set outside the function and can be passed in as a parameter.

The mousejoint variable is set within the function and needs to be defined as a global somewhere before the first function call and should be initialised to zero to make sure it is not 'active' when you first call the function.

If you post the exact code you have in your project, we can definitely help you work it out. Please don't say it is the same as the tutoral, because it isn't, that is why you are having an issue.

Cheers,
Ancient Lady
AGK Community Tester
Funnell7
12
Years of Service
User Offline
Joined: 8th Sep 2011
Location: UK, England
Posted: 25th Jul 2012 20:42
CodeTrasher, your problem is definately with declaring the variables. They must be global. Here is the full code working as a function...



I made all the changes detailed in my first post and it works fine. I have rem'd with XXX so you can search for the changes made...
CodeTrasher
11
Years of Service
User Offline
Joined: 18th Jul 2012
Location: Tampere, Finland
Posted: 26th Jul 2012 07:38
Well, the code I have is exactly the same than Funnell7 posted, without the 'global' declarations. I was only trying to declare 'rock' as global, I didn't have a clue that I should declare 'mouseJoint' as a global, too. I certainly have to try making those changes. Thanks for your help, once again! I haven't done anything with BASIC for a while so it's going to be a "fresh" start again

Login to post a reply

Server time is: 2024-05-05 10:54:09
Your offset time is: 2024-05-05 10:54:09