Skip to main content
GameDev.net gamedev.net
🔒 Locked

Tile map loader problem (visual basic 4.0)

Started by Life unlived Mar 9, 2008 at 12:29 PM 6 replies 1.5k views
Original Post
Life unlived
Life unlived
Well actually there isn’t a problem with my map loader. It seems to be working fine just , I want it to work its just that I’m trying to find a way to cut down the amount of code I use to load the tiles my current code to load 3 rows of 9 each. Any ideas on hoe i could cut down my code considerably? Code----------------------------------------------
Function load()
On Error Resume Next
Open App.Path & "\map.ini" For Input As #1

Input #1, tile1, tile2, tile3, tile4, tile5, tile6, tile7, tile8, tile9
Input #1, tile10, tile11, tile12, tile13, tile14, tile15, tile16, tile17, tile18
Input #1, tile19, tile20, tile21, tile22, tile23, tile24, tile25, tile26, tile27







Close #1

If tile1 = 1 Then Tiles1 = block1
If tile1 = 2 Then Tiles1 = block2
If tile1 = 3 Then Tiles1 = block3
If tile1 = 4 Then Tiles1 = block4

If tile2 = 1 Then tiles2 = block1
If tile2 = 2 Then tiles2 = block2
If tile2 = 3 Then tiles2 = block3
If tile2 = 4 Then tiles2 = block4

If tile3 = 1 Then tiles3 = block1
If tile3 = 2 Then tiles3 = block2
If tile3 = 3 Then tiles3 = block3
If tile3 = 4 Then tiles3 = block4

If tile4 = 1 Then tiles4 = block1
If tile4 = 2 Then tiles4 = block2
If tile4 = 3 Then tiles4 = block3
If tile4 = 4 Then tiles4 = block4

If tile5 = 1 Then tiles5 = block1
If tile5 = 2 Then tiles5 = block2
If tile5 = 3 Then tiles5 = block3
If tile5 = 4 Then tiles5 = block4

If tile6 = 1 Then tiles6 = block1
If tile6 = 2 Then tiles6 = block2
If tile6 = 3 Then tiles6 = block3
If tile6 = 4 Then tiles6 = block4

If tile7 = 1 Then tiles7 = block1
If tile7 = 2 Then tiles7 = block2
If tile7 = 3 Then tiles7 = block3
If tile7 = 4 Then tiles7 = block4

If tile8 = 1 Then tiles8 = block1
If tile8 = 2 Then tiles8 = block2
If tile8 = 3 Then tiles8 = block3
If tile8 = 4 Then tiles8 = block4

If tile9 = 1 Then tiles9 = block1
If tile9 = 2 Then tiles9 = block2
If tile9 = 3 Then tiles9 = block3
If tile9 = 4 Then tiles9 = block4

If tile10 = 1 Then tiles10 = block1
If tile10 = 2 Then tiles10 = block2
If tile10 = 3 Then tiles10 = block3
If tile10 = 4 Then tiles10 = block4

If tile11 = 1 Then tiles11 = block1
If tile11 = 2 Then tiles11 = block2
If tile11 = 3 Then tiles11 = block3
If tile11 = 4 Then tiles11 = block4

If tile12 = 1 Then tiles12 = block1
If tile12 = 2 Then tiles12 = block2
If tile12 = 3 Then tiles12 = block3
If tile12 = 4 Then tiles12 = block4

If tile13 = 1 Then tiles13 = block1
If tile13 = 2 Then tiles13 = block2
If tile13 = 3 Then tiles13 = block3
If tile13 = 4 Then tiles13 = block4

If tile14 = 1 Then tiles14 = block1
If tile14 = 2 Then tiles14 = block2
If tile14 = 3 Then tiles14 = block3
If tile14 = 4 Then tiles14 = block4

If tile15 = 1 Then tiles15 = block1
If tile15 = 2 Then tiles15 = block2
If tile15 = 3 Then tiles15 = block3
If tile15 = 4 Then tiles15 = block4

If tile16 = 1 Then tiles16 = block1
If tile16 = 2 Then tiles16 = block2
If tile16 = 3 Then tiles16 = block3
If tile16 = 4 Then tiles16 = block4

