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 / Pasting a lot of images, scrolling sometimes stutters

Author
Message
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 16th May 2013 16:16
Here is the problem: I have a 2D scrolling game that sometimes stutters, once or twice every minute for a second or so.

How I do it: I have an array that contains image numbers. The images are pasted to the screen. I only read the area in the array that is currently beeing shown.

I use nothing but arrays, variables, paste image and a handfull of other stuff.

I tried shaving nearly everything away but it still stutters now and then. Any ideas? If no one got an idea I will try and make the smallest possible piece of code and post it with media.

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 16th May 2013 21:28
Yeah, please do post an example; there are too many likely causes.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th May 2013 01:44
When you say stutters, do you mean you're getting a horizontal line shifting upward that appears to shear the pixels across the screen slightly? If that's the case, there's a flag for the sync command that has something to do with the vertical sync. I can't remember exactly, it might just be sync 1.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Mr Bigglesworth
18
Years of Service
User Offline
Joined: 4th Mar 2008
Location:
Posted: 17th May 2013 02:40
Also, don't paste images to make a game, it is too slow. Use sprites instead, they are really fast.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 17th May 2013 14:42
And if you have not yet tried Advanced 2D, now is a good time to make use of its rapid image drawing commands. I use it to the screen without any noticeable frame-rate drop.

Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 17th May 2013 15:14 Edited at: 17th May 2013 17:50
@Phaelax: Are you talking about tearing from V-sync? If so, no, it is not that kind of stuttering.

@Mr Bigglesworth: I have achieved very high looping count using paste image only instead of sprites. I have tried sprites instead of paste image but it still stutters.

@Chris Tate: No, I have not tried advanced 2D. I will try that very soon Is it a part of Extended? I have made a small package with a "Paste image only" and "Sprite only" source code and I chopped everything not needed away for demonstration of the stuttering. I would like to run it in fullscreen but when I try to it comes up with a runtime error stating it is missing an image, so I run it in windowed fullscreen instead. I am running this on a dual screen setup and I am using the primary monitor for the game. I noticed I get a lot more stuttering if I run something in fullscreen at the secondary monitor while running the game.

EDIT: Updated the attached file

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th May 2013 16:09 Edited at: 17th May 2013 16:16
Quote: "@Phaelax: Are you talking about tearing from V-sync? If so, no, it is not that kind of stuttering."

yea.

Quote: "Also, don't paste images to make a game, it is too slow. Use sprites instead, they are really fast."

I've always stuck to paste sprite in DBP and never had any performance issues.

Quote: "I noticed I get a lot more stuttering if I run something in fullscreen at the secondary monitor while running the game"

Sounds like a graphics card issue. I remember when I worked on a media center program, I got great frame rates on my monitor, but when I moved the program onto my secondary screen the frame dropped a lot and would run choppy. Fortunately, that type of application didn't require a high frame rate.


I tried to run your program, but it's missing several files. Basically, all the ".bca" files in the Bitmaps directory.

I was able to run the sprite version, but not the paste version. The only "stutter" effect I could see was a vertical sync issue which I fixed with set display mode 1280, 720, 32, 1

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Mr Bigglesworth
18
Years of Service
User Offline
Joined: 4th Mar 2008
Location:
Posted: 17th May 2013 16:28
What are your computer specs?
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th May 2013 17:00
I'm not sure what's meant by "stuttering" but when I run the sprite version and hold down the up (or down) key the image scrolls smoothly except every now and then it halts for a just noticeable fraction of a second. Is that what you mean? I've seen it a lot in my own DBPro demos where images are scrolled and have often wondered about the cause. Adding the sync flag makes no difference.

I believe it also happens when you continually rotate objects.

