Function~Delegate Parameter'ing Help Needed

HegemonKhan
I'm not exactly sure how to describe my question~problem~situation correctly... if need be, I'll provide my code to better describe what I'm asking about.

but I'll give it a try using sample pseudo code ...is there any way to do this in quest:

// scripting:

if (xxx) {
function_variable = function_1
} else (xxx) {
function_variable = function_2
}
function_0 (function_variable)

// ---------------------------------------------

<function name="function_0" parameters="function_parameter">
function_parameter
</function>

<function name="function_1">
</function:>

<function name="function_2">
</function>


I'm still confused by Delegates, so I'm not sure if these can be used for doing this as a substitute for Functions (which so far, seems not to work ~ didn't see anything in the wiki for a command for it to do this functionality: 'GetFunction (Function_name)', I tried the 'GetUniqueElementName (xxx)' as it was the only thing that looked like it might work, but this didn't work, sighs). I don't think the 'GetObject' refers to 'Object Oriented Programming's OBJECTS: ELEMENTS in quest', only to 'Objects (a subtype of ELEMENTS) in quest', right?

The Pixie
A different approach is to use "eval". Say you have a function called "test1", you could do this to call it:
s = "test1"
result = eval(s + "()")

Note that eval has to return a value, so you need to assign that to something, even if you do nothing with it, and that also means that your function, test1, has to return a value, even if that is meaningless (just return true).

ETA: There is a good page on delegates here, but I am not sure they are what you need in this situation.
http://docs.textadventures.co.uk/quest/ ... gates.html

jaynabonne
Another alternative: you can use scripts as "function thunks", that is have scripts that call the functions for you. Then pass the scripts to your function. You'll have to use invoke instead of a direct call, but there is no way in Quest that I know of to call a function indirectly.

Your example this way:

  <function name="function_0" parameters="function_parameter">
invoke(function_parameter)
</function>
<function name="function_1">
msg("This is function_1")
</function>
<function name="function_2">
msg("This is function_2")
</function>
<command>
<pattern>test</pattern>
<script>
if (GetRandomInt(0,1) = 0) {
s => { function_1() }
} else {
s => { function_2() }
}
function_0(s)
</script>
</command>

In this case, "{ function_1() }" is an anonymous script that calls a function. And "=>" is the way you assign such a script to a variable. It could be any sort of script you like. For example, this is valid (though a bit stupid):

s => {
msg("Greetings, sir")
if (player.clothed) {
msg("Glad to see you are ready for the day.")
}
}

That will make "s" contain the script body you assign it. It's not a very common thing - you can just as easily create scripts as attributes and reference them the same way.

If they don't have to be functions, then you can just hang scripts off an object and pass them directly. It will invoke the script.

Delegates are just special script-like things that take parameters and/or return values. They can't hold a function as such either.

jaynabonne
Just to follow up, if they don't need to be functions per se, then I'd just use script attributes directly. Create some sort of holder object (or even use the game itself). Then you can just do things like:

     if (GetRandomInt(0,1) = 0) {
s = game.script_1
} else {
s = game.script_2
}
function_0(s)


function_0 would be the same (still uses invoke).

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

Support

Forums