Question About Integer & Float Variables.

Started by Makeii Runoru, June 18, 2008, 08:34:09 PM

Previous topic - Next topic

Makeii Runoru

I've always tried to get into programming, but usually I have way too many questions to ask. Every little bit of the programming always messes me up because I always want to know why it's done the way it is.

So, I'm gonna try to post a really beginnerish question:

(1.) In the "Turret" tutorial, the following code is included:

; Create two variables Set
GunX#=100


GunX#: does the # need to be included, or can it be left out? I think that this indicates that GunX is an integer. Though, we can define GunX "As Integer" anywhere else, right?

I know this seems newbish to ask, but there are some things about programming that always stick out as jargon even though it's the simplest of things.
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

kevin

#1
QuoteGunX#: does the # need to be included, or can it be left out? I think that this indicates that GunX is an integer

    If we want the GunX variable to hold only Integer values (whole numbers), then No, then # symbol is not needed.


    For example..

PlayBASIC Code: [Select]
; Create & Assign the Integer Variable GunX the Value 400
GunX = 400

; Create & Assign the Integer Variable GunX the Value 400
GunY = 300

; Display the contents of the GunX and GunY integer variables
Print GunX
Print GunY

; Tell PB to display the screen
Sync

; Wait for a key press before ending
Waitkey




   If you cut and paste the sample code into PlayBASIC and run it,  PlayBASIC will display 400 and then 300 in the top left section of the screen.    Notice that these values are whole numbers.  There's no fractional part of them.  

  While generally we can happily use with Integer variables exclusively in our programs.   There are times where we'll need some more accuracy though, so  in those cases we can use Floating Point variables.

  Float variables require the "#" postfix symbol.   Float variable can represent values with a fractional part.   For example, the number 1.23 is a floating point number.      


  In the following example,  we'll define two variables again.  But this time first is a integer variable and then second is float.


PlayBASIC Code: [Select]
; Create & Assign the Integer Variable Score with the Value 20000
Score = 20000

; Create & Assign the Float Variable Gravity# with the Value 9.8
Gravity# = 9.8

; Display the contents of the variables
Print Score
Print Gravity#

; Tell PB to display the screen
Sync

; Wait for a key press before ending
Waitkey



  If we copy and paste this into PlayBasic and run it.  PB will display the contents of the two variables to the screen.  I.e.  20000 and 9.8 bellow it.


  You'll notice that every time we use our floating point variable (Gravity#), we refer to it with it's "#" post fix symbol.    This distinguishes to PlayBasic (and you!) that we want to access the  Gravity variable of type float.


  Ok, so what happens if you forget, or drop the # symbol ?   - Lets have a look..


PlayBASIC Code: [Select]
; Create & Assign the Integer Variable Score with the Value 20000
Score = 20000

; Create & Assign the Float Variable Gravity# with the Value 9.8
Gravity# = 9.8

