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.

DarkBASIC Discussion / Functions

Author
Message
1337 programm3r
18
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 11th Mar 2008 03:27
Am I just doing it wrong or can you not use data statements, that were declared outside of a function, inside of a function? What about variables?

Super Cool
LBFN
19
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 11th Mar 2008 03:45
Data statements for functions need to be included in the function itself, otherwise DBC will not 'see' it and the code will error. If you don't want to include the data there, why not just change it into a procedure? You could then tuck all of your data statements at the end of the program.

Variables in a function in DBC are local and you cannot have globals in DBC. One thing you could do is use an array. Arrays will work inside a function fine.

LB
1337 programm3r
18
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 11th Mar 2008 06:04
hmmm, maybe I will use arrays. What if you include the data statments inside a function, do all other functions recognise the data?

Super Cool
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 11th Mar 2008 11:00
Data statements do not work in a function or in a #Include file.

They should really only be at the end of your main program code.

TDK_Man

LBFN
19
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 11th Mar 2008 11:50 Edited at: 11th Mar 2008 11:56
Try this code. After you run it once, rem out the first part and remove the remstart/remend from the second part.



LB
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 11th Mar 2008 12:03
I only noticed yesterday that data statements didn't work at the end of a #Include file - the colour picker component in my GUI function collection was missing a row of primary colours. (I was using data statements to read the RGB colour values in a loop).

I know #Include files can only contain functions, but I thought it would be OK to have the statement list external to the functions. Apparently not.

As LBFN's code snippet shows, putting data statements inside a function isn't good practice. (Well I wouldn't do it anyway - even if DB is happy with it)!

TDK_Man

1337 programm3r
18
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 11th Mar 2008 18:35
Thanks guys, I just feel stupid because I wrote out all this data for these enemies and now I cant use them, I actually might put the data statements inside the function just this once

Super Cool
TDK
Retired Moderator
23
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 11th Mar 2008 22:46
Quote: "I wrote out all this data for these enemies and now I cant use them"


Why not?

Just put them at the end of the main DBA file and read them into an array in your main program. This array can then be accessed inside any of the functions.

Just shout if you are not sure what to do...

TDK_Man

Libervurto
20
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 12th Mar 2008 00:25
How about using RESTORE inside the function?
or doesn't that work either?

reading data doesn't seem very function-ey to me, I just have a hunch that it won't work. My view is that functions are for things that can change, but data is set in stone.
I'm probably just saying the same as TDK and LBFN but meh...

LBFN
19
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 12th Mar 2008 02:46
Quote: "How about using RESTORE inside the function?
or doesn't that work either?
"


Actually, the example I gave has a restore command within each function. I put some additional data in front of it to see what DBC would do and it did restore the data pointer correctly. So yes, it will work within a function.

Quote: "reading data doesn't seem very function-ey to me,"

I agree, but that is what 1337 programm3r asked for. One advantage would be that all of your data statements that are related to that specific function are right there for easy perusal/editing. This might come in handy when you set a game on a shelf for a while and pick it back up and want to edit/add data.

Still, I don't see me doing this a whole lot.

LBFN
1337 programm3r
18
Years of Service
User Offline
Joined: 19th Nov 2007
Location: Your MOM
Posted: 12th Mar 2008 07:20 Edited at: 12th Mar 2008 07:21
Quote: "Just put them at the end of the main DBA file and read them into an array in your main program. This array can then be accessed inside any of the functions.
"


I know what you are talking about , I guess I could do that but it would be easier just to put the things into arrays in the first place, Im just making extra work for myself pretty much.

Super Cool

Login to post a reply

Server time is: 2026-07-05 13:38:44
Your offset time is: 2026-07-05 13:38:44