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 Professional Discussion / Games and stuff... help!

Author
Message
Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 10th Apr 2011 02:28
Hey folks!

So... I'm making some games in DBP and the code tends to get rather long. Is there a way for me to write DBP code on, lets say a .txt, and then inn the main code just make it "read" it?

thnx

- Tim
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 10th Apr 2011 06:35 Edited at: 10th Apr 2011 06:35
If you're using DBP you should be able to include multiple files.


Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 10th Apr 2011 15:06
Ok, cool!
Now, can some one pliz give me a small example on how to make the main file read the other files?

Thnx

- Tim
Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 10th Apr 2011 15:51
Cool

Can someone pliz give me a small example on how to run code on a separate file, and then make the main file call info from the separate file? for example: I have a text game whit a fight system (pokemon style) and I want to run the fight code on a separate file. Can I just run the fighting on that code, and then feed the damage back to the main file?

I don't need alot of code, I just need to understand how the consept works

thnx alot!

- Tim
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 10th Apr 2011 18:21
The example above shows you how it's done...using include files.

If you want to do it at runtime, then that's a different question altogether. You would have to learn lua scripting or similar, and employ one of the lua plugins to make it work.

Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 10th Apr 2011 23:57
Hmmm... i know how to make the other files, i just need to know the comands to use to get data from the included file, if they don't comunicate at all time.

thnx

- Tim
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 11th Apr 2011 02:33
You talking about file input/output? You may want to look at these commands.


Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 11th Apr 2011 15:34
Ok, so... Right now I got a text game whit a fighting system, and that is all one long file. I want to splitt it up to several (darkbasic) files so that it is easyer to manage. I need help whit how to do that.

Thnx

- Tim
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 11th Apr 2011 19:08
I don't know what to say... I thought a visual example would've helped

Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 11th Apr 2011 19:55
Sorry, I might just be stupid (prbably :p)

I know how to make several files in a project. What I need to know is, how do I tell the mail file to run the code in the other files and retreve data from it?

thnx

- Tim
Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 11th Apr 2011 19:55
Love the fruit wars game btw

- Tim
Wyldhunt
16
Years of Service
User Offline
Joined: 27th Sep 2009
Location: The Dark Side
Posted: 11th Apr 2011 20:18
The included files should contain functions.
You can execute these functions from your primary game loop in your first script.

It would look something like this:

Main Game Script:


Your included script would have:


Once you have a block of code in a function, you can call that function anywhere you need it. So, you can have a function named DoCombatStuff() that holds all of your combat AI code. When you need to determine combat stuff in your main combat loop, just call DoCombatStuff() and it'll find that function in your include files and run that code.

There's a bit more to it than that, but that may be enough to help you get started.
Here's a short tutorial from an old TGC Newsletter which may help you out a bit also:
http://www.thegamecreators.com/pages/newsletters/newsletter_issue_27.html#9

Alternatively, see if you can find examples or help in the DBP help files on declaring functions.
Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 11th Apr 2011 21:06
Thnx that's what I needed to know.

- Tim
Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 12th Apr 2011 22:46
It's important to note, however, that using include files is no different, functionally, to putting those functions in your main source file and calling them from there. The only practical difference is that you can use the same include files in multiple projects - kind of like a plugin. Even that, however, is functionally identical to copypasting the relevant functions from one project to another, and keeping everything in main source files instead of includes. When the compiler starts its run on the code to create an executable, it effectively appends each include file onto the end of your main source file to create one big, long concatenated source from which to compile, so all of the includes get compiled along with the primary source. This means that the separation between the main source and each include is therefore a logical break, not a physical one. It also means that once you compile your project, making changes to the include file will not have any effect on your program unless you compile it again.

Perhaps I'm misunderstanding, but I don't think this suits your purposes. Weren't you asking in your original post about scripting and dynamic code? Or am I way off base and you just want to know how to split your code up into logical pieces (cause that's what you use includes for)?

I thought you wanted to put actual DBPro code into a text file, and have it executed by the main source file dynamically. When you compile your program, don't you want to retain the ability to adjust those text files and have the same executable handle the updated content of those text files?

If that is the case, it's done by coding an interpreter into your main source file. You can also use LUA scripting quite easily if you use the appropriate LUA plugin (and, of course, if you know how to script in the LUA format). The alternative, one that I've taken to, is creating your own scripting language and interpreting it yourself in your main project. I use a custom-built scripting language technique for all my dynamic programming needs, and I tweak it on a project-by-project basis so that it is specifically tailored for its unique purpose. I can show you how to do this if I am interpreting your question correctly.

