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 / "Read Float" changing the value of my array?

Author
Message
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 15th Jul 2009 18:14
Ok i was messing around and decided to make a space invaders type game, well i was reading my map into the game it when it calls read float it changes the value of my array(i have tracked it down to that command, putting a break before the command it is correct, after it isn't)

it's supposed to be 4(it's the image number for the bullets) but it changes it to 20. here's the code:



the first loop it goes to 20 then it goes all random from 0-3 and 20, but it ends on 0 every time. got any ideas?

New Site! Check it out \/
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 15th Jul 2009 18:29 Edited at: 26th Jul 2009 13:34
I don't see a connection with the array "bullet#(0,0)" and the Float value...

You are doing something wrong : A float is a real number (3.453 , 4.34532 , 6.4), and a variable without the "#" sign behind it will be rounded up or down to an integer value (1 , 5 , 24). So if you use "Read float 1,Float", your float wont be a float, but an integer.

If you read your float and store it in "Float", then you break and see what "Bullet#(0,0)" is, I think you know why you can't see your float value. Change
to


Do you get what I mean?

TheComet


Make the path of your enemies easier with Waypoint Pro!
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 15th Jul 2009 18:33 Edited at: 15th Jul 2009 18:36
But Bullet#(0,0) has nothing to do with the reading of the Map information.

it's not supposed to be changing that at all, it's a "Constant" through the hole game. it's the image number for all the bullets.


Bullet#(0,0) was dimmed erlier in a different subroutine, and is only used in the bullet functions, actually it's only used in one of them to paste the image other then that it's not touched through the hole thing.

New Site! Check it out \/
Latch
17
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 15th Jul 2009 18:56 Edited at: 15th Jul 2009 19:13
@Caleb
The extension on your file is .txt . Does that mean the information in there is stored as text? If so, you cannot read back a float from a series of character values. You would have had to write floats in order to read them correctly.

And just to avoid any uncontrollable errors, don't use the keyword Float as a variable name.

And you are using a long type in this call:

Read float 1,Float

Float should be something like flt#

[Edit]
Rereading your post, are you saying Bullet#() is changing? That could be related to the reading of the float value from the file. There is an error I discovered a while ago with using float arrays with DLLs. The values passed back and forth were not accurate. If I used a regular float variable that wasn't an array, it worked fine, but float arrays would not pass the correct values.

A user named empty (I think) said it had something to do with where the float value was stored before it was returned to DBC. The regular float variables were stored in an expected register, but the the float arrays didn't seem to be. Something like this could be going on with your array. Are all of the values in the array changing, or just the first position? If it's only the first position, a workaround may be to store that value in a temporary float while you are reading the file, then assign the temp float value back to the array.

Enjoy your day.
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 15th Jul 2009 20:07
Everything was written to the txt file as floats. i just used txt cuz it was the first thing that came to mind.

i was reading as a long because they wern't actually floats. they were longs originaly.


Float was just a generic name because it wasn't going to be used, just reasigned to another variable.

i'm not sure if it's changing the other parts of the array, i can't get far enough to check(it tries to paste the image and that variable equals 0 so it throws a error.)

i will just reaply the value after it does the map loading. this is really odd lol.

New Site! Check it out \/
TheComet
16
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 15th Jul 2009 21:18
Could you possibly post all of the code? Don't post it if you want it to stay private.


Make the path of your enemies easier with Waypoint Pro!
Brick Break
User Banned
Posted: 15th Jul 2009 23:27
This is easy stuff. What I do is read a string from the file, then convert it to a float.


WINNER list:
Latch, Lee Bamber, TDK
Thanks for the help!
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 18th Jul 2009 00:37 Edited at: 18th Jul 2009 00:39
Comet:

Never mind, i just changed it after that.

i didn't post the hole thing because i didn't see it necessary to post it. i had narrowed it down to that command and was just wondering if anyone had any ideas.

Brick break:

This isn't the problem, the problem was that when i called "Read float 1,Float" it changed the Bullet#() arrays value.

