Adding a script fragment to an existing script

Hi community.

Is there a way to add something to a script fragment, such as object.changedparent? Like a variable:

string = string + "Contents"

script = script + => Addscript
object. changedparent = object. changedparent + => Script


(filler for getting my edited post. updated/posted)

(finally a topic that I can help with pretty well, for once, hehe)

yes, it involves using 'Delegates', here's documentation:

(after I learned how delegates work... I now hardly ever use Functions, as I like working with Objects for encapsulation and organization, hehe)

basically a delegate is like a 'prototype' but for Script Attributes instead of functions

and the delegate enables a Script Attribute to have the same functionality as a Function (parameters and/or returning a value)

here's an example of it:

(this is using an Integer Parameter VARIABLE, but you can use an Object Parameter VARIABLE, for what you want to do in your OP, or any of the Data/Attribute Types: Strings/Doubles/Booleans/Lists/Dictionaries/Integers/Objects, just like with a Function)

<include ref="English.aslx" />
<include ref="Core.aslx" />

This is a good location for Delegates, to ensure they get compiled first, so you can then use them (without/no errors):

<delegate name="two_integers_and_return_delegate" type="int" parameters="integer_parameter_1, integer_parameter_2" />


<game name="example_game">
  <attr name="start" type="script">
    msg ("Addition Result: " + addition_script_attribute (5, 10))
    msg ("Multiplication Result: " + multiplication_script_attribute (5, 10))
  <attr name="addition_script_attribute" type="two_integers_and_return_delegate">
    return (integer_parameter_1 + integer_parameter_2)
  <attr name="multiplication_script_attribute" type="two_integers_and_return_delegate">
    return (integer_parameter_1 * integer_parameter_2)

// output/results:
Addition Result: 15
Multiplication Result: 50

yes, you can do concatenation:

(simple/quick example)

string_variable = "Hi"
string_variable = string_variable + ", my name is HK"
msg (string_variable)

// output/result:

Hi, my name is HK

you can also use the 'set' Function:

string_variable = "Khan"
set (player, "alias", "Hegemon" + string_variable)
msg (player.alias)

// output/results:


for scripting / script Attributes, you can use the 'do' Function for doing concatenation:

(just a quick example)

// Thanks KV for the correction (was missing the curly braces):

joe.run_laps_script_attribute => { msg ( + " runs laps") }
jim.run_laps_script_attribute => { msg ( + " runs laps") }

game.my_objectlist_attribute = NewObjectList ()
list add (game.my_objectlist_attribute, joe)
list add (game.my_objectlist_attribute, jim)

string_variable = "run_laps"

foreach (object_variable, game.my_objectlist_attribute) {
  do (object_variable, string_variable + "_script_attribute")

// output/results:
joe runs laps
jim runs laps

but I'm not sure about the 'Script Attribute' stuff... hopefully someone else can help with it...

I know you can make a script like this:

game.myScript => {
  msg ("Blah, blah, blah...")

Then, you can:

myOldScript = game.myScript

...but I don't know if you can add on to the script like you can a string.

You want to create scripts at runtime? Uuuu... A cold shiver runs down my spine. :-)

If you just want to add something to the default script, it'd be easier to copy the default script into your code.
Or this.oldchangedparent = this.changedparent, and then set a new script which does do (this, "oldchangedparent") at the end.

If you might be adding multiple scripts, here's a way to do it…
(warning: this is massive overkill for anything you are really likely to want)

if (not HasAttribute(this, "listchangedparent")) {
  this.listchangedparent = NewList()
  if (HasScript(this, "changedparent")) {
    list add (this.listchangedparent, this.changedparent)
  this.changedparent => {
    parameters = NewDictionary()
    if (IsDefined("oldvalue")) {
      dictionary add (parameters, "oldvalue", oldvalue)
    for (i, 0, ListCount(this.listchangedparent)-1) {
      set (this, "scriptpart_changedparent"+i, ListItem(this.listchangedparent, i))
      do (this, "scriptpart_changedparent"+i, parameters)
newscript => {
  // Script you want to add goes here
list add (this.listchangedparent, newscript)

(In the past I've looped over a list of scripts calling them with invoke; in this case it's probably easier to add them to numbered script attributes so that they can all use this normally. But it's easier to add/remove/check-existence for lists, so easier to keep them in a list and then map onto the numbered attributes when they're needed)

This topic is now closed. Topics are closed after 60 days of inactivity.