; Display the contents of the variables
Print Score
Print Gravity ; (notice the # is missing !)

; Tell PB to display the screen
Sync

; Wait for a key press before ending
Waitkey



  If we copy and paste this into PlayBasic and run it.   PB will display the contents of the two variables to the screen, but they may be want you're expecting.    I.e.  20000 and 0 bellow it.


 Why ?

     In PlayBasic and many versions of BASIC language,  variables of different types (integer/float and string) can all share the same name.  So us programmers and PlayBasic distinguishes between the different instances via the post fix symbols.

       Float variables use the "#" character as a postfix
       Strings Variables use the "$" character.

        Integers don't require a postfix symbol, as they are the default variable type.  

     Armed with this knowledge,  we can now decipher why the previous example displayed the 0 for our Gravity variable.  Rather than the 9.8 value we might have expected.

     This occurs because Gravity and Gravity#  are two completely different variables.   While they share the same name,  they're of a different type.


       Gravity is an Integer Variable

       Gravity# is a Float Variable
     
   We can demonstrate this with the following.

 
PlayBASIC Code: [Select]
; Create & Assign the Integer Variable Gravity with the Value 50
Gravity = 50

; Create & Assign the Float Variable Gravity# with the Value 9.8
Gravity# = 9.8

; Display the contents of the variables
Print Gravity ; (This will display the Integer version of gravity)
Print Gravity# ; (This will display the Float version of gravity)


; Tell PB to display the screen
Sync

; Wait for a key press before ending
Waitkey



  If we copy and paste this into PlayBasic and run it.   PB will display the contents of the two variables to the screen, but they may be want you're expecting.    I.e.  50 and 9.8 bellow it.

  If you look closely, the two variables use the same name Gravity but they're clearly two different variables.


 So,  to quickly wrap this up,  

     Integer Variables  are for whole numbers.  And don't require a post fix.  

    Float Variables are for fractional numbers and do require the post fix.


     if you, mess up while programming and either forget to add # symbol to a float,  or you add an # symbol to a integer variable,  then PB will return a result.  But it's unlikely to be the one you want.



 
  Some of this ground is covered here.     Variables & Math Questions



QuoteI know this seems newbish to ask


   Nah, not at all,  this is exact type of question this forum is here for.  So fire away!




Quotebut there are some things about programming that always stick out as jargon even though it's the simplest of things.

    Yes, sadly there's a lot of jargon in programming.  So it's not so easy to weed out completely.    
   




   



Makeii Runoru

#2
This is probably one of the most detailed answers to a question I have ever seen. Thanks, Kevin! This makes so much more sense to me now, and the # postfix was completely different than I thought it would be. So, if I assign a variable "Something$," then this means that I can assign the variable to a string as well.

But now, am I able to assign a varible without using the $ postfix, or does a standard variable with no postfix be considered an integer by default? Nevermind. I looked at the link you gave me in your post and it answered my question. Thanks again. : )
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

kevin

#3
     Variables are a fundamental programming principal,  while they're no doubt confusing as hell initially,  we don't want to over think this too much either.   So I highly recommended experimenting with any code snippet you see.    Practice is what will help you turn the theory mumbo jumbo into working knowledge.
   

   
QuoteSo, if I assign a variable "Something$," then this means that I can assign the variable to a string as well.  

      If we have a string variable it can really only be used in conjunction with other strings.    You can think of each string variable as way for us to hold  words and sentences in the computers memory.    So there not for numerical calculations.   They're purely for working for text.  Such as like displaying a the name of your game for example.

     Eg.

 
PlayBASIC Code: [Select]
    ; Create the GameName$ string variable  and store the string "Fire Bomber" into it
GameName$="Fire Bomber"

; Tell PlayBASIC to PRINT the contents o the Game Name$ Variable to the screen.
Print GameName$

; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
Waitkey


     

    If we cut and paste this into PlayBASIC, then press F5 to run it.   PlayBASIC will display Fire Bomber in the top left hand corner of the display.    

  In this example we'll create two string variables and display them.

PlayBASIC Code: [Select]
 ; Create the GameName$ string variable and store the string "Fire Bomber" in it
GameName$="Fire Bomber"

; Create the AuthorName$ string variable and store the string "Kevin Picone" in it
AuthorName$="Kevin Picone"

; Tell PlayBASIC to PRINT the contents of both variables to the screen
Print GameName$
Print AuthorName$


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
Waitkey




    If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display Fire Bomber in the top left hand corner and Kevin Picone under it.


    Can can combine strings using the addition operator.   In this example we'll use the add to join our GameName$ and AuthorName$ strings into a new string.  

PlayBASIC Code: [Select]
 ; Create the GameName$ string variable and store the string "Fire Bomber" in it
GameName$="Fire Bomber"

; Create the AuthorName$ string variable and store the string "Kevin Picone" in it
AuthorName$="Kevin Picone"

; Create the GameAndAuthorName$ string variable and store text from GameName$ and AuthorName$ in it
GameAndAuthorName$=GameName$+AuthorName$


; Tell PlayBASIC to PRINT the contents of both variables to the screen
Print GameName$
Print AuthorName$
print GameAndAuthorName$


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
Waitkey



    If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display Fire Bomber, then Kevin Picone and Fire BomberKevin Picone under it.

    Notice how when we add strings together, the resulting string is the combined verbatim.   Which in this case makes it the words Bomber and Kevin run into each other.   But this is easily solved.  

    To separate the text from GameName$ and AuthorName$ when we add them tolgether,  we could add another string in between them.    Something like this would do it.


PlayBASIC Code: [Select]
 ; Create the GameName$ string variable and store the string "Fire Bomber" in it
