NewScriptDictionary Questions

Entropic Pen
28 Dec 2013, 18:48
I'm on a hot lead to create procedurally generated planets for "The Secrets of Planet X", but I'm running into a hiccup: I need to create script dictionaries to handle events/actions like landing on generated planet to search for lifeforms (sentient or not), and mining it for resources.

All that I can handle but I just need to know how I can create script dictionaries using scripts and adding scripts to those script dictionaries?

jaynabonne
28 Dec 2013, 21:13
I don't know *exactly* what you want to do, so let me cover all the bases I can think of.

You can just use CreateDictionary() to create an empty dictionary and then add scripts to it. The tricky part is how to add the scripts. You can't build scripts on the fly (sadly). But as long as the scripts you want to add are known ahead of time, then it's just a question of where you store them before adding them to the dictionary.

One way is like this:

      sd2 = NewDictionary()
script => {
msg("This is scriptA")
}
dictionary add(sd2, "scriptA", script)
script => {
msg("This is scriptB")
}
dictionary add(sd2, "scriptB", script)

You have to assign the script to a variable first, and then assign. But I think that it's easier just to have a holder object that holds all the possible scripts.

  <object name="ScriptHolder">
<scriptA type="script">
msg ("This is scriptA")
</scriptA>
<scriptB type="script">
msg ("This is scriptB")
</scriptB>
</object>

Then the dictionary builder would use:

      sd2 = NewDictionary()
dictionary add(sd2, "scriptA", ScriptHolder.scriptA)
dictionary add(sd2, "scriptB", ScriptHolder.scriptB)

If you really did want to create custom scripts on-the-fly (e.g. programmatically change what's in the script), then you can't do that. Scripts have to be known up front. They are not generated from strings, so you have no way to feed customized script code into an attribute or dictionary. But there should be a more data-driven way to do it, if you need that sort of customization. If that's the case, I can help if you can provide more details about what you're trying to do.

jaynabonne
28 Dec 2013, 21:18
Actually, there is a way to do inline script setting without assigning to a variable first. It just takes an intermediate function (as strange as that may sound):

  <function name="SetScript" parameters="dict, key, script">
dictionary add(dict, key, script)
</function>

Then you can do:
sd3 = NewDictionary()
SetScript(sd3, "landScript") {
msg("This is in landScript")
}

The trailing script on the SetScript call maps into the third "script" parameter in the function. It doesn't appear that "dictionary add" supports that syntax directly.

Entropic Pen
29 Dec 2013, 19:01
It's just what I needed!

Here's where I was (trying) to get at:
- Creating a dictionary of scripts that would hold actions:
-- A script for analyzing a planet's resources like Methane, Uranium, Lithium, etc.
-- A script for going from planet to planet.
-- etc.

And all those scripts would go into a dictionary. I would call those scripts in that dictionary using their keywords via a showmenu (all of which I've done already :D ). This is all for procedurally/randomly generated filler between larger story-centric stations and planets in "The Secrets of Planet X".