EDIT: Spewing. I typed all of that and then noticed this post from Tim that I'd completely overlooked:

Quote: "Ok, so... Right now I got a text game whit a fighting system, and that is all one long file. I want to splitt it up to several (darkbasic) files so that it is easyer to manage. I need help whit how to do that."


I'm posting this anyway, cause I typed it and I can't bring myself to delete it
Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 13th Apr 2011 10:47
Hehe well, I think I got it working now. Only problem is that I need to re-write some of my code. It works when i one single long file, but when I use includes it stop working -.-' well... I guess that is some of the fun whit coding :p

- Tim
Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 13th Apr 2011 18:15 Edited at: 13th Apr 2011 18:16
It shouldn't make any difference whether your functions are in includes or if they are in your main source. Put the include files themselves in the same folder with your main source, and make sure they are in the includes list in your IDE. You can't access the functions in include files unless the IDE has been told to include those files in the compile. Take a look at the screenshot that Ashingda posted (the second post in this thread, the very first response to your question) to see what I mean.

Wyldhunt
16
Years of Service
User Offline
Joined: 27th Sep 2009
Location: The Dark Side
Posted: 13th Apr 2011 21:27
@Agent
Keep in mind that Tim9009 is just learning this stuff. Basics that more advanced programmers take for granted may not be obvious.
In fact, there is a very important difference between running code in your main loop and copy/pasting part of that code in to a function.

I'm not sure what your error is, exactly. It'd be easier to help you if you told us exactly what the error said.

I'm guessing that it has to do with your variables.

If you copy/pasted your code in to a function, you may not have access to your variables any more.
You'll need to do some research on this, but you need to declare your variables as global if they are used in an included function. Variables declared inside of a function only work inside of that function. You can feed variables in to a function by adding them to the function declaration like this:

function myfunction(Variable1$, Variable2, Variable3#)
.
.
.
endfunction

When you call that function in your script, it'll let you feed 3 variables in to the function. In this case, a string, an integer, and a bool.
Tim9009
15
Years of Service
User Offline
Joined: 9th Apr 2011
Location:
Posted: 14th Apr 2011 01:18
Well, I have been coding in db for a good 6 months now, and know a good portion of the language, it just this include part that is new to me.
I first started out whit functions in the include files, but that did not work as they did not update my variables, so I switched to sub routines. That seems to work alright. I had a load/save system for my game that did work, but once I moved the sub routine from the main source file to the include it "stoped working". The program still runs, it just does not save the variables properly. (I save the variables to a .sav file) and, yes I have checked that the include files run.

- Tim
Agent
21
Years of Service
User Offline
Joined: 7th Sep 2004
Location: Sydney, Australia
Posted: 14th Apr 2011 12:03 Edited at: 14th Apr 2011 12:05
Quote: "In fact, there is a very important difference between running code in your main loop and copy/pasting part of that code in to a function."

You can only be referring to global/local variable declaration. However, earlier you said:
Quote: "The included files should contain functions."

If he's already got the code in functions then the global/local issue doesn't arise whether the function is in the main source or in an include.

What I said was:
Quote: "It shouldn't make any difference whether your functions are in includes or if they are in your main source."

I stand by this. However, I suppose we all assumed in our responses that he was using functions. If he's not, the issue is completely different.

Can you clarify, Tim?

Wyldhunt
16
Years of Service
User Offline
Joined: 27th Sep 2009
Location: The Dark Side
Posted: 14th Apr 2011 16:35
@Agent
You are correct, of course. I'm just trying to predict common mistakes made by people who are still learning these things.

@Tim
I tend to avoid subroutines to keep my spaghetti code to a minimum. As far as I know, the location of your subroutines shouldn't matter because of the reasons Agent mentioned above about the includes getting compiled with the source in to one long program.
If you are saving your variables to make them persistent, and you've been moving stuff around, I'd guess that it's something more mundane. It's easy to get things mixed up as you get more complex.

Without the exact compile error and a code snippet of whatever part of the code is failing, I doubt I can help you beyond that.

Login to post a reply

Server time is: 2026-07-18 10:24:30
Your offset time is: 2026-07-18 10:24:30