If tile17 = 1 Then tiles17 = block1
If tile17 = 2 Then tiles17 = block2
If tile17 = 3 Then tiles17 = block3
If tile17 = 4 Then tiles17 = block4

If tile18 = 1 Then tiles18 = block1
If tile18 = 2 Then tiles18 = block2
If tile18 = 3 Then tiles18 = block3
If tile18 = 4 Then tiles18 = block4


If tile19 = 1 Then tiles19 = block1
If tile19 = 2 Then tiles19 = block2
If tile19 = 3 Then tiles19 = block3
If tile19 = 4 Then tiles19 = block4

If tile20 = 1 Then tiles20 = block1
If tile20 = 2 Then tiles20 = block2
If tile20 = 3 Then tiles20 = block3
If tile20 = 4 Then tiles20 = block4

If tile21 = 1 Then tiles21 = block1
If tile21 = 2 Then tiles21 = block2
If tile21 = 3 Then tiles21 = block3
If tile21 = 4 Then tiles21 = block4

If tile22 = 1 Then tiles22 = block1
If tile22 = 2 Then tiles22 = block2
If tile22 = 3 Then tiles22 = block3
If tile22 = 4 Then tiles22 = block4

If tile23 = 1 Then tiles23 = block1
If tile23 = 2 Then tiles23 = block2
If tile23 = 3 Then tiles23 = block3
If tile23 = 4 Then tiles23 = block4

If tile24 = 1 Then tiles24 = block1
If tile24 = 2 Then tiles24 = block2
If tile24 = 3 Then tiles24 = block3
If tile24 = 4 Then tiles24 = block4

If tile25 = 1 Then tiles25 = block1
If tile25 = 2 Then tiles25 = block2
If tile25 = 3 Then tiles25 = block3
If tile25 = 4 Then tiles25 = block4

If tile26 = 1 Then tiles26 = block1
If tile26 = 2 Then tiles26 = block2
If tile26 = 3 Then tiles26 = block3
If tile26 = 4 Then tiles26 = block4

If tile27 = 1 Then tiles27 = block1
If tile27 = 2 Then tiles27 = block2
If tile27 = 3 Then tiles27 = block3
If tile27 = 4 Then tiles27 = block4 
[Edited by - Life unlived on March 15, 2008 6:58:49 PM]
"But I don't want to go among mad people," Alice remarked. "Oh, you can't help that," said the Cat: "We're all mad here. I'm mad. You're mad." "How do you know I'm mad?" said Alice. "You must be," said the Cat, "otherwise you wouldn't have come here."
jpetrie
jpetrie
You might want to look into the concept of a "loop." Ask Google about Visual Basic loops.

EDIT: Oh, and arrays too.

[Edited by - jpetrie on March 9, 2008 10:38:11 PM]
Life unlived
Life unlived
I believe that I have a Halfway-Decent way of shortening my code. My idea is to ,instead of repeating lines of very similar code using a variable to change the words from tiles1 ,tiles2 and so on. My new code is shown below and ive googled and cant find why it wont work. I keep getting a strange error.

The error reads

Function call on left side of assignment ,must return variant or object. Please help me debug.



Function load2()On Error Resume NextOpen App.Path & "\map.ini" For Input As #1Input #1, tile1, tile2, tile3, tile4, tile5, tile6, tile7, tile8, tile9Input #1, tile10, tile11, tile12, tile13, tile14, tile15, tile16, tile17, tile18Input #1, tile19, tile20, tile21, tile22, tile23, tile24, tile25, tile26, tile27Input #1, tile28, tile29, tile30, tile31, tile32, tile33, tile34, tile35, tile36Input #1, tile37, tile38, tile39, tile40, tile41, tile42, tile43, tile44, tile45Input #1, tile46, tile47, tile48, tile49, tile50, tile51, tile52, tile53, tile54Input #1, tile55, tile56, tile57, tile58, tile59, tile60, tile61, tile62, tile63Close #110 If Val("tile" & Str$(n)) = 1 Then Val("tiles" & Str$(n)) = block1If Val("tile" & Str$(n)) = 2 Then Val("tiles" & Str$(n)) = block2If Val("tile" & Str$(n)) = 3 Then Val("tiles" & Str$(n)) = block3If Val("tile" & Str$(n)) = 4 Then Val("tiles" & Str$(n)) = block4n = n + 1GoTo 10End Function



