Attributes: Scripts

Sora574
07 Apr 2013, 20:12
Okay, this is my last complaint, I promise :lol:

I noticed that you can set a variable, not just an object attribute, to a script like this
Wiki (Setting variables) wrote:
variable => { script }

However, you can't really do anything with that variables, or in other words, you can't run the script.
I've searched everywhere and there seems to be no way to do it. Of course, there's a simple way around it by setting it to an object attribute, but I think you should be able to run it straight from the variable. It just makes creator's lives that much easier.

An example of when this would be needed is something like...
<!--For a command with the parameter 'throw #object#'-->
s => {
switch (object) {
case (ball) {
if (b = true) {
msg ("You throw the ball and hit the target.")
}
else {
msg ("You try to throw the ball, but you don't have arms.")
}
}
case (apple) {
if (b = true) {
msg ("You don't want to. You eat it instead.")
}
else {
msg ("You drop it on the floor because well... You're armless.")
}
}
}
}

if (Got(object)) {
if (Got(arms)) {
b = true
}
else {
b = false
}
run (s)
}
else {
msg ("Pick it up first.")
}

jaynabonne
07 Apr 2013, 21:35
This works:

var => { msg("hello!") }
invoke(var)

jaynabonne
07 Apr 2013, 21:42
As a side note, did you know about passing scripts to functions, that you can inline the final parameter? For example, if you have a function like:

<function name="myFunc" parameters="list, script">
// do something with the list
invoke(script)
</function>

Then you can call it with:

myFunc(list) {
msg("myFunc has finished!")
}

Sora574
07 Apr 2013, 21:43
jaynabonne wrote:This works:

var => { msg("hello!") }
invoke(var)

...Oh. Well... You passed the test! Because... That's what this was. :)

( Lol thanks... I don't know how I didn't see that :oops: )

Sora574
08 Apr 2013, 01:43
jaynabonne wrote:As a side note, did you know about passing scripts to functions, that you can inline the final parameter? For example, if you have a function like:

<function name="myFunc" parameters="list, script">
// do something with the list
invoke(script)
</function>

Then you can call it with:

myFunc(list) {
msg("myFunc has finished!")
}

Oops didn't see that post...
And yes, but I don't think it's necessary for what I am doing.

[wish there was a strikethrough code]However... Could you help with something else?
I can't seem to figure out how to capitalize words after every space for things like names.
I might be missing a built-in function, but I've tried to make a function that includes the "CapFirst" "Replace" and "Split" functions, but they don't work. It spits out an error message saying that those 3 functions don't exist.[/strikethrough]

EDIT: Nevermind, I sort of found a way around it.
<function name="CapSpaces" parameters="text" type="string">
text = Replace (text, " a", " A")
text = Replace (text, " b", " B")
text = Replace (text, " c", " C")
text = Replace (text, " d", " D")
text = Replace (text, " e", " E")
text = Replace (text, " f", " F")
text = Replace (text, " g", " G")
text = Replace (text, " h", " H")
text = Replace (text, " i", " I")
text = Replace (text, " j", " J")
text = Replace (text, " k", " K")
text = Replace (text, " l", " L")
text = Replace (text, " m", " M")
text = Replace (text, " n", " N")
text = Replace (text, " o", " O")
text = Replace (text, " p", " P")
text = Replace (text, " q", " Q")
text = Replace (text, " r", " R")
text = Replace (text, " s", " S")
text = Replace (text, " t", " T")
text = Replace (text, " u", " U")
text = Replace (text, " v", " V")
text = Replace (text, " w", " W")
text = Replace (text, " x", " X")
text = Replace (text, " y", " Y")
text = Replace (text, " z", " Z")
text = CapFirst(text)
return (text)
</function>

and then I use
msg (CapSpaces( string ))

Maybe there's an easier way than doing this though?

jaynabonne
08 Apr 2013, 09:10
This should do what you want:

  <function name="CapWords" parameters="input" type="string">
<![CDATA[
words = Split(input, " " )
capped = NewStringList()
foreach (word, words) {
if (word <> "") {
word = CapFirst(word)
}
list add(capped, word)
}
return (Join(capped, " "))
]]>
</function>


The "if" in there is just to handle a case where there are multiple spaces in the same word, since CapFirst doesn't like empty strings. It may never be a case you run into, but just in case, I figured it would be better to have the code in there than have it spit out a message like:

Error running script: Error evaluating expression 'CapFirst(word)': Index and length must refer to a location within the string.Parameter name: length

If you'd rather have it simpler, you can just use this:

  <function name="CapWords" parameters="input" type="string">
<![CDATA[
words = Split(input, " " )
capped = NewStringList()
foreach (word, words) {
list add(capped, CapFirst(word))
}
return (Join(capped, " "))
]]>
</function>


but beware of the limitations. :)

Sora574
08 Apr 2013, 09:52
Ohhh... There's a Join function...
That makes my life so much easier -- thank you lol

Also, that script is exactly what I needed, thanks. It also made me think... What if there's a symbol or number after a space? I guess the Replace function could just erase it right?

jaynabonne
08 Apr 2013, 09:56
It works fine. The "upper case" of a number or symbol is just that number or symbol (just as if it is already upper case, it gets left as is).

Sora574
08 Apr 2013, 12:42
jaynabonne wrote:It works fine. The "upper case" of a number or symbol is just that number or symbol (just as if it is already upper case, it gets left as is).

Ah okay. That makes sense

Thanks again