# coding problem

Discussion in 'General Discussion' started by zed, Apr 25, 2019.

1. ### zed Veteran

Joined:
Dec 16, 2005
Messages:
4,793
934
EDIT: OK my bad, board_heights[ num ];
so I solved it, doh!

Sorry for the thread, but my mind cant think, this is one of those problems Ive had over the last 30 years, where it doesnt make sense, usually its cause Ive done something wrong
Code:
```for ( int x=0; x<VERT_HEIGHT_RESX; x++ ) {
for ( int y=0; y<VERT_HEIGHT_RESY; y++ ) {
board_heights[x+VERT_HEIGHT_RESX*y] = 0.0f;

int num = dungeon.tiles[x+VERT_HEIGHT_RESX*y];
if ( num >= 'a' && num <= 'z' ) {
board_heights[x+VERT_HEIGHT_RESX*y] = dungeon.ReturnFloatFromKeyValue( num );
print( board_heights[x+VERT_HEIGHT_RESX*y] );[B] [COLOR=#ff4d4d]// good values[/COLOR][/B]
height_field[ x+VERT_HEIGHT_RESX*y ] = board_heights[x+VERT_HEIGHT_RESX*y];[B] [COLOR=#ff4d4d]// works[/COLOR][/B]
}
}
}

for ( int x=0; x<VERT_HEIGHT_RESX; x++ ) {
for ( int y=0; y<VERT_HEIGHT_RESY; y++ ) {
int xx = x;//(x<VERT_HEIGHT_RESX) ? x : VERT_HEIGHT_RESX-1;
int yy = y;//(y<VERT_HEIGHT_RESY) ? y : VERT_HEIGHT_RESY-1;
float height = 0.0f;
int num = dungeon.tiles[ xx+VERT_HEIGHT_RESX*yy ];
if ( num >= 'a' && num <= 'z' ) {
height = board_heights[ num ];
print( height + " " + num ); [B][COLOR=#ff4d4d] // always 0.0[[/COLOR]/B]
}
}
}```
why is height always 0.0?
ta zed

edit ok you cant do color or bold in a code field

#1
Last edited: Apr 25, 2019
2. ### Davros Legend

Joined:
Jun 7, 2004
Messages:
15,970
3,209
For god sake man comment your work

#2
3. ### zed Veteran

Joined:
Dec 16, 2005
Messages:
4,793
934
no time for comments , though seriously I do occasionally place comments in, though more along the lines of WTF, etc

#3
McHuj likes this.
4. ### Davros Legend

Joined:
Jun 7, 2004
Messages:
15,970
3,209
put a breakpoint just before put a watch on the variable you want and step through

because you don't have a variable called height
edit:
yes you do and you've assigned it a value of 0.05859375

#4
Last edited: Apr 26, 2019
5. ### Davros Legend

Joined:
Jun 7, 2004
Messages:
15,970
3,209
for ( int y=0; y<VERT_HEIGHT_RESY; y++ )
shouldnt that be a while loop or a repeat until
eg:
y=0 *initialise variable
while y<VERT_HEIGHT_RESY
begin;
do stuff
increment y
end;

#5
6. ### BRiT Verified (╯°□°)╯ ModeratorLegendAlpha

Joined:
Feb 7, 2002
Messages:
15,924
14,842
Location:
Cleveland
Syntactically, While / Repeat loops are worse looking than foreach / for loops, especially when you don't need the loop counter to exist when the loop is finished.

Just my own quirkiness style.

#6
7. ### Davros Legend

Joined:
Jun 7, 2004
Messages:
15,970
3,209
I dont know the language so i dont understand this line
num is an integer - yes ?
but your trying to assign it a value of "a" which is a char I see no place where a variable called a is declared
ps:
is && logical and ?

#7
8. ### zed Veteran

Joined:
Dec 16, 2005
Messages:
4,793
934
I’m using unity, I never actually took the time out to figure out how to do stepping through the code with it, eg with breakpoints etc hehe
I should take the time and learn

I prefer for loops also , or foreach better (though slower)

Davros, I’m just comparing two numbers, sure I chould cast the char to an int, but hey (or is it a char?) cause it doesn’t complain so perhaps the compiler converts ‘a’ to 65int during compilation

