Page 1 of 1

put 'something" into variable

Posted: Tue Nov 19, 2013 1:55 pm
by chris25
put 0.01 into shipSpeedX

in the context of speed does this variable have a value of move 'x'th of a pixel per millisecond or something like it? What exactly is the number referring to?

Re: put 'something" into variable

Posted: Tue Nov 19, 2013 2:18 pm
by Mark
Hi Chris,

I don't understand your question. A value isn't store as a pixel, a word or a coordinate. In other words, LiveCode doesn't know data types, pretty much contrary to most other languages.

Whenever you store a value in a variable, you can simply consider the data as a chunk of data. Each time when the variable is accessed, LiveCode decides from the context of your syntax whether the variable is supposed to be a number. If it isn't a number, then LiveCode tried to execute the code using the data as string. If that's impossible, you get an execution error.

Do you have a particular reason to ask this?

Kind regards,

Mark

Re: put 'something" into variable

Posted: Tue Nov 19, 2013 2:33 pm
by chris25
Yep just after 2 hours recognised this, I was just about to delete the message when I saw your reply, the number refers to distance from window edge, but simply can not connect anything to the speed. The reason I have asked is because I have been messing with a piece of code for the last 2 hours trying to break down its logic. While I have had reasonably gratifying results from all this analyzing I am stuck since last night on one single variable declared within a function and used elsewhere. Trouble is there too much code to post here but I got the code from a game called space shooter 01 which was on a post from last year, can not find it now, I downloaded it but never saved the page from where it came. It was written by a newbie who had just joined the forum, but newbie my foot, certainly not. I typed in the name of game but no luck in finding it again. I just remember that Klaus was the person who commented on it.

Kind regards chris

EDIT: I will post the extracted code that is relevant

Re: put 'something" into variable

Posted: Tue Nov 19, 2013 2:47 pm
by chris25

Code: Select all

-- Global variables
global shipStartX
global shipStartY
global shipX
global shipY
global shipSpeedX


on arrowKey theKey 
  
   if theKey is "left" then
      put (shipSpeedX - 2) into shipSpeedX
      play audioclip "ship_move.wav"
   end if
   
   if theKey is "right" then
      put (shipSpeedX + 2) into shipSpeedX
      play audioclip "ship_move.wav"
   end if
   
   
   send Animate_All_Objects to me in 50 milliseconds
   
end arrowKey



 --- continue the animation of the ship
      
      put item 1 of the location of group "ship" into shipX
      put item 2 of the location of group "ship" into shipY
      put (shipX + shipSpeedX) into shipX
      --10 causes ship to bounce back from edge.  0 means ship just stops.
      if (shipX <=35) then
         put 35 into shipX
         put 10 into shipSpeedX
      end if
      --But the 100 here causes the ship still to stop, as does 25 but not return.
      if (shipX >=685) then
         put 685 into shipX
         put 25 into shipSpeedX
      end if
      
      set the location of group "ship" to shipX, shipY



function initializeShip
   
   put 360 into shipStartX
   put 460 into shipStartY
   
   put shipStartX into shipX
   put shipStartY into shipY
   
   set the location of group "ship" to shipX, shipY
   
   put 1 into shipSpeedX
   
   --return 0 (redundant has no purpose)
   
end initializeShip
Ok, some of the numbers have been changed by me while experienting. Some of the comments are mine also. I understand what mush of this is doing, but blow me if I can not find in all this how it is affecting the ability of the program to speed up and slow down the shipspeed.

I understand everything here. But what is completely mystifying is HOW the ship speed is being determined. I have presumed that since the ORIGINAL declared shipspeed was at 0 (from the function), then everything else must be mathematically determined by disatnces from the window sides, so having experiemented with changing these values and finding that actually even these values have absolutely zero effect on the speed of the ship. Other things I have attempted simply do not affect the ship speed. So this shipspeed X is really getting on my nerves not being able to understand how all ofthis code is affecting the ship speed, in this context the ship speed is from a slow movement to an increasingly faster movement when the horizontal keys are pressed, and pressing the opposing horizontal speed forces the ship speed to slow down to a stop.

Re: put 'something" into variable

Posted: Tue Nov 19, 2013 2:59 pm
by Mark
Hi Chris,

Every time when the left arrow key is pressed, this pine executes:

Code: Select all

 put (shipSpeedX - 2) into shipSpeedX
and the speed diminishes by 2. If the right arrow key is pressed, the spee dincreases by 2. Elswhere in the script, shipSpeedX is added to the horizontal position of the ship, et voilá, the ship moves by the speed of shipSpeedX.

Kind regards,

Mark

Re: put 'something" into variable

Posted: Tue Nov 19, 2013 3:21 pm
by chris25
Hi Mark, I did notice this, in fact this was the very first number that I changed, I guess I got so stuck in that I lost the plot. But what I was trying to understand was logically, if in maths you say : put (X - 2) into Value, then X must already be Something, so I then searched and found that this X (shipspeedX) was declared as 0. So this 0 is in fact a distance, ie the starting point of the ship as declared by shipstartX. So if I understand you correctly, everytime the arrowkey is pressed the program calculates as follows: start point + or - 2 then taken or added to the distance from the screen edge? This is what i am trying to understand, because if I can knuckle down the maths I believe that from now on the code will be easier. I have been approaching some things from the wrong perspective and I realized that one really needs to understand the calculating logic of a program before even beginning on the code. So I hope I have understood you correctly?

kind regards
chris

Re: put 'something" into variable

Posted: Tue Nov 19, 2013 4:18 pm
by Mark
Hi Chris,

Yes, in this case, you can consider the value in shipSpeedX a number of pixels or a distance in pixels. So, I think you understood it all correctly :-.)

Kind regards,

Mark

Re: put 'something" into variable

Posted: Tue Nov 19, 2013 5:07 pm
by chris25
one final thought please and Last question on this subject. To limit the max speed: I could use:??

1. number keypads say 1-4, by a series of if keydown 1 then... if keydown 2 then...?
2.use max function to limit speed but this would mean that I have to work out how fast the max speed is at my chosen limit and then calculate milliseconds/pixels or something I do not know what?
3.assign an if then statement to:

if theKey is "left" then
if (shipSpeedX - 0.1) > ?? then
do something
end if

This is as far as I can think about it. I have never come across anything that limits speed in any stack that I have peeked into.

regards
chris

Re: put 'something" into variable

Posted: Wed Nov 20, 2013 3:41 am
by Mark
Hi Chris,

You might use the min() and max() functions:

Code: Select all

put max(shipSpeedX - 2,0) into shipSpeedX

put min(25,shipSpeedX + 2) into shipSpeedX
Keep in mind that the animation may be affected by the processor speed. E.g. if you update the animation every 50 milliseconds (with 'send "Animate_All_Objects" to me in 50 millisecs' at the end of the Animate_All_Objects handler) and the script needs 55 milliseconds to update the screen, the computer or tablet will run the script as often as possible, limited by its speed.

Kind regards,

Mark