Is it possible to send a time-delayed message with parameters?

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
AGC studios
Posts: 21
Joined: Thu Apr 23, 2020 1:49 pm

Is it possible to send a time-delayed message with parameters?

Post by AGC studios » Mon Nov 30, 2020 7:25 pm

Pretty much as the title says. I'm making a mobile game for fun where the player plays as a spaceship and needs to pass all sorts of challenges (the user controls the player using the accelerometer) and the last "boss" level is going through an "asteroid belt" and avoiding getting hit. The asteroids are spawned at the top of the screen and then move down using "move". I've got it all figured out other than when the asteroids reach the bottom of the screen, I'd like to delete them rather then them piling up at the bottom and using the processing power of the phone, I'd like to delete the asteroids. The problem is, the only way I could think of doing this is to send a custom event (I'm sorry if this isn't the correct terminology, English is my second language, code below for clarification.) with a time delay, the issue is, I can't seem to find a command that allowed both sending parameters (which I need due to the way the code is written) and a time delay, any ideas?

code:

Code: Select all

on MoveAst
   put (item two of loc of _gAsteroidID + 658) into _tYcoord
   move _gAsteroidID to (item one of loc of _gAsteroidID) , _tYcoord in 7 sec without waiting 
   dispatch EndOfLife to _gAsteroidID in 7 sec  {I'd like to add parameters here}
end MoveAst

on EndOfLife pAstID pAstNum
   if the bottom of pAstID is 645
   then
      delete variable _gAstList[pAstID]
      delete pAstID
   else
      send EndOfLife to _gAsteroidID in one tick with pAstID , pAstNum
   end if
end EndOfLife
full code if anyone is interested:

Code: Select all

--Vars--
global _gAsteroidBaseName
global _gAsteroidNumber
global _gNameingState
global _gCurrentAstroidName
global _gAsteroidID
global _gIconID
global _gAstList

--methods--
on CreateAsteroid
   put "Asteroid_" into _gAsteroidBaseName --setsThe base name for each astroid made ["Asteroid_XX"]
   put AsteroidNameMaker() into _gCurrentAstroidName  --calls the name making function in a var
   create invisible graphic _gCurrentAstroidName --creats new graphic w/ the name stored in the var
   put the long ID of graphic _gCurrentAstroidName into _gAsteroidID --stores the ID of the newly made asteroid
   put _gAsteroidID into _gAstList[_gAsteroidNumber]
   add 1 to _gAsteroidNumber --adds 1 to the var for the next asteroid made
end CreateAsteroid

on SetAstPropoties
   put the long ID of ChooseIcon() into _gIconID --calls the icon chooser function and stores it in a var 
   set the backgroundPattern of _gAsteroidID to the id of _gIconID --changes the icon of the asteroid
   set the opaque of _gAsteroidID to true 
   set the colorOverlay of _gAsteroidID to false
   set the lineSize of _gAsteroidID to 0
   set the lockloc of _gAsteroidID to true
   set the width of _gAsteroidID to 100
   set the height of _gAsteroidID to 100
   set the left of _gAsteroidID to (random(220))
   set the bottom of _gAsteroidID to 0
   set the visible of _gAsteroidID to true
end SetAstPropoties

on MoveAst
   put (item two of loc of _gAsteroidID + 658) into _tYcoord
   move _gAsteroidID to (item one of loc of _gAsteroidID) , _tYcoord in 7 sec without waiting 
   dispatch EndOfLife to _gAsteroidID in 7 sec _gAsteroidID , (_gAsteroidNumber - 1)
end MoveAst

on CheckCollisons
   repeat for each element _tLine in _gAstList
      if intersect (_gAsteroidID , _tLine , "pixels") and _gAsteroidID <> _tLine
      then
         put "yes" into _tToDel
      end if
   end repeat
   if _tToDel is "yes"
   then
      delete variable _gAstList[(_gAsteroidNumber - 1)]
      delete _gAsteroidID
   else
      MoveAst
   end if
end CheckCollisons

on EndOfLife pAstID pAstNum
   if the bottom of pAstID is 645
   then
      delete variable _gAstList[pAstID]
      delete pAstID
   else
      send EndOfLife to _gAsteroidID in one tick with pAstID , pAstNum
   end if
end EndOfLife

--functions--


function AsteroidNameMaker
   if _gNameingState is not "passedIf" --checks if this the first time an asteriod is made
   then
      put 0 into _gAsteroidNumber --resets the var
      put "passedIf" into _gNameingState
   end if 
   return (_gAsteroidBaseName & _gAsteroidNumber) --returns the name
end AsteroidNameMaker

function ChooseIcon
   put the long ID of img "asteroidOne" into _tImage[1]
   put the long ID of img "asteroidTwo" into _tImage[2]
   return _tImage[random(2)] --chosses one of the (2) images
end ChooseIcon

Thank you for reading!

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Is it possible to send a time-delayed message with parameters?

Post by Klaus » Mon Nov 30, 2020 7:40 pm

Hi AGC studios,

you are mixing the syntax of SEND and DISPATCH.

Code: Select all

...
## All strings and messages you send in quotes! 
## Get used to it in your own interest! The engine is getting less forgiving with "sloppy" syntax with every version.
## Believe me, I experienced this several times in the last 20 years!
dispatch "EndOfLife" to _gAsteroidID with pAstID , pAstNum
...
However DSIPATCH does not support ... in time
But you can do:

Code: Select all

...
## LC will of course replace the name of the variables with their content this way!
send "EndOfLife pAstID, pAstNum" to _gAsteroidID in 1
...
Hint:
If you leave out the "timescale" like in my example, LC presumes TICKS.


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Is it possible to send a time-delayed message with parameters?

Post by dunbarx » Mon Nov 30, 2020 7:57 pm

Sure.

Code: Select all

on mouseUp
   testParamSending
end mouseUp
   
on  testParamSending x
   put x
   if the optionKey is down then exit to top
   send  "testParamSending" && random(999) to me in 10
end testParamSending
Craig

AGC studios
Posts: 21
Joined: Thu Apr 23, 2020 1:49 pm

Re: Is it possible to send a time-delayed message with parameters?

Post by AGC studios » Mon Nov 30, 2020 8:06 pm

Klaus wrote:
Mon Nov 30, 2020 7:40 pm
Hi AGC studios,

you are mixing the syntax of SEND and DISPATCH.

Code: Select all

...
## All strings and messages you send in quotes! 
## Get used to it in your own interest! The engine is getting less forgiving with "sloppy" syntax with every version.
## Believe me, I experienced this several times in the last 20 years!
dispatch "EndOfLife" to _gAsteroidID with pAstID , pAstNum
...
However DSIPATCH does not support ... in time
But you can do:

Code: Select all

...
## LC will of course replace the name of the variables with their content this way!
send "EndOfLife pAstID, pAstNum" to _gAsteroidID in 1
...
Hi Klaus! Thanks for the speedy response! The code I sent was a trial and error version of me testing to see if I could possibly combine both dispatch and send, and I forgot to change it back to send after I concluded that it wouldn't work. I've used both dispatch and send many times so I know that lol. When you say to put all messages inside quotes, does this include variables and handlers (I think that's what they are called, blocks of code)? Because then how would LC distinguish between a string and variable/ handler?

I will try your way and see what happens :) Again, thank you for responding and have a great day!

Klaus
Posts: 13829
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Is it possible to send a time-delayed message with parameters?

Post by Klaus » Mon Nov 30, 2020 8:14 pm

AGC studios wrote:
Mon Nov 30, 2020 8:06 pm
When you say to put all messages inside quotes, does this include variables and handlers (I think that's what they are called, blocks of code)?
No, only strings like object names etc and the name of the message you want to SEND or DISPATCH
including the parameters for the sent handlername, see my example.
AGC studios wrote:
Mon Nov 30, 2020 8:06 pm
Because then how would LC distinguish between a string and variable/handler?
No idea, ("Its magic, ladies and gentlemen!" :-)) but it works, just like in my example above:

Code: Select all

...
send "EndOfLife pAstID, pAstNum" to _gAsteroidID in 1
...

AGC studios
Posts: 21
Joined: Thu Apr 23, 2020 1:49 pm

Re: Is it possible to send a time-delayed message with parameters?

Post by AGC studios » Mon Nov 30, 2020 9:04 pm

Klaus wrote:
Mon Nov 30, 2020 8:14 pm
Ok! To be honest, this sounds very counterproductive, if the goal is to keep the syntax as understandable and compiler "bug-free", then why use apostrophes when apostrophes are the way the compiler (or any other technique LiveCode uses for translating code to computer language) knows stuff are strings, why confuse it with using apostrophes for other uses :?: :!: Will parenthesis do the same job as the apostrophes? If so, aren't they more suitable to use? (I may very well be missing something as I'm really not all that knowledgeable when it comes to theory and syntax lol.)

