Script not running closing card and returning

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Aduro91
Posts: 36
Joined: Sat Jul 22, 2023 8:49 pm

Script not running closing card and returning

Post by Aduro91 » Wed Dec 20, 2023 1:52 am

I have an issue with a script of a card not running when I leave the card and then come back, making my game only playable once!

I have a global gtimer thing running in idle which sets off a doaction function:

on openCard
set the idleticks to 5
global gTimers
put "1,doAction,this card,0" into gTimers
pass openCard
end openCard

on idle
global gTimers
put the ticks into currentTime
repeat with x = 1 to number of lines of gTimers
put line x of gTimers into theLine
if (item 4 of theLine + item 1 of theLine) < currentTime then
do "send" && quote & item 2 of theLine & quote && "to" && item 3 of theLine
put currentTime into item 4 of line x of gTimers
end if
end repeat
pass idle
end idle

on closeCard
global gTimers
put empty into gTimers
end closeCard

on doAction
if length (fld "timer") > 0 then
put empty into fld "timer"
else
put "*" after fld "timer"
end if

etc etc....


So I have my game looking at the length of field "timer" to execute all the moving elements while still being able to click buttons etc. When a player quits though with a button sending them to another card, if they return, this process of continually putting in and removing the asterisk ends and so nothing moves.

I've tried putting in conditions to correct this, as I thought maybe interrupting the idle/doaction gtimer commands mid execution was causing things to jam. A quit button appears when its game over, and so I put in 'if the visible of button "quit" is false then" for all those idle and do action commands, so they should all stop before the person quits. It still doesn't work when returning to that card.

Any ideas anyone?

Cheers!

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 3:15 am

Ideas...

First, I would NOT use the idle message to do anything, ever. Well, hardly ever.
Second, I cannot help at all without your stack. The code snippet you posted has no context.

There are a lot of little things than can be cleaned up, but they are not terribly important now. Can you post the stack?

Craig

Aduro91
Posts: 36
Joined: Sat Jul 22, 2023 8:49 pm

Re: Script not running closing card and returning

Post by Aduro91 » Wed Dec 20, 2023 3:44 am

dunbarx wrote:
Wed Dec 20, 2023 3:15 am
Ideas...

First, I would NOT use the idle message to do anything, ever. Well, hardly ever.
Second, I cannot help at all without your stack. The code snippet you posted has no context.

There are a lot of little things than can be cleaned up, but they are not terribly important now. Can you post the stack?

Craig
Thanks for the reply!

Could you let me know how you would make a constantly changing field at a consistent rate that I can use as a reference point to anchor the movement of my game, without using idle?

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 4:28 am

Hi.

When you say a a "constantly changing field at a consistent rate", do you mean a field where the contents change, perhaps randomly?

Craig

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 4:36 am

Were you a Hypercard user way back when? I was. The idle message was all the rage in 1989, but really should not be used.

Not sure how late it is for you, but try this. On a new card make a button and a field. In the button script:

Code: Select all

on mouseUp
   changeField
end mouseUp

on changeField
   if the optionKey is down then exit to top
   put random(999) && any char of "XYZ" into fld 1
   send "changeField" to me in 30 -- change this value to adjust the timing
end changeField
This is a more modern way to time events, and this simple gadget can be far more easily managed than using "idle".

Craig

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 4:38 am

Also, always place code within the code tags "</>". These are right above this text field, fifth from the left. Quotes as well within their own tags, fourth from the left.

Craig

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 4:43 am

An example of text within quote tags:
that I can use as a reference point to anchor the movement of my game,
So you now have this changing field. Know that the posted time period, 30, is measured in "ticks", which are 1/60 of a second.

Now what? How do you want to interact with your game? How is this governed by the contents of the field?

Craig

AndyP
Posts: 615
Joined: Wed Aug 27, 2008 12:57 pm
Location: Seeheim, Germany (ex UK)
Contact:

Re: Script not running closing card and returning

Post by AndyP » Wed Dec 20, 2023 7:15 am

Also the global gTimers only needs to be initiated once.
Just put global gTimers at the top of the card script outside of any handlers and it will be available for use for all handlers in the card script.
To use gTimers any where else, eg. a button, another card, stack again just add to the top of the script of the control etc that users it.
Andy Piddock
https://livecode1001.blogspot.com Built with LiveCode
https://github.com/AndyPiddock/TinyIDE Mini IDE alternative
https://github.com/AndyPiddock/Seth Editor color theming
http://livecodeshare.runrev.com/stack/897/ LiveCode-Multi-Search

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 4:12 pm

What Andy said. But know that global and local variables MUST be declared above whichever handlers might need them. I always put mine at the very top of the script so they are available everywhere.

I suppose it may be desirable to limit such variables only to certain handlers within a script. If so, one must be careful that the variables of interest along with any such handlers are placed below any others that are to be excluded.

I have never needed to do that. Has anyone else?

Craig

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Script not running closing card and returning

Post by stam » Wed Dec 20, 2023 6:55 pm