GameName$="Fire Bomber"

; Create the AuthorName$ string variable and store the string "Kevin Picone" in it
AuthorName$="Kevin Picone"

; Create the GameAndAuthorName$ string variable and store the combined text
; from the GameName$ variable with the string " By "+ and AuthorName$ variable
GameAndAuthorName$=GameName$+" By "+ AuthorName$


; Tell PlayBASIC to PRINT the contents of both variables to the screen
Print GameName$
Print AuthorName$
print GameAndAuthorName$


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
Waitkey





  If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display Fire Bomber, then Kevin Picone and Fire Bomber By Kevin Picone under it.

  If we're only going to display the GameName$ and the AuthorName$ combined, then we could away with the GameAndAuthorName$ variable.   And tell PlayBASIC to Print the result  like this


PlayBASIC Code: [Select]
 ; Create the GameName$ string variable and store the string "Fire Bomber" in it
GameName$="Fire Bomber"

; Create the AuthorName$ string variable and store the string "Kevin Picone" in it
AuthorName$="Kevin Picone"


; Tell PlayBASIC to PRINT the contents GameName$ combined with this string " by "
; and Author name$
print GameName$+" By "+ AuthorName$


; Tell PlayBasic to show the screen to us and wait for a key press
Sync
Waitkey




  If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display Fire Bomber By Kevin Picone under it.

  Previously I mentioned that we can only ADD strings together with other strings.   But what if we wanted combine a string and numerical variable together ?  -  For this we just the function STR$()  


PlayBASIC Code: [Select]
 ; Declare the integer variable called MyNumber
MyNumber = 557

; Tell PlayBASIC to PRINT the
print "MyVariable is currently = " + Str$( MyNumber )


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
Waitkey



   If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display MyVariable is currently = 557.

   So Str$() is used to translate numerical variable or values into the characters (the digits) the represent this value.

   To expand upon that a little more,  the entire point of variables is that we can change then while the program is running.   So lets say we wanted to display a count down to lift off message on the screen.

   Something like

   "10 seconds to lift off"
   "9 seconds to lift off"
   "8 seconds to lift off"
    etc
    "blast off!"



PlayBASIC Code: [Select]
; Manually display all the messages
print "10 seconds to lift off"
print "9 seconds to lift off"
print "8 seconds to lift off"
print "7 seconds to lift off"
print "6 seconds to lift off"
print "5 seconds to lift off"
print "4 seconds to lift off"
print "3 seconds to lift off"
print "2 seconds to lift off"
print "1 seconds to lift off"
print "Blast off"

; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
WaitKey



   If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display all those messages verbatim.   The problem is, this solution isn't very dynamic.  What if we wanted to count down from 100  or a 1000 perhaps ?... That's a lot of typing and a lot wasted time and energy.  

  What we could do,  is make PlayBasic loop 10 times (or however many required) and display the count message each time.    For this we'll use a the FOR / NEXT looping commands.   If you're not sure about FOR/NEXT loops then I recommended digging through the LOOPS tutorial in the PlayBasic Help.   You'll find under the ABOUT section.    It covers pretty much everything you've ever wanted to  know about loops.  

   In a nut shell a FOR / NEXT  loop lets us tell PlayBASIC to run a section of code more than once.  There's no limit upon the number of times,  so you can run the section 5, 10, 5000,  a million times if you need.    To set up a loop we have to give 3 basic things.  A variable to use as the current counter,  the starting value and the ending value of the loop.

   eg.  
 
PlayBASIC Code: [Select]
            For Counter =  1 to 10
; Your program code goes in here.. You can have as much code as you like
Print Counter
Next
Sync
WaitKey




  ok,  lets get back to our example about.   In this version of our lift off we'll use a FOR/NEXT loop to display the count down message  

PlayBASIC Code: [Select]
   ; Create a FOR/NEXT loop that will loop 10 times (from 1 to 10)

For CountDown = 1 to 10

; Tell PlayBASIC to convert CountDown integer varaibles to a String
; then message the characters with the string " seconds to lift off"
print str$(CountDown) +" seconds to lift off"

next

; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
WaitKey





   If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display  our 10 messages, but there's a problem, since the loop is counter from 1 up to 10, then the outputted messages will be the wrong order.    

   To fix this,  we need to tell our FOR/NEXT loop to count backwards by one's.  We do this adding the STEP keyboard, which is followed by the amount to step.  Since we wish to count backwards,  we also swap the Start and End values around.  So our for next loop will look a bit like this.

 eg.  
 
PlayBASIC Code: [Select]
            For Counter =  10 to 1 step -1
; Your program code goes in here.. You can have as much code as you like
Print Counter
Next
Sync
WaitKey




  So if we rearrange our previous lift example,  we get something like this..

 
PlayBASIC Code: [Select]
   ; Create a FOR/NEXT loop that will loop backwards 10 times, starting from 10
; and counting down to 1

For CountDown = 10 to 1 step -1

; Tell PlayBASIC to convert CountDown integer variables to a String
; then message the characters with the string " seconds to lift off"
print str$(CountDown) +" seconds to lift off"

next

; Display the blast off message
Print "Blast Off!!"


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
WaitKey



  If we cut and paste this into PlayBASIC then press F5 to run it.   PlayBASIC will display  our 10 messages in the correct order.   This time we can change the length of our count down by simply changing the starting value in our For/Next loop.

If you've run of the previous lift off snippets, you'll notice they virtually appear instantly.   So it's not much of count down.  So  In this final variation we'll use the Wait command to force PlayBasic to wait 1 second each time we processes the loop.   I've also change the count down from 10 to 5.  So if you think about it, this example will take 5 second to complete.  Since the our for/next loop is running 5 times and each time PlayBASIC runs through the code, it'll wait 1 second.  


PlayBASIC Code: [Select]
; Create a FOR/NEXT loop that will loop backwards 5 times, starting from 5
; and counting down to 1

For CountDown = 5 to 1 step -1

; Tell PlayBASIC to convert CountDown integer variables to a String
; then message the characters with the string " seconds to lift off"
print str$(CountDown) +" seconds to lift off"

; Refresh the screen so we can see it
Sync