In my own demos I wondered whether it was simply an optical illusion caused by the way the eye and brain work together. For example, it's very hard not to follow a scrolling image or rotating object but the eye obviously has to "reset" itself as the spot it is subconsciously focussed on disappears from view. That might be enough to give the impression of halting or stuttering even when there isn't any. Can anyone think of a way of ruling out that possibility?
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 17th May 2013 17:10
Quote: "GG: when I run the sprite version and hold down the up (or down) key the image scrolls smoothly except every now and then it halts for a just noticeable fraction of a second."



That's what I'm getting too...

Cybermind, I noticed that your subroutines are in separate .dba files.
Have you considered hard-coding these into your main .exe instead, or have you tried that already?

D.D.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th May 2013 17:16
Quote: "I noticed that your subroutines are in separate .dba files."


That shouldn't be relevant to this issue and is a standard way of organising large projects.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 17th May 2013 18:09
I have updated the attached file. Sorry for the paste image version, it gets confusing working with all those projects that are only slightly different from each other

@Phaelax: Well, I gueess you are right I put the vertical sync flag on and it seems like the stuttering is all gone I think I had this issue early in the development and I put the vertical sync on which fixed it back then also :-P I have been fiddling with the screen stuff and I had forgotten all about that vertical sync flag Thanks a lot!

@Mr Bigglesworth: My specs are CPU: AMD Phenom II X4 955 3.2GHz, RAM: 4GB DDR2 667MHz, GFX: ATI Radeon 6870 1GB, SYSTEM DRIVE: OCZ Agility 3 SSD 120GB, STORAGE/WORK DRIVE: Seagate HDD 500GB, MOTHERBOARD: Gigabyte GA-MA770-UD3, OS: Windows 7 Home Premium 64bit.

@Green Gandalf: It was stuttering that lasted about 1 to 3 seconds so I don't think it was an optical illusion :-P And as said, the vertical sync flag fixed it

@Derek Darkly: It is only a fraction of the code that was in the example I posted. It would be impossible to manage all the code in one document :-P I have at the moment over 6000 lines in the game itself

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th May 2013 18:16 Edited at: 17th May 2013 18:17
I'd assumed that vsync didn't solve it when you wrote:

Quote: "Are you talking about tearing from V-sync? If so, no, it is not that kind of stuttering."