dunbarx wrote:
Wed Dec 20, 2023 4:12 pm
I have never needed to do that. Has anyone else?

Craig
Nope. And I can't see a real reason to do this and risk my sanity ;)

I mean handler-local variables declared within the handler are one thing. Script local and global variables always up top, outside the handlers.
I think this confuses some new users because there is some comment I remember seeing in some lesson or post that you had to declare the global variable in the handler to use this and it took me a short while to realise that's not the case when I picked up LC 3 years ago.

Personally I don't use global variables much but I do use a ton of script-local variables declared up-top, which are basically globals but only for the handlers in the same script. Very useful...
Aduro91 wrote:
Wed Dec 20, 2023 3:44 am
Could you let me know how you would make a constantly changing field at a consistent rate that I can use as a reference point to anchor the movement of my game, without using idle?
Personally, without really knowing your use case and I suspect like others may have suggested, I would use a 'send in time' to keep calling a handler at set intervals - if that's what you mean?
i.e.

Code: Select all

on setReferencePointToAnchorMovement
  // do some stuff
  send "setReferencePointToAnchorMovement" to me in 500 milliseconds -- or whatever interval you need
end setReferencePointToAnchorMovement
or some such...

Also to clarify Craig's post about code tags, these are [ code] and [ /code] (without the spaces) that surround your code and keep it's formatting.
To do this easily, select the text of your code and click the "</>" button, 5th from the left starting the the Bold/italics etc

Aduro91
Posts: 36
Joined: Sat Jul 22, 2023 8:49 pm

Re: Script not running closing card and returning

Post by Aduro91 » Wed Dec 20, 2023 7:24 pm

Thanks all, sending the function to me every x milliseconds works, I don't have the problem anymore!

I'll make sure to avoid idle from now on. I grew up with Hypercard, although I was a kid and didn't learn idle stuff. But later on I went back to hypercard on a Mac OS 9 emulator and was cobbling games together using idle. I had heard it wasn't the best thing to do but I was a little negligent and continued with it.

Thanks so much for all the help guys!!

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 7:25 pm

Stam.

Since you wrote it, do you know what " setReferencePointToAnchorMovement" could possibly mean?

It's the "anchor" part I am not getting. I have never anchored anything in a LC stack. The OP has a field with regularly changing text in it. When do you drop anchor?

Craig

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

Re: Script not running closing card and returning

Post by dunbarx » Wed Dec 20, 2023 7:28 pm

@Aduro91. AHA. Glad it works.

Milliseconds, eh?

So since it does now work, what are you doing in (or between) the intervals? I am curious how the user interacts with this ever-changing field.

Craig

stam
Posts: 2686
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Script not running closing card and returning

Post by stam » Wed Dec 20, 2023 7:40 pm

dunbarx wrote:
Wed Dec 20, 2023 7:25 pm
Stam.

Since you wrote it, do you know what " setReferencePointToAnchorMovement" could possibly mean?

It's the "anchor" part I am not getting. I have never anchored anything in a LC stack. The OP has a field with regularly changing text in it. When do you drop anchor?

Craig
No I don’t know, I just used the OP’s words as an example, to illustrate the “send in time” command.

I’ve long ago learnt that how people use words is not the same from one to another. Just let it flow around you and the force will guide you ;)

Aduro91
Posts: 36
Joined: Sat Jul 22, 2023 8:49 pm

Re: Script not running closing card and returning

Post by Aduro91 » Thu Dec 21, 2023 2:53 am

dunbarx wrote:
Wed Dec 20, 2023 7:28 pm
@Aduro91. AHA. Glad it works.

Milliseconds, eh?

So since it does now work, what are you doing in (or between) the intervals? I am curious how the user interacts with this ever-changing field.

Craig
So it's not that the user interacts with the field. Maybe I'm overengineering in this method, but its where I got to from troubleshooting through my Hypercard experience.

This code I've asked about is the motor behind the game running. When I used Hypercard as a kid I could never create a game in which you had to do something before something else happens. This is because I would create buttons with something like wait 1 second, go to card "gameover" or what have you. Then I would notice that I can't click any buttons before the 1 second is up, same with if I was cycling through cards i.e., you can't shoot the bad guy before he shoots you, for example.

So I asked on reddit how to get around this and somebody gave me the idle code I referenced earlier. This worked, as I could have a something happening, an imminent threat coming closer, while the user can still interact with buttons etc.

I have the moving elements, such as a rising flood in my case, attached to the 'anchor' or perhaps better described as a 'motor' (e.g. if length (field "timer") > 0 then, *threat gets closer"). An asterix is put it the field, and then removed, then back in, on and on - this time using a loop created by the 'send "doaction" to me in 100 milliseconds'. So I have all the elements of the game I want to be happening while the user can click buttons etc attached to this motor, or at least that is the underlying thing pushing all the elemtents along.

Maybe with Livecode this is an overengineered way to do things but its the way I've learnt to make players have to do something before something else happens! Its working now, thanks all. I hope to submit my game to the app store perhaps early in the new year and I can share it on the forum then.

Post Reply

Return to “Talking LiveCode”