[Edited by - Life unlived on March 16, 2008 3:39:51 PM]
"But I don't want to go among mad people," Alice remarked. "Oh, you can't help that," said the Cat: "We're all mad here. I'm mad. You're mad." "How do you know I'm mad?" said Alice. "You must be," said the Cat, "otherwise you wouldn't have come here."
Supaflyfrank
Supaflyfrank
I am not familar with vb that much but by looking at your code and a quick google search I believe the problem is your usage of the function Val and then your assigning it a value. Since Val returns numerical values there is no way you can assign it a value. Its as if you are assigning 3 = 6. Notice how its not in any kind of conditional statement such as IF so a comparison isnt being called but an assignment is. Long story short

Val("tiles" & Str$(n)) = block4

should be

block4 = Val("tiles" & Str$(n))
Life unlived
Life unlived
Function load2()if n => 60 then goto 20On Error Resume NextOpen App.Path & "\map.ini" For Input As #1Input #1, tile1, tile2, tile3, tile4, tile5, tile6, tile7, tile8, tile9Input #1, tile10, tile11, tile12, tile13, tile14, tile15, tile16, tile17, tile18Input #1, tile19, tile20, tile21, tile22, tile23, tile24, tile25, tile26, tile27Input #1, tile28, tile29, tile30, tile31, tile32, tile33, tile34, tile35, tile36Input #1, tile37, tile38, tile39, tile40, tile41, tile42, tile43, tile44, tile45Input #1, tile46, tile47, tile48, tile49, tile50, tile51, tile52, tile53, tile54Input #1, tile55, tile56, tile57, tile58, tile59, tile60, tile61, tile62, tile63Close #110 If val("tile" & Str$(n)) = 1 Then block1 = val("tiles" & Str$(n))If val("tile" & Str$(n)) = 2 Then block2 = val("tiles" & Str$(n))If val("tile" & Str$(n)) = 3 Then block3 = val("tiles" & Str$(n))If val("tile" & Str$(n)) = 4 Then block4 = val("tiles" & Str$(n))n = n + 1GoTo 1020 endEnd Function



I thought about your suggestion and googled it. It made sense so I decided to input it into my code. It did successfully rid me of the error but it still is not giving me the results I was hoping for , Nothing happens. Tile1 through tile60 are the variables that it loads from the map.INI file , tiles1 through tiles60 are the tiles' picture boxes, and block1 through block4 are the terrain graphics each number represents.

[Edited by - Life unlived on March 16, 2008 3:24:30 PM]
"But I don't want to go among mad people," Alice remarked. "Oh, you can't help that," said the Cat: "We're all mad here. I'm mad. You're mad." "How do you know I'm mad?" said Alice. "You must be," said the Cat, "otherwise you wouldn't have come here."
Supaflyfrank
Supaflyfrank
First you want to in close your code in between

!source lang="vb"]

!/source]

