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 / how to pass a variable to a function

Author
Message
cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 30th Dec 2012 21:06
I have a variable called "X#" I want to pass it or its value to a fuction and at the end of that fuction have it returned..

I've tryed but can not figure it out
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 30th Dec 2012 21:09
You can pass variables by listing them inbetween the function parameters. E.g



cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 30th Dec 2012 21:13
I can do this with 2 variables? I just realized I will need the x and y of a sprite...

thank you for your time...
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 30th Dec 2012 21:28
Yes, you can pass up to 9 I think. There is currently a limit but hopefully that will be removed eventually.


this.mess = abs(sin(times#))
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 30th Dec 2012 21:50
Quote: "you can pass up to 9 I think. "

I was surprised when I first learned this. DBP could support up to 255 arguments I think.

cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 30th Dec 2012 21:55
thanks guys... I've ben reading from the App Game Kit Documentation Home Page, but sometimes its confuseing...
cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 30th Dec 2012 22:58 Edited at: 31st Dec 2012 01:01
ok... I'm not shure what I'm doing wrong but the compiler keeps telling me "could not understand command at line 51"

its ths line:

if Dirx# > 0.5
waklingright()
endif


am I calling the function wrong or did I do my function wrong

hears my code;
main



function


*EDIT
code updated
cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 30th Dec 2012 23:00
sorry I just got back into programing and I'm a little rusty...
Tone Dialer
Valued Member
19
Years of Service
User Offline
Joined: 17th Oct 2005
Location: England, well a town in it !
Posted: 30th Dec 2012 23:18
@ cryptojoe

You need to pass x# and y# to the function

if Dirx# > 0.5
waklingright(x#,y#)
endif

cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 30th Dec 2012 23:48
still says; could not understand command at line 51
cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 31st Dec 2012 00:08 Edited at: 31st Dec 2012 00:36
its the "waklingright(x#,y#)" thats at line 51

dose a function have to be declared at the begining of the program like a variable?

*EDIT
it should be "walkingright(X#,Y#)" I have fixed that and still getting the same error...
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 31st Dec 2012 00:39
Two things here:

You've declared the function with two parameters, but called it with no parameters at all! The compiler is correct to flag this as an error.

Second: if this function ever gets called it will go into an infinite loop if Dirx# is greater than 0.5 when it's called. Nothing inside the "while" loop changes the value of Dirx# - so if it ever gets inside the "while" it's there for all eternity, and the computer will probably be locked up.

Whatever programming language we're using or whatever interface, it's vital to have a get-out clause in repeat and while blocks - and we must never forget that input has to be handled.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
baxslash
Valued Member
Bronze Codemaster
17
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 31st Dec 2012 00:50
Also @cryptojoe could you use the "Edit Post" link under your avatar rather than making a new post when nobody has replied yet, cheers!


this.mess = abs(sin(times#))
cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 31st Dec 2012 00:50 Edited at: 31st Dec 2012 00:56
ok "Dirx# = GetDirectionX()" was in the wrong place in the Function, fixed that...

thanks Jim

*EDIT it was an oversight on my part...
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 31st Dec 2012 01:01
No problem! Done it too many times, so I get scared when I see some loops posted on here!

Others disagree, but I think a simple state machine inside a case statement is the clearest way to handle things in the main loop. I worry about the number of Sync() statements you have there.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 31st Dec 2012 01:05 Edited at: 31st Dec 2012 01:31
I don't know how to code a state machine yet... but I'm always looking for ways to do things better...

*EDIT
ok I see the problem I'm trying to use a Function in a way it was never intended to be used... what I need are nested loops... my bad... thanks for the help guys...
The Zoq2
15
Years of Service
User Offline
Joined: 4th Nov 2009
Location: Linköping, Sweden
Posted: 31st Dec 2012 01:08
Quote: "Others disagree, but I think a simple state machine inside a case statement is the clearest way to handle things in the main loop. I worry about the number of Sync() statements you have there."


After you told me about that, I havn't gone back. It makes thing so much easier
JimHawkins
15
Years of Service
User Offline
Joined: 26th Jul 2009
Location: Hull - UK
Posted: 31st Dec 2012 12:40
@TheZoq2 - One more thing learned with bitter experience is - start worrying if blocks of code get too big. It's better to have many small functions that are easier to read and debug than pages of code.

@cryptojoe - All programs are a state machine! At the simplest, it's starting, running, ending. Here's a very small state machine for your character, written in Pascal:



Actually, it would be better have little state machines for each "character" and then set their state in the main loop.

-- Jim DO IT FASTER, EASIER AND BETTER WITH AppGameKit FOR PASCAL
cryptojoe
12
Years of Service
User Offline
Joined: 27th Sep 2012
Location: Michigan U.S.A
Posted: 31st Dec 2012 16:27
thanks Jim... I copyed your code snippet to note pad and will be studying it... Hodgey helped me get my code to do what I wanted it to do... "I was over thinking the problem"... it was as simple as this...

Login to post a reply

Server time is: 2024-11-24 06:59:19
Your offset time is: 2024-11-24 06:59:19