change script from script

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Samuele
Posts: 282
Joined: Mon Oct 11, 2021 7:05 pm

change script from script

Post by Samuele » Sun Dec 19, 2021 11:26 pm

Hi, i want to change the code of an object in the middle of a running process, or more clearly, is there a way to change the script of an object from another script? like, if the user reaches level 2 i want to change some things in the script, for example speed (and not a lot of things, if not i would've made another card :D )
any ideas?
thanks!
Samuele.

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

Re: change script from script

Post by SparkOut » Mon Dec 20, 2021 12:14 am

Yes you can.
But you probably don't need to.

You can

Code: Select all

put the script of card "theCardRef" into tScript
--edit tScript
set the script of card "theCardRef" to tScript
But something as simple as a change of speed according to the level should be handled programmatically by variable name, or by custom property.

Eg, on reaching level 2, set the cWarpFactor of card "theCardRef" to 2, and in the section that moves the background starscape down the screen

Code: Select all

set the top of graphic "backStars" to the top of graphic "backStars" + the cWarpFactor of this card
or other handling as appropriate. You CAN change the script of an object on the fly, but there's very little likelihood that you would need to.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: change script from script

Post by dunbarx » Mon Dec 20, 2021 12:17 am

Hi.

The script of an object is simply a property of that object, no different than its loc or it topLeft.

So you can:

Code: Select all

set the script of yourControl to someContainer
"someContainer" could be anything, another script, the contents of a field, a variable, whatever.

But you cannot do that while the target script is running.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: change script from script

Post by dunbarx » Mon Dec 20, 2021 12:20 am

Sparkout.

Our posts crossed in the eMail.

How can you modify a running script? I don't see how at all, though that means nothing. :wink:

I will make some tests, like running a handler that has "wait with messages" and try to change that script by clicking a button, and also in-line. I don't think HC could do anything like that.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: change script from script

Post by dunbarx » Mon Dec 20, 2021 12:51 am

So I tried two experiments. Neither allowed a running script to be modified on the fly.

1- A button runs a simple loop that places random chars into a field. Those chars are created in-line and stored in a variable. Another button tries to change that variable on the fly. Nothing changes.

2- A button's handler is running. Another button tries to change the script of that button. An error is thrown telling me that one cannot modify a running handler.

I can post the stack with the two experiments, and would be very interested to know that I am off base. Self modifying code is something I thought that xTalks could not have.

Craig

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: change script from script

Post by dunbarx » Mon Dec 20, 2021 1:08 am

Samuele.
if the user reaches level 2 i want to change some things in the script, for example speed
This is much simpler to do by monitoring the status of the user. That can be done in many ways, and need never have to go down the path of modifying the actual handler itself. Consider a button and a field on a new card. In the button script:

Code: Select all

on mouseUp
   put "abcdef" into dataSet
   repeat with y = 1 to 30
      put any char of dataSet into fld 1
      wait 10 with messages
      if y = 15 then
         put "CHANGEOVER" into fld 1
         wait 20
         put "123456" into dataSet
      end if
   end repeat
end mouseUp
Or imagine you have a "switch" construction in your handler, and the case statements fire according to certain rules.

This is common practice, to test the state of the stack and act accordingly.

Craig

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

Re: change script from script

Post by SparkOut » Mon Dec 20, 2021 7:36 am

@Craig, I never said you could change a running script. It just didn't occur to me to mention that you can't. Reading the original request, I can see that is a barrier to the "change the script" approach as well, the OP did say "from another script" but also when a level changed so it could easily be within the game loop and currently running. I was too focused on thinking "don't do it this way" to think of the extra problems of a running script.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: change script from script

Post by dunbarx » Mon Dec 20, 2021 3:13 pm

Sparkout.

I read your
"You CAN change the script of an object on the fly, but there's very little likelihood that you would need to.
Anyway, we are all good. There is no need for the OP to even think down that path.

But any thoughts on self-modifying code in an xTalk in general?

Craig

Samuele
Posts: 282
Joined: Mon Oct 11, 2021 7:05 pm

Re: change script from script

Post by Samuele » Mon Dec 20, 2021 5:04 pm

SparkOut wrote:
Mon Dec 20, 2021 12:14 am
Yes you can.
But you probably don't need to.

You can

Code: Select all

put the script of card "theCardRef" into tScript
--edit tScript
set the script of card "theCardRef" to tScript
ok thanks but can i change only a line of the script?
Samuele.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10045
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: change script from script

Post by FourthWorld » Mon Dec 20, 2021 5:56 pm

dunbarx wrote:
Mon Dec 20, 2021 3:13 pm
But any thoughts on self-modifying code in an xTalk in general?
Sef-modifying code is more expensive to maintain, and usually not necessary.

Have you seen a case where an alternative using static code didn't exist?

Behaviors are great for dynamically swapping code, but fully static, and so readable, and so more easily maintained.

In many cases good use of the range of conditional expressions available can do what's needed.

The subject comes up now and then, but I don't recall seeing a case where a static alternative wasn't available.

If we find such a case it would be interesting to see.
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: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: change script from script

Post by dunbarx » Mon Dec 20, 2021 6:01 pm

Samuele.

Again, a script is a property of an object. Just like its backColor, you have to think of a property as a whole. One can set the script to anything you want. But it is the whole of its contents.

It is NOT a container, like a field or variable, where you can change a portion of its contents, like a single word, in place.

So to answer your question, if you want to change one line, you must extract the whole script into somewhere, change whatever you want in it, and then set the script of the object to what you just made.

Craig
Last edited by dunbarx on Mon Dec 20, 2021 11:38 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: change script from script

Post by dunbarx » Mon Dec 20, 2021 6:03 pm

Richard.
and usually not necessary.
I agree completely. I have never even wished for it, never having needed it. i was just musing based on the OP comments.

Craig

Post Reply