New Site! Check it out \/
Silverman
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 18th Jul 2009 01:33
Open to read 1,"Level1.txt"
For X=1 to 10
For Y=1 to 15
Read float 1,Float
break str$(Bullet#(0,0))
Map(X, Y) = Float
if Map(X,Y) = 2
Create_Enemy(X*32+150.0,Y*32+0.0)
endif
if Map(X,Y) = 1
PX# = X*32+150 : PY# = Y*32
endif
next X
next Y
close file 1



"next X" before "next Y", are you sure ?

AMD Athlon(tm)XP 3200+ (2.2GHz) / DDR pc3200 (1024Mo) / Nvidia 6800GT (driver 178.24 WHQL)/ XP Pro SP3 / DirectX 9.0c (march 2008)/ DBPro 7.1
Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 18th Jul 2009 01:38 Edited at: 18th Jul 2009 01:39
Thats true that isn't correct but idk how that would do this, i will try though. thanks

New Site! Check it out \/
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 18th Jul 2009 23:03
Quote: ""next X" before "next Y", are you sure ?"


You are correct - Next Y should come first, but a 'bug' means that it doesn't matter and the program will still run without an error. DBC doesn't even look at the 'X' or 'Y' parts at all.

It should be corrected however - if nothing else to avoid confusion when tracing through the program.

As for the original problem, as has already been hinted at, it's probably down to memory corruption as you are breaking a few rules in your code.

Firstly, only use floats if you actually need them. Otherwise, stick to integers - they are faster. I only base this comment on the line in your code:

Map(X, Y) = Float

...which is storing whatever was loaded in an integer array anyway. Even if the 'load float' had worked, not using Map#(Y,Y) would have simply cast it as an integer.

Same goes for the line:

PX# = X*32+150 : PY# = Y*32

Those calculations mean that PX# and PY# can never be real numbers - only integers, so need to be PX and PY. (Or you could force floats by adding .0 on the end of the constants). But, as I said before - stick to integers whenever possible.

Without seeing more of your code I can only assume that your array Bullet#(0,0) need not be a float either. You say "it's the image number for all the bullets" - and images cannot be anything other than integers.

Actually, if you are only using the array to make the image number available in all functions then it probably doesn't need to be multi-dimensioned or a float array. Bullet(0) should do.

TDK

Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 19th Jul 2009 01:27
I geuss i could use write word or something that way it wouldn't need converting to a integer since the only thing in that file is integers.


Quote: "
Actually, if you are only using the array to make the image number available in all functions then it probably doesn't need to be multi-dimensioned or a float array. Bullet(0) should do."


Well that array actually stores all the information about the bullets themselves. thats why it's a float, it stores the x and y of the bullets.

see the bullets are used like this:

when you press fire it creates a bullet and stores the x and y position and information. then all bullets on the screen are updated and is checked for collision every loop.

so that array is all the information on all the bullets.

I was using floats for PX# and py# because i was moving the player by 1.5 every loop. i geuss i could round up or down. i will change these and see what happens. thanks

New Site! Check it out \/
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 26th Jul 2009 02:00
Quote: "Well that array actually stores all the information about the bullets themselves. thats why it's a float, it stores the x and y of the bullets."


But 2D X and Y screen positions cannot be floats - only integers.

Quote: "I was using floats for PX# and py# because i was moving the player by 1.5 every loop."


If it's a 2D space invaders game then you can only move anything on screen in pixels. As you can't have half a pixel then every time you use 1.5, DB simply has to drop the .5 bit - effectively converting your float to an integer.

So it would be quicker for your program to use integers natively.

TDK

Caleb1994
15
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 26th Jul 2009 07:02
Well the reason i used 1.5 is because 2 seemed fast and 1 seemed slow. thats why i startd using floats.

New Site! Check it out \/
Silverman
17
Years of Service
User Offline
Joined: 18th Jan 2007
Location: France
Posted: 26th Jul 2009 13:04
this code reproduces your problem? If not, can you rewrite it to create the bug ?



AMD Athlon(tm)XP 3200+ (2.2GHz) / DDR pc3200 (1024Mo) / Nvidia 6800GT (driver 178.24 WHQL)/ XP Pro SP3 / DirectX 9.0c (march 2008)/ DBPro 7.1
TDK
Retired Moderator
21
Years of Service
User Offline
Joined: 19th Nov 2002
Location: UK
Posted: 26th Jul 2009 18:30 Edited at: 26th Jul 2009 18:35
Quote: "Well the reason i used 1.5 is because 2 seemed fast and 1 seemed slow. thats why i startd using floats."


Ah OK - I understand now!

Using floats is OK, but you would be better off using timers or at the least, some form of counter which stops the position incrementing routine from being called EVERY time you go around the main loop.

Basic example:



The advantage of this method is that the object moves at the same speed on every machine regardless of how fast or slow it is.

You also have full control of the object's speed. Using a variable like SpriteSpeed (or an element of an integer array) you can increase and decrease it's speed at will at any point in the game.

Note: You can create other timers (like MoveTimer) in the same way for other tasks.

TDK

Login to post a reply

Server time is: 2024-05-20 06:15:19
Your offset time is: 2024-05-20 06:15:19