I guess it was that kind of stuttering after all. [But I wouldn't expect that to last 1 to 3 seconds though. ]

We still have to explain the stuttering that both Derek Darkly and I have noticed.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 17th May 2013 18:31
@Green Gandalf: You are absolutely right, I just thought that v-sync problems tore a horizontal line dividing the screen in two, differently updated sections :-P

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 17th May 2013 18:34
It's possible that such tearing just wasn't noticeable in your particular application.

Anyway, the important thing is that you've fixed it.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 17th May 2013 18:36
And I won't forget that vertical sync flag again anytime soon

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Derek Darkly
14
Years of Service
User Offline
Joined: 22nd Sep 2011
Location: Whats Our Vector, Victor?
Posted: 17th May 2013 22:38
Cyber, how did you use external subroutines without the #INCLUDE command? This is probably common knowledge, but I can't seem to pull it off.

D.D.
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th May 2013 01:34
Quote: "Cyber, how did you use external subroutines without the #INCLUDE command? This is probably common knowledge, but I can't seem to pull it off."


You can do it very simply in the default editor using the File/Open/File...(Add to project) option from the main menu.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th May 2013 02:10 Edited at: 18th May 2013 02:11
Quote: "What are your computer specs?"

i7-3770k
2GB GTX 560
16GB ram


Quote: "wouldn't expect that to last 1 to 3 seconds though"

It was barely even noticeable to me. Like a few milliseconds maybe.



Why are you using strings to check for game states? This would make more sense (and more efficient than using string comparisons)



"You're all wrong. You're all idiots." ~Fluffy Rabbit
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 18th May 2013 12:54 Edited at: 18th May 2013 12:56
Quote: "Why are you using strings to check for game states? This would make more sense (and more efficient than using string comparisons)"


I agree 99% with what Phaelax said. An integer comparison is faster than a string comparison. But this program is not complex enough for you to notice the difference isn't it?

The microsecond or so you lose is only 0.1% of one millisecond in the typical 15 milliseconds you have to play with in VSync mode. Doing a single string check 60 times per second is not going to KO frame-rate.

It is good practice to use integers and boolean states (See boolean states section of my article).

Sometimes using string states requires less work, allows easy to implement user definition and can reduce code. But when performing more than 1000 of these string comparisons in a single frame, it would be better to compare integers or boolean states.

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 18th May 2013 13:00
The performance difference is negligible, but I'm just trying to push for better practices. You wouldn't someone do that in a real world environment.

Plus, my first programming classes were taught using PCs with Cyrix 133mhz cpus, we needed every millisecond!

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th May 2013 13:20
Quote: "Plus, my first programming classes were taught using PCs with Cyrix 133mhz cpus, we needed every millisecond!"


Mine used punched cards and at some point we moved on to paper tape. Those were the days ...

We didn't use string though.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 18th May 2013 13:40
Quote: "The performance difference is negligible, but I'm just trying to push for better practices."


I do actually try to get the best perfomance possible, and I am very glad to recieve advice on better programming practices because I need that, a lot!

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 18th May 2013 16:25 Edited at: 18th May 2013 17:48
Quote: "You wouldn't someone do that in a real world environment. "


Absolutely not; to quote myself

Quote: "It is good practice to use integers and boolean states"


And typically, we all use some software once and a while that compares strings, be it lookup tables or inputs.

I also posted an article that discusses using boolean states; which are pretty cool, you use the bits of the variables to switch and even combine various states, or flags.

For me, if it does not affect performance do not optimize it; if comparing strings saves more time and gets the job done without affecting performance in a given situation, it is good to save time and get the job done; there are more crucial things to watch out for in DBP than comparing one string; I've seen pretty high profile programming careers compare strings frequently; OK mostly to search and filter or simply just to test out something or to save time creating constants; it's not best practice when you don't know what you are doing.

I would have created a 1000 line program and created a string comparison and not feel bad about it; I do have a 70,000 odd line project between DBP, VB.NET and LUA, 70,000 string comparisons would probably run about 0.01 FPS; but one string comparison isn't going to hurt as I can clearly see quite literally if I add one hundred string comparisons to a loop (1300 uncapped FPS to be precise on a below average machine).

Quote: "Plus, my first programming classes were taught using PCs with Cyrix 133mhz cpus, we needed every millisecond!"


I hate to sound rude or disrespectful, and I do know you have been programming for many more years than me; and I do know where you are coming from with providing good advice; I remember my first Windows PC had a 50MHz processor. I remember the buzz and the big deal when we got to the 133 mhz mark... I used to be jeolous of people with more than 100MHz; 1GHz "what is a gigahertz?"

but we are not exactly dealing with 133 Mhz CPUs today, when you build an accelerated DirectX video game today, you tend to build for tomorrows hardware; an average PC has two CPUs and an accelerated graphics card.. There is no need for me to even start to talk about the clock speeds of todays hardware let alone tomorrow.

Every millisecond counts, but in a limited amount of time that we have, as we age and as technology becomes obsolete there are more vital bottle necks that WILL take up many hours of your time trying to track, there are more fierce monsters out there that will eat away your performance than a comparing a few strings.

Everyone who programs with scripting; knows that strings are being compared; every AAA title on my hard drive uses some form of scripting and is thus comparing strings on a per loop basis; alot of these strings are dynamic and compiled frequently in small chunks as you play; check out all the LUA code snippets in the forum, check out the scripting languages and string comparisions used by Valve and Firaxis in their engines. Todays machines are simply comfortable with real time scripting, therefore are comfortable with moderate use of string comparison.

Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 18th May 2013 19:38
Quote: "The performance difference is negligible, but I'm just trying to push for better practices. You wouldn't someone do that in a real world environment. "

atta' boy.

Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 18th May 2013 20:16 Edited at: 18th May 2013 20:21
At the moment there are a few string comparisons each loop and it is not that big a deal for me to change them to constants instead, but I better do it now before I expand that part of the code

EDIT: Could you please explain further Phaelax? Why do I put the "=1" after #CONSTANT STATE_MAIN?

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Mage
Valued Member
19
Years of Service
User Offline
Joined: 3rd Feb 2007
Location:
Posted: 18th May 2013 20:29 Edited at: 18th May 2013 20:37
Watch out with VSync because it can lock you at 30fps if you aren't fast enough to get 60fps.


Requires Sync On and Sync Rate 0...

I implement a frame limiter instead. This example rolls syncing the screen and a frame limiter all into one easy function. It is also CPU friendly.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 18th May 2013 23:25
Quote: "Could you please explain further Phaelax? Why do I put the "=1" after #CONSTANT STATE_MAIN?"


You're right - you don't need the "=" but without the "=" you'll need the space. In other words the following are equivalent:



and

Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 19th May 2013 09:24
Quote: "I hate to sound rude or disrespectful, and I do know you have been programming for many more years than me; and I do know where you are coming from with providing good advice; I remember my first Windows PC had a 50MHz processor. I remember the buzz and the big deal when we got to the 133 mhz mark... I used to be jeolous of people with more than 100MHz; 1GHz "what is a gigahertz?"

but we are not exactly dealing with 133 Mhz CPUs today, when you build an accelerated DirectX video game today, you tend to build for tomorrows hardware; an average PC has two CPUs and an accelerated graphics card.. There is no need for me to even start to talk about the clock speeds of todays hardware let alone tomorrow."


I know what you're saying. The only point I was trying to make with that reference was that from the hardware I started on, I was focused on those tiny efficient details and they have carried over to today, even if they aren't a big deal anymore. It's just a habit of mine to want to gain every extra tick possible.


Quote: "EDIT: Could you please explain further Phaelax? Why do I put the "=1" after #CONSTANT STATE_MAIN?"


Doesn't really matter what number you assign it. Just use the constant name in place wherever you were going to use the string "main". Whether assigning it to the state variable or in a comparison check. Using constants this way, you still have a variable with a meaningful name so you know what it is, but rather than doing string comparison it'll do integer comparison. The end result is the same, but this is just.... better.


Just a quick example showing there is a speed difference between integer and string comparisons. (even though I had to do 10 million iterations to time a difference)

10 million integer comparison checks take about 20ms, whereas strings took 80ms. That's 4x slower for strings. And that's really the best case scenario for strings as well. Because in this example we're using an empty string thing$. If the variable matches the target string, it'll take longer to check. And the longer the string, the slower it gets if the characters all match.



But like I said, you won't notice the difference unless you're doing over a million comparisons.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 19th May 2013 10:40 Edited at: 19th May 2013 11:18
Quote: "The only point I was trying to make with that reference was that from the hardware I started on, I was focused on those tiny efficient details"


I know; and I wish I did too, I admire those people who can entertain me with just 8 bits.

Quote: "It's just a habit of mine to want to gain every extra tick possible."


I had your principle in mind when I first started, I have thousands of constant state integers in my project, and continue to practice this habit as well. But as I go I am seeing that a lot of such optimization has wasted time. But this not an issue for a 1000-5000 liner project or something concise targeting mobile hardware.

I found out that using loads of constants takes longer to manage than using alternative means such as function pointers, hash-tables, UDT arrays, flags, XML, LUA, and string databases. One of many scenarios is LUA needing to check the state and not DBP; another is needing to store states as strings anyway to display visual feedback; another is later on wishing to change functionality without re-compiling 1000s of lines.

I now choose different methods of handling states according to specific needs (Favorite being function pointers, scripts, UDT enumerators and flags); but I tend to default to defining things outside the program; in my real world environment, recompiling to add just one or two new constants is an absolute no no. But for something concise, it is good because compiling only takes less than a minute.

Quote: "Using constants this way, you still have a variable with a meaningful name so you know what it is"


I also personally find it annoying searching through 1000s of constants to remember the name of one of many states. I prefix the name of the constant with namespace; as you did. Which is why 'namespacing' and auto-complete is handy, but only good when auto-complete works.... :-|

Quote: "10 million integer comparison checks take about 20ms, whereas strings took 80ms. That's 4x slower for strings."


A lot states only need checking once every second, some once a minute, for me 4 times per second for each TCP packet update, at most a human is not going to interact with the game more than 10 times per second 99% of the time unless it is an intense Dark Dragon fighting match or an Olympic button bash game; so you'd have to check a good billion strings to upset your AMD/Intel hamster that way, which is not going to happen.

The few microseconds lost for comparing a string is actually less expensive than for example using a print command instead of a2Text; both commands can display the information to the user, print will get the job done easier and is more productive with concatenation, a2Text runs faster, but we tend to use print because it is handy. So I suppose that's why my gut feeling pushed me to contribute a little feedback about constants being running faster, but not being all that handy in the long run in few situations as mentioned.

Integers are best

A word for newbies, don't even bother with bytes, booleans or words; stick to integers. All my frame-by-frame stuff is integers and references mostly handled by plugins, so if it needs to be performed 60/30/What ever refresh target times per second, stick to integers. Each use of any other non-precision data type is converted to integers for calculation. Booleans take up as much ram as bytes. May as well store up to 32 booleans in one integer using flags.

Quote: "Because in this example we're using an empty string thing$. If the variable matches the target string, it'll take longer to check. And the longer the string, the slower it gets if the characters all match."


With the string comparison, indeed each character of both string operands are compared one by one, to the end of the string I am guessing; so it will be slower. I have done this speed test before when comparing long string comparison Vs multiple short string comparisons for my message system.

So my guess that a medium word string comparison is less than about 0.000001 seconds is more or less confirmed by your experiment.

I also experimented and did not manage to affect my 1300 uncapped FPS on my crappy PC comparing the series "abcdefgh" with itself 100 times per loop; which shows that doing stupid amounts string comparisons will make you lose frames especially when capped; but it is safe to do a moderate amount of real-time script parsing or minor string comparisons if done with reasonable sized strings or done 4 or so times per second.

....

Another thing that bothers me is the use of long Select statements; there is this huge novel of AI script state select case AI_STATE constant set-up in FPS Creator source code; checking through all of those integer comparisons every AI command and comparison in every loop is something that looks like a bottle neck to me, that seems like it would be better as a short circuited branch with 32 bit or perhaps 64 bit flags.

This is the perfect example of where NOT to do string comparisons; although the parsing of the AI script requires string operation.

The main thing is it works; but people say FPSC is slow, maybe - just maybe this contributed to it; possibly Lee Bamber did this for the same principle, good practice?



Do you not also agree that this is a bit much? What do you do when you realise the overhead? Restructure all of that?

The real snippet goes for just under 2000 lines; if all are false, ALL cases are compared for all script codes for each entity in the live game and is performed on every frame. Integer or not, I am sure this will eat FPS for breakfast. Note this may not be the case in the latest version.

It is easy to say when it is not your project, but I am sure that this part of the code would run 500% faster with function pointers. Just store a function reference in the state and call the function stored in the command; no need to check each integer one by one until you find the match.

Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 19th May 2013 18:41
Quote: "Doesn't really matter what number you assign it."


But why do I have to assign a number at all then?? And there will be string comparison somewhere anyhow, right?? Because STATE_MAIN is still a string, sort of :-P

Is assigning a constant, either one in this case, to variable state not just making variable state becoming = 1?? Would this work??

#CONSTANT STATE_MAIN 1
#CONSTANT STATE_BATTLE 1

state = STATE_MAIN

do
if state = STATE_MAIN
code code code
state = STATE_BATTLE
endif
if state = STATE_BATTLE
code code code
endif
loop

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 19th May 2013 19:34 Edited at: 19th May 2013 19:55
Nah, the state will store an integer. This is how the compiler will interpret what your wrote



Commands prefixed with a hash (#) are compiler commands; the constant command replaces all occurances of the name with the literal; so

Quote: "
#CONSTANT EGG 1
If EGG = 1 Then Print "True" `>> Prints True
"


Quote: "
#CONSTANT = EGG 1
If EGG = 1 Then Print "True" `>> Prints True
"



Quote: "
#CONSTANT EGG 10-9
If EGG = 1 Then Print "True" `>> Prints True
"


Quote: "
#CONSTANT EGG "Hello World"
If EGG = "Hello World" Then Print EGG `>> Prints Hello World
"


Quote: "
#CONSTANT EGG If 1 = 1 Then Print 100
EGG `>> Prints 100
"


So all occurances of egg are replaced with what follows the constant command; then your code is assembled into the EXE.

You shouldn't generally do what I did in the last example; but it demonstrates the behavior of the process quite literally.

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th May 2013 00:22
Quote: "You shouldn't generally do what I did in the last example; but it demonstrates the behavior of the process quite literally."


Why? That's the whole point of #constant - to replace a cumbersome or opaque bit of code with something shorter and more memorable, whether it's



or



is entirely up to the programmer.
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 20th May 2013 04:01
Quote: "But why do I have to assign a number at all then?? And there will be string comparison somewhere anyhow, right?? Because STATE_MAIN is still a string, sort of :-P"

A constant is a static variable who's value you cannot change.

If you have to assign something to 'state' to know what it is don't you? The constants need a value assigned to them as well otherwise they'll all equal the same thing.

"You're all wrong. You're all idiots." ~Fluffy Rabbit
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 20th May 2013 12:19 Edited at: 20th May 2013 12:20
Quote: "Nah, the state will store an integer. This is how the compiler will interpret what your wrote

+ Code Snippet
state = 1
do
if state = 1
code code code
state = 1
endif
if state = 1
code code code
endif
loop"

Okay, I get it Thats why there is no string comparison So I don't necessarily have to assign different constants with different things, if they get the same thing applied they just get interpreted to the same thing?

Quote: "#CONSTANT EGG If 1 = 1 Then Print 100
EGG `>> Prints 100"

Can you store a command in a constant??

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 20th May 2013 12:58 Edited at: 20th May 2013 13:45
Quote: ""You shouldn't generally do what I did in the last example; but it demonstrates the behavior of the process quite literally."

Why? That's the whole point of #constant - to replace a cumbersome or opaque bit of code with something shorter and more memorable, whether it's"


I never said it wasn't; quite literally EGG is not a memorable term for IF 1 = 1, yes for some strange person who wants to use the term EGG that way in a hobby or business project; no for Cybermind.

So let us enter into another topic on this thread with learners in mind; this is not me suggestion a learner go into this before learning how to program in the first place.

I mentioned it with Cybermind in mind; no pun intended.

So to go into further detail now; Cybermind should not do it generally; not saying that it must not be done ever. A bit like saying, you should not use native 2D commands generally. I posted the example to show what is possible because I do it, but not generally; or in other words not in every situation or frequently.

Most of your program should be structured code that easy to read, hiding general language syntax that way on a general basis makes it difficult to read. I know when you first look at the feature as a learner you can tend to go wild with it.

Here are examples of how it used in my engine:

Example 1:

Quote: "
#CONSTANT NEW_SMALL_FONT a2CreateFont("Sports Fiction Body", 8, a2Size_Char(), a2Style_Normal())"


I am always creating test programs. So include a shared library which contains the above constant. When I need to create an A2 Font in a seperate test program; I just write NEW_SMALL_FONT.

Example 2:
Quote: "
#CONSTANT CPOS Camera Position X(), Camera Position Y(), Camera Position Z()
"


I like to pass the camera position as parameters, having to type that 100 times per day sucks so I just type CPOS instead; it is reused so often that the meaning of CPOS is as memorable to me as ABS or SQRT


An example of something too general in my opinion for constants.

Example 3
Quote: "#CONSTANT HandleCharacter10 If Character(10).Health# <= 0 Then GoSub KillCharacter(10)"


That is a condition and command better suited inside function, sub-routine or branch using a variable reference to the character ID. The constant name is too vague and rigid to focus on only character 10.

Example 4
Quote: "#CONSTANT IfTrueThenPrintIt If A = B Then Print A"


This is too general because it is vague; if what is true? Print what? What if it was something more delicate that causes a compiler error or run-time error; the IDE will not reveal the problem as directly.

This may look obvious to you Gandalf, but I assume this is not obvious to a learner.

Edit
Quote: "
Quote: "#CONSTANT EGG If 1 = 1 Then Print 100
EGG `>> Prints 100"
Can you store a command in a constant??"


here we go now, lets not get excited; consider what I posted above if you skipped past it.

You can put any single line command into a constant Cybermind; any command that is not #constant or #include. The constant name must not be reserved and you should namespace with a prefixed word indicating the context of the constant the less common its purpose. EG: PLAYER_START_HEALTH and AI_START_HEALTH instead of START_HEALTH. Notice that the convention is to use different format for defining constant names; in my case I use uppercase characters delimited with underscores.

Spend some time practicing around with it first before putting it to use directly; try to grasp the basics first before jumping into the deep end, avoid example 3 or 4 where you are dealing with vague references or hiding too much detail, and name the constant with something as descriptive as possible. Exhausted now...

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th May 2013 15:10
Quote: "Exhausted now..."


Phew! I'm not surprised.

@Phaelax

I'm not quite sure what you meant when you said:

Quote: "A constant is a static variable who's value you cannot change."


The general situation regarding #constants is as Chris Tate has described. They are simply a directive to the compiler to replace all occurrences of the constant name by the defining expression (which may or may not be a simple constant). That is done before any further syntax checking etc. They are nothing more than an aid to the programmer and are not the same as constant variables () as used in many other languages.

@Chris Tate

Quote: "This may look obvious to you Gandalf, but I assume this is not obvious to a learner."


Agreed on both counts - except that many DBPro programmers who are not "learners" still seem confused by the issue.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 20th May 2013 15:49
true

Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 20th May 2013 20:59
Thank you so much guys! I truly appreciate the time and effort you put into this thread, it has given me a lot! In fact, I went out on the interweb and found me copy of "Hands on DarkBASIC Pro Volume 1". I realized I have build way too much of my game without some proper fundamentals, other than those I invented my self (I take your reality and subsistute it with my own)

I can't give you much except my deepest thanks and a rare screenshot of my current game

There is one attached to this post and a few old ones in the link: https://plus.google.com/u/0/photos/109413213427864221266/albums/5772880687026844993

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 20th May 2013 21:48
Looks good. Have you considered starting a Work In Progress thread? You seem to have enough to justify one.
Cybermind
Valued Member
23
Years of Service
User Offline
Joined: 28th Nov 2002
Location: Denmark
Posted: 20th May 2013 22:18
Quote: " Have you considered starting a Work In Progress thread? You seem to have enough to justify one."

Yes, I have and chosen not to Even though the editors and the game it self have been in development for about 2-4 hours a day more than a year now I do not wan't to show much before it is nearly done I have decided to read the Hands on books and then perhaps change the game code structure afterwards, something I have been meaning to do for a long time but I do not know how "proper" code structure looks :-P I do not think I am going to change the editors code as they are for me only

The byte chrunchers are coming...
DIVIDING BY ZERO/BECAUSE WE SUCK...
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 21st May 2013 02:36
Looks like your game's editor is taking shape; keep up the good work.

Login to post a reply

Server time is: 2026-07-07 08:19:28
Your offset time is: 2026-07-07 08:19:28