Anyway, It works now! I'm having problems that when I delete each asteroid the program stops momentarily and all of the other asteroids and moving objects stop moving, which isn't really good so I'm trying to solve that. (It's also weird because I delete elsewhere in the code and it doesn't stop the program so I'm very confused...)

AGC studios
Posts: 21
Joined: Thu Apr 23, 2020 1:49 pm

Re: Is it possible to send a time-delayed message with parameters?

Post by AGC studios » Mon Nov 30, 2020 9:07 pm

dunbarx wrote:
Mon Nov 30, 2020 7:57 pm
Thank you! Fundamentally, what's the difference between and, with, &, and &&? I know I sound very basic here but I never understood the difference so I always guess and change if it doesn't work lol.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Is it possible to send a time-delayed message with parameters?

Post by SparkOut » Mon Nov 30, 2020 9:21 pm

The easy part to answer here is that & is a concatenator and will join two strings. && is a concatenator and will join two strings with a space separating them.

Code: Select all

put "This" & "that"
-> Thisthat

Code: Select all

put "This" && "that"
-> This that


Now,

Code: Select all

send "myMessage" & tParam1 & comma & tParam2
will pass only the first parameter.

Code: Select all

send "myMessage tParam,tParam2"
will be evaluated by the engine and pass both parameters.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9837
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Is it possible to send a time-delayed message with parameters?

Post by FourthWorld » Mon Nov 30, 2020 9:48 pm

FWIW there's an enhancement request in the queue to extend the send command to use the cleaner argument passing style we enjoy with dispatch:
https://quality.livecode.com/show_bug.cgi?id=13287
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9663
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Is it possible to send a time-delayed message with parameters?

Post by dunbarx » Mon Nov 30, 2020 9:55 pm

What Sparkout said.

The operator "and" is entirely different than the ampersand. You use "and" in boolean gadgets like;

if arg > 5 and arg < 7 (if true, likely a 6)

Craig

AGC studios
Posts: 21
Joined: Thu Apr 23, 2020 1:49 pm

Re: Is it possible to send a time-delayed message with parameters?

Post by AGC studios » Thu Dec 10, 2020 4:52 pm

~mistake, I found my problem the second I posted this~

Post Reply

Return to “Talking LiveCode”