News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Question about data and arrays

Started by esper, December 10, 2007, 03:55:50 AM

Previous topic - Next topic

esper

Hi. Sorry to already be asking another question. This one is easier and doeasn't require downloading my program to get it to work  :P

At the beginning of my game, I randomly generate 25-75 stars and then give them random names from a list of 201 possibles. I'm trying to store already used names in an array so no two stars get the same name. Unfortunately, all is not working according to plan. Here's my code...

for i=1 to starstotal

do                                            ;keep looping until you find a star name that has not already been used
namer=rnd(200)                  ;choose a random number from 0-200 (201 possible)
namer$=str$(namer)            ;assign it a string variable so you can use it to search the database
restore namer$                   ;go to the number in the database that was randomly selected
tempname$=readdata$()      ;read that number
tempname$=readdata$()      ;read the name of the star that comes immediately after it and store it in a variable
if findarraycell(alreadyused$(),1,1,getarraydimensions(alreadyused$()),tempname$)=-1     ;look for that star name in the already-used database
sun(i).name$=tempname$   ;if you don't find it, name the sun after the data you read earlier
a=getfreecell(alreadyused$())   ;find a free cell in the array
alreadyused$(a)=tempname$    ;store the star data in the already-used array
exitdo                                  ;exit the loop
endif
loop

next i


...And the data reads...

data "0","Starname","1","Starname1","2","Starname2","3","Starname3"

etcetera, etcetera, ad infinitum...

So why isn't my code keeping star names from being repeated?

kevin


    It's a little difficult to test without providing a snippet we can quickly cut and paste and experiment with.  However I don't think you want to the search the dimensions (aka getarraydimensions(alreadyused$()))   as this will always return 1 on a 1D array.    I think what you're after is GetArrayElements()  here.


Big C.

#2
ok esper,

I hope I 've understand your problem correctly...

here is my way...


; PROJECT : Project1
; AUTHOR  : Big C.
; CREATED : 11.12.2007
; ---------------------------------------------------------------------

;Get the max of Stars in a given range 
StarsTotal = RndRange(1,20)

Print "Star total: " + Str$(StarsTotal)

Type TSun
Name$
xPos
yPos
EndType

Dim Sun(StarsTotal) As TSun


;Get the quantity of elements of an Datasection (more dynamically) and substract one (because we start at 0)
n = GetDataQuantity()-1

Print " quantity-1 = " + Str$( n)

;Make a temp array to hold the status of an used Starname
Dim StarnameTMP(n)

;Here we populate our Suns with the random names
For i=1 To StarsTotal
Do
namer=Rnd(n)
If StarnameTMP(namer) <> 1                  ;here we check the temp array if the random name is NOT used then
Restore namer                              ;get the random name of the DataSection
Sun(i).Name$ = ReadData$()           ;fit the sun variable with the random name and
StarnameTMP(namer) = 1               ;set the status USED in the temp array
ExitDo                                         
EndIf
loop
Next i

;Give the Memory of the temp array free
UnDim StarnameTMP()


For i=1 To StarsTotal
Print "Star " + Str$(i) + " " + Sun(i).Name$
Next

Sync

Waitkey
End


;Datasection with Names. Optimation: This part could outsourced to an flatfile or whatever
Data "Starname","Starname1","Starname2","Starname3","Starname4","Starname5"
Data "Starname6","Starname7","Starname8","Starname9","Starname10","Starname11"
Data "Starname12","Starname13","Starname14","Starname15","Starname16","Starname17"
Data "Starname18","Starname19","Starname20","Starname21","Starname22","Starname23"



Big C.

esper

#3
Thanks, Big C. There's no way I can be sure, as I'd have to test it an infinite amount of times to see if it's repeating names or not, but it doesn't seem to be.

EDIT: While I'm at it, I don't want to be a jerk and cover the main page with new topics, so let me throw this in here: why is "spritesoverlap" not following the camera? After the camera has moved, it still thinks my mouse sprite is overlapping a planet after the camera has moved and the planet is no longer there.


kevin


Quote
EDIT: While I'm at it, I don't want to be a jerk and cover the main page with new topics, so let me throw this in here: why is "spritesoverlap" not following the camera? After the camera has moved, it still thinks my mouse sprite is overlapping a planet after the camera has moved and the planet is no longer there.

If neither sprite has been repositioned since the last update, then of course they'l still be colliding. 



esper

Hmmm... well, see... the mouse cursor has a built-in sprite that follows it around and acts as a "collision detector" to see if the player is clicking the planet, so the mouse sprite does move. The camera moves away from the planet, and you can fly forever away in any direction, but then when you go to scan something, it will only work when you pass the mouse cursor over the place where the planet that was there at the beginning of the game (earth), and it will only realize that earth is there, even if it is a million light-years away. Is there any way I can get this to work.

kevin


   Sprite collisions occur in world space.   The camera's position or direction for that matter are irrelevant to sprite collision.   

  I.e.


       Camera  (viewer)
                  |
                  \/   
<--Objects in World Space ------------------------------------------------------>

      [Sprite A]          [Sprite B]

                          [Sprite C]
<--------------------------------------------------------------------------------->



    If you compare Sprite A to B   this frame or the next frame,  they're collision status would be the same,  irrespective if how the viewer (the camera) moves.   If the objects move between frames then they impact status could change.

     However, if you're repositioning objects to scene from world space to scene(or screen) space in your game.  Then obviously if the camera moves, then scene objects have to be made relative to the viewer.  Without which you'll get overlaps between sprites that aren't repositioned accordingly (lagged between frames).  This is not a PB issue is the logical issue in your program.


esper

Hmm. I was hoping that wasn't the case. My game runs very smoothly, but I tried incorporating CreateWorld and everything slowed down to a crawl, not to mention the fact that the World commands are some of the least well documented. I guess it's off to study the manual again.