(replace ! with [ had to do that to display the code)
are making a loop the hard and incorrect way.
Here is the link to actually see what I am talking about.
Displaying Code
Check towards the bottom.


Also you want is a for loop.
For Loop Info

Also from what I read you are using the INPUT function incorrectly. You are not actually reading in anything.

LINE INPUT #1,tile1

Line Input Info

Look at this listing to find all the various ways to read data since it setting it up is dependant on how your text files is set up.

Also you should use arrays.
Arrays info

Basically with for loops and arrays you can reduce your code alot. I use c++ but you can easily see the difference in code size.
Function load2()On Error Resume Next' create some kind of array for the tiles with the size of it being 63Open App.Path & "\map.ini" For Input As #1	 For  n=1 to 63       LINE INPUT #1,tile(n) Next  For n = 1 to 63 NextClose #1



I ignored the last portion of your code:

If Val("tile" & Str$(n)) = 1 Then Val("tiles" & Str$(n)) = block1If Val("tile" & Str$(n)) = 2 Then Val("tiles" & Str$(n)) = block2If Val("tile" & Str$(n)) = 3 Then Val("tiles" & Str$(n)) = block3If Val("tile" & Str$(n)) = 4 Then Val("tiles" & Str$(n)) = block4

This doesnt make sense because one in your incorrect loop you are setting the values for block(1,2,3,4) but since your looping the values have a possibility to be overwritten ever time it loops. Also the Val function from my understanding convert the string to an integer. Therefore "titles" & Str$(n) as a parameter to that function doesn't make sense.
Info on Val

So all things consider I am done helping you with your code. Not trying to be mean but you need to get used to looking things up yourself. I used to get upset when people tell me this but the better you get a solving problems yourself the better as a programmer you will be and the quicker you will solve problems. If your wondering how you would solve this kind of problem simply keep on re reading information on the functions you are using to get a better understanding on what they can and can not do. From looking at some of the errors you have made I don't think a map loader is a good project. But if you stick it through I guess anything is possible or you will simply give up. Look at all the links I provided. With all those changes you can reduce your line of code from about 40 or what ever + (i didn't count) to probably around 15. Check out the links throughly because from what I saw those webpages were parts of a VB tutorial that can teach you alot of things. Also your using a ini file extension for map. Your using it incorrecly. INI files are usually used for application settings where you can retrieve values by specifying the name of the Value. I am not explaining it correctly so for example
////////////example.ini//////////
[players]
name = frankie

//////////////////////////////////

Program
But looking in the group category it calls name and frankie is returned.
Anyway I am not that great with explaining so this would serve you better
INI file info
Unless you use the correct functions for ini files you are not really using it to its potential but doing it your way could work but specifying ini or txt wouldnt really make a difference.
Anyway good luck.
Life unlived
Life unlived
Supafly about your comment on my input code I know 100% that it does work because I have used the same code for several different projects. Other than that i found quite a bit of your' tips helpful. Also about a tile map loader not being the project for me, I already have one working 100% perfectly but i know it is coded in a very inefficient and problematic way. I am simply trying to find a way to cut down the code. The last portion of code which you ignored is supposed to mean if tile(n)=1 then tiles(n)=Block1
and so on for the other lines of code.
"But I don't want to go among mad people," Alice remarked. "Oh, you can't help that," said the Cat: "We're all mad here. I'm mad. You're mad." "How do you know I'm mad?" said Alice. "You must be," said the Cat, "otherwise you wouldn't have come here."
Life unlived
Life unlived
I’m sorry for my past arrogance. I did as Jpetrie suggested and studied up on rays and loops and got a very good solution to my problem. Its working 100% so thank you tons, Jpetrie.

Function load()Dim tile(63)On Error Resume NextOpen App.Path & "\map.cac" For Input As #1Input #1, tile(1), tile(2), tile(3), tile(4), tile(5), tile(6), tile(7), tile(8), tile(9)Input #1, tile(10), tile(11), tile(12), tile(13), tile(14), tile(15), tile(16), tile(17), tile(18)Input #1, tile(19), tile(20), tile(21), tile(22), tile(23), tile(24), tile(25), tile(26), tile(27)Input #1, tile(28), tile(29), tile(30), tile(31), tile(32), tile(33), tile(34), tile(35), tile(36)Input #1, tile(37), tile(38), tile(39), tile(40), tile(41), tile(42), tile(43), tile(44), tile(45)Input #1, tile(46), tile(47), tile(48), tile(49), tile(50), tile(51), tile(52), tile(53), tile(54)Input #1, tile(55), tile(56), tile(57), tile(58), tile(59), tile(60), tile(61), tile(62), tile(63)Close #1For I = 1 To 63If tile(I) = 1 Then tiles(I).Picture = block1If tile(I) = 2 Then tiles(I).Picture = block2If tile(I) = 3 Then tiles(I).Picture = block3If tile(I) = 4 Then tiles(I).Picture = block4If tile(I) = 5 Then tiles(I).Picture = block5Next IEnd Function
"But I don't want to go among mad people," Alice remarked. "Oh, you can't help that," said the Cat: "We're all mad here. I'm mad. You're mad." "How do you know I'm mad?" said Alice. "You must be," said the Cat, "otherwise you wouldn't have come here."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.