Newbie questions!

BlitzMax Forums/BlitzMax Beginners Area/Newbie questions!

kronholm(Posted 2006) [#1]
Hey all, I've got a couple, probably easy, questions. I'd be really happy if someone could help me answer them! :)


1) Getting one item from a list:
Say I have a list (balllist) with a lot of ball types (tball) in it. How can I, instead of looping through the entire list, get data from one item in the list? For instance getting ball.speed from one of the balls in balllist.

2) Scope within functions:
I have a lot of classes (types) for stuff such as ball, paddle, powerup etc. When I want to make a new ball for instance, I do 'Global ball:tball = New tball'. If I do this in a function though, for instance 'Function createball()', the ball variable is not seen anywhere outside the function. I realize this is working as intended, but how do I deal with it?


CS_TBL(Posted 2006) [#2]
2)

Function Createball:TBall(args)
  local a:TBall=new TBall
  ' initialize fields
  ' ..
  ' ..
  ' ..
  Return a
End Function

Local bla:TBall=Createball(args)



kronholm(Posted 2006) [#3]
Wow, thanks CS_TBL, just what I was looking for :D

Any ideas on #1?


kronholm(Posted 2006) [#4]
Actually I just tested your suggestions and it's not what I was looking for. You're still kind of creating the ball outside a function. I wish to create a ball inside a function, but want it's data and fields, eg. ball.fieldname, accessible everywhere outside the function. I want it truly global.

To paint a clearer picture:

2) I wish to have a function called gamestart() which does the following: creates a ball, creates a paddle and creates a powerup - which are accessible outside the function.
3) How do I check if a variable is set?


Scott Shaver(Posted 2006) [#5]
just make the variables global.

Global player:TBall = Createball()


as for the list thing

x:TBall = TBall(mylist.ValueAtIndex(23))


Diablo(Posted 2006) [#6]
2)
global ball:tball
global paddel:tpaddel
global powerups:tpowerup[]


function gamestart()

ball = tball.create()
paddel = tpaddel.create()
powerups = createpowerups() ' return an array

end function

1) look up LINKS(TLINK) and type casting.

i cant help much i'm not at MY computer.


CS_TBL(Posted 2006) [#7]
kronholm: Maybe this next suggestion is way ahead your current interests yet, but why everything global?

If ball, paddle, power etc. need to be global "within your game", I'd rather put them all in a type, a "gametype" and make that single instance a 'global' then, actually a Local in the mainloop is enough.. I'm as usual a bit worried about too many globals.. :P

Type TGame
  Field Ball:TBall
  Field Paddle:TPaddle
  Field Powerup:TPowerup

  Method New()
    Ball=CreateBall()
    Paddle=CreatePaddle()
    Powerup=CreatePowerup()
  End Method

  Method CreateBall:TBall()
  End Method

  Method CreatePaddle:TPaddle()
  End Method

  Method CreatePowerup:TPowerup()
  End Method

  Method GameAI()
  End Method

  Method UpdateGame()
  End Method

..

..

..

etc.

End Type

local MyGame:TGame=New TGame


Sofar you might think 'hey, that's 1000x more code than just typing "Global". The benefits will come when you'd do a 2-player game with 2 seperate windows orso.

Local player:TGame[2]
player[0]=New TGame
player[1]=New TGame


tadaaa.. 2 games at the same time!

Ahwell, let's not rush things.. ignore it ^_^


kronholm(Posted 2006) [#8]
Thanks TBL but it's still not what I'm looking for :)

I want to be able to create a ball within a function called newgame(), and have that ball be referenced anywhere outside of the scope of that function.

Can anyone help me with my other questions? How to check if a variable is set/defined, and how to get a reference to an item in a list assigned to a variable, but without having to loop through the list. The example above with the list worked, but once that item was removed from the list, the game would crash.


Dreamora(Posted 2006) [#9]
You can not get data from a linked list without looping.
Use TMap instead and unique names for the object, then you can get the object by name.

On the "reference outside everywhere": Set the ball global to the type instead of field. now you can use type.ball to reference it after you used the newgame function/method (within the type)


VP(Posted 2006) [#10]
#1) if you need to access one item from a linked list, you've done something wrong at the design stage.

Take 5 steps back, drink a hot beverage of your choice, stare out of the window for 10 minutes and then start again, but with pencil and paper first!

I fell into the same problem myself and couldn't see a way past it, until I did the above.


kronholm(Posted 2006) [#11]
Dreamora:
Can you perhaps elaborate with an example? I don't understand what you mean by setting the ball global to the type instead of field..

Vinylpusher:
When I'm making all my balls, I'm putting them in a balllist. At some point in time I need to get some data, e.g. ball.speed and ball.x and ball.y from a ball in the list, it doesn't really matter which. This is where I wish to avoid looping through everything. I've looked at it plenty from afar and I can't really think of a solution :(


Dreamora(Posted 2006) [#12]
Type Instance
global ball:TBall
...
end type

You can access it from anywhere using Instance.ball
This ball is the same to all object of type Instance (so even with 10 objects, you have only 1 ball)