Page 1 of 1
change script from script
Posted: Sun Dec 19, 2021 11:26 pm
by Samuele
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

)
any ideas?
thanks!
Re: change script from script
Posted: Mon Dec 20, 2021 12:14 am
by SparkOut
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.
Re: change script from script
Posted: Mon Dec 20, 2021 12:17 am
by dunbarx
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
Re: change script from script
Posted: Mon Dec 20, 2021 12:20 am
by dunbarx
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.
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
Re: change script from script
Posted: Mon Dec 20, 2021 12:51 am
by dunbarx
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
Re: change script from script
Posted: Mon Dec 20, 2021 1:08 am
by dunbarx
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
Re: change script from script
Posted: Mon Dec 20, 2021 7:36 am
by SparkOut
@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.
Re: change script from script
Posted: Mon Dec 20, 2021 3:13 pm
by dunbarx
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
Re: change script from script
Posted: Mon Dec 20, 2021 5:04 pm
by Samuele
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?
Re: change script from script
Posted: Mon Dec 20, 2021 5:56 pm
by FourthWorld
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.
Re: change script from script
Posted: Mon Dec 20, 2021 6:01 pm
by dunbarx
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
Re: change script from script
Posted: Mon Dec 20, 2021 6:03 pm
by dunbarx
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