FWIW this is what I’m coding

#8
9. ### BRiT Verified (╯°□°)╯ ModeratorLegendAlpha

Joined:
Feb 7, 2002
Messages:
15,924
14,842
Location:
Cleveland
Typically in C based languages, chars are shorts and autocast to ints.
That loop is two expressions joined by the logical AND ( && ) operator.
Its roughly: if the character is inclusive between lowercase A and lowercase Z.

#9
10. ### Davros Legend

Joined:
Jun 7, 2004
Messages:
15,970
3,209
But you should use for loops to loop a certain number of times eg: for i = 1 to 10 or for i = 1 to x
you shouldnt use for loops as a loop while condition is true or a loop until condition = false

ps: 0.0f is hex yes
here :
https://www.rapidtables.com/convert/number/hex-to-decimal.html
gives 0.05859375 in decimal

here :
https://www.binaryhexconverter.com/hex-to-decimal-converter

here :
https://codebeautify.org/hex-decimal-converter
gives 0

pps:
board_heights[x+VERT_HEIGHT_RESX*y]
is that an array of board_heights ?

#10
11. ### BRiT Verified (╯°□°)╯ ModeratorLegendAlpha

Joined:
Feb 7, 2002
Messages:
15,924
14,842
Location:
Cleveland
I disagree with that statement.

#11
12. ### Malo Yak Mechanicum LegendVeteranSubscriber

Joined:
Feb 9, 2002
Messages:
7,684
3,758
Location:
Pennsylvania
There are conditions in programming where your loop is based on an abstract condition becoming true, rather than simply looping for X times. A while until true is perfectly fine for certain circumstances.

#12
13. ### zed Veteran

Joined:
Dec 16, 2005
Messages:
4,793
934
Yes I sometimes use while loops then, though less frequently then I used to.
As there’s the greater possibility of being stuck in an infinite loop

#13
14. ### N00b Regular

Joined:
Mar 11, 2005
Messages:
698
114
I think the code is pretty self-explanatory. All the variables and member have meaningful enough names. If you have trouble understanding it, you should work on your ability to read foreign code. And I don't mean to be condescending. Reading foreign code fluently is one most important skills for a developer. Relying on comments to understand code is, IMHO, often not a good idea, since comments can be outdated, vage, incomplete, misleading or plainly wrong. That said, comments however can be a good idea if your doing something out of the ordinary like optimizations or advanced math.
I think if you use for or while/do while loops is mostly a matter of personal taste. I usually prefer for because you have initialization expression(s), exit condition and iteration expression(s) all in one line while the "business logic" is in the inner scope. That way it is better separated and easier to read, in my opinion at least.
No. Since Zed is using Unity, the programming language is clearly C#. That means 0.0f is a float (as denoted by the f postfix). A hex number would have a 0x prefix, e.g. 0xdeadbeef = 3735928559.
That looks like really nice, oldskool fun! Any idea when it will be finished?

#14
15. ### zed Veteran

Joined:
Dec 16, 2005
Messages:
4,793
934
Finish date? I don’t even have a title yet!
But it’s coming along good, though regretting buying my pc, with intel onboard gfx, after coming from nvidia which is rock solid it’s painful, so unstable I have to reboot the pc, a few times a day which cuts down production time

#15
16. ### N00b Regular

Joined:
Mar 11, 2005
Messages:
698
114
Intel onboard graphics? Ouch! Punish yourself much? Can't you just buy a add-on graphics card? Or did you buy a mini-pc or something similar?

#16
17. ### zed Veteran

Joined:
Dec 16, 2005
Messages:
4,793
934
yes mini pc, I was looking into maybe getting a external graphics card something cheap nvidia 750 or something, but from looking external graphics seem more expensive than just buying a new PC. Unless someone knows of a cheap option?

#17

Replies:
5
Views:
459
2. ### scan the qr code on the website where you enable 2FA

zed, in forum: Site Feedback
Replies:
0
Views:
858

Replies:
7
Views:
1,040
4. ### Prepare to dine -Code Vein. Namco announces some kind of Dark Souls with vampires,manga characters<3

Cyan, in forum: Console Gaming
Replies:
6
Views:
1,457

Replies:
7
Views:
1,019

Replies:
4
Views:
688