; Force PlayBASIC to wait 1 second (1000 milliseconds
Wait 1000

next

; Display the blast off message
Print "Blast Off!!"


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
WaitKey





   Is that the only way we could do it ?  - Nope,  we could also use a REPEAT / UNTIL or a WHILE / EndWhile loop
     

Repeat / Until version of the lift off program


PlayBASIC Code: [Select]
; Create the CountDown Variable and assign it the starting value
CountDown = 5

; Start of REPEAT / UNTIL loop section
Repeat

; Tell PlayBASIC to convert CountDown integer variables to a String
; then message the characters with the string " seconds to lift off"
print str$(CountDown) +" seconds to lift off"

; Refresh the screen so we can see it
Sync

; Force PlayBASIC to wait 1 second (1000 milliseconds in the conmputer ='s 1 second to us)
Wait 1000


; Subtract 1 from CountDown variable
CountDown = CountDown -1

until CountDown=0

; Display the blast off message
Print "Blast Off!!"


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
WaitKey





While / EndWhile version of the lift off program


PlayBASIC Code: [Select]
; Create the CountDown Variable and assign it the starting value
CountDown = 5

; Start of While / EndWhile oop section
While CountDown<>0

; Tell PlayBASIC to convert CountDown integer variables to a String
; then message the characters with the string " seconds to lift off"
print str$(CountDown) +" seconds to lift off"

; Refresh the screen so we can see it
Sync

; Force PlayBASIC to wait 1 second (1000 milliseconds in the conmputer ='s 1 second to us)
Wait 1000


; Subtract 1 from CountDown variable
CountDown = CountDown -1

EndWhile

; Display the blast off message
Print "Blast Off!!"


; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
WaitKey






 
More String Functions


    PlayBASIC has a list of string functions a mile long.  You can do stuff like searching for chrs within a string, replace fragments of a string,  and cut strings up into sections just to name a few.     Some of the most commonly used string functions are  LEN() LEFT$() and RIGHT$() and  MID$()

     Here's a bit of example.. I hope it helps...


PlayBASIC Code: [Select]
   ;  Create a string with all the charcter sof the alphabet in it.
AlphaBet$ = "abcdefghijklmnopqrstuvwxyz"


; Tell PB to display how long our AlphaBet$ string variable is. The Length
; is how many character are in this string.
print Len(alphaBet$)


; Tell PB to display the first 10 charcters of the AlphaBet$ string. For this
;we use the LEFT$() function.
Print Left$( Alphabet$, 10 )

; Tell PB to display the last 10 characters of the AlphaBet$ string. For this
;we use the RIGHT$() function.
Print Right$( Alphabet$, 10 )


; Tell PB to display the 5 characters starting at character 10 characters from
; the AlphaBet$ string. For this we use the MID$() function.
Print Mid$( Alphabet$, 10 , 5 )

; Use a FOR / NEXT loop to run through and display each individual character from the string
; For this we'll use the MID$() function again. This time we'll tell it to grab the
; a one character at the current counters position each time it loops.
For Counter = 1 to Len(AlphaBet$)
Print Mid$(AlphaBet$, Counter, 1)
next



; Tell PlayBASIC to show the screen to us and wait for a key press
Sync
WaitKey





 Part 7 - Everything You Wanted To Know About Strings (But Were Afraid To Ask)




Makeii Runoru

It seems that you really can't jump to conclusions on programming. since this is a game-developing language, you can assume that I would want to design a good game. The ideas are far, but the knowledge of how to make them is something anyone could lack.

Is there any other sort of method of learning I might be missing? This forum is a great resort, and the tutorials might come in handy if I wanted to get a basic idea on a particular type of programming. You also said that experimentation with snippets is something that will enhance my education on programming.

I've declared that anything I might know from prior engines or languages, or anything I've known in the past should be discarded for things I will now learn for PlayBasic.
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.

kevin


QuoteIt seems that you really can't jump to conclusions on programming. since this is a game-developing language, you can assume that I would want to design a good game. The ideas are far, but the knowledge of how to make them is something anyone could lack.

    Programming a game is like being the puppet master.  Forget features and languages for a minute as these are largely irrelevant,  as at the end of the day the character representations on screen would be completely lifeless without your program code to drive it.



QuoteIs there any other sort of method of learning I might be missing? This forum is a great resort, and the tutorials might come in handy if I wanted to get a basic idea on a particular type of programming.  You also said that experimentation with snippets is something that will enhance my education on programming.

    Theory is wonderful, but practice is the only real way to make the theory translate into workable knowledge.  It's cycle, we study a bit, try it out (learn from our mistakes), then repeat.    Soon concepts the seemed foreign initially, will be become more clear.   So making your way becomes easier.

    One way is to keep active in your own programming education.  It's not going to magically happen, if you don't make it it happen.  You can do this my setting yourself small generally simple objectives.   Some ideas of these might be,

   *  How do get my image on screen ?
   *  How can make this image move ?
   *  How can I control it with keyboard?.   etc.   How can i control it with the mouse/joystick .. 
   *  How can I get another image on screen.   How do i make it move.. How can I detect collisions ?



QuoteI've declared that anything I might know from prior engines or languages, or anything I've known in the past should be discarded for things I will now learn for PlayBasic.

      Focus is the key,  it doesn't matter what the language is.  The fundamentals (variables /loops/arrays/ comparisons) are pretty much the same.    The best analogy I can think of it, would be that BASIC is like the ENGLISH language.  While it's universal, there are differences between regional dialects.   So reading/learning about Dialect A,  will help when learning dialect B,  but there will be some differences between them.



Makeii Runoru

Small goals are key, then. Taking baby steps so that nothing important is overlooked. I'll definitely take this into consideration.

I have been doing some tinkering with some of the easier programming methods in Game Maker 7, but a lot of times doesn't give you the decent flexibility that Play Basic would. It was easier, though.

When I bought Play Basic, I saw a blank screen, with lines for where PB would initially type up the date and the title, and the default name of the user of the computer. It was very different, because I knew that I'd have to define sounds and graphics using external software. I actually have a hard time finding places to start, as if it was a sketchbook and I had my pencil in my hand, but I didn't know where to start drawing.

This topic should probably be moved to the Theories section or something, as this is starting to veer a little off topic. I should also spend more time in that section, because I know I have a bunch of ideas but don't know how to properly shape them in the form of a programming language.

Thanks, Kevin, for your help. It makes me feel that the 30+ dollars I spent on it is not to waste.
This signature is boring, and could be improved. However, the user of this signature doesn't need a fancy signature because he or she doesn't really care for one.