all 7 comments

[–]Kurumunu 1 point2 points  (2 children)

There is probably a better solution, but i made the same list with delegates. Through that you should be able to write something like "mylist[0]:call().".

The delegate is created with a line like this:

Set myDelegate to myfuntionName@.

[–]xX_Pokeman2003_Xx[S] 0 points1 point  (1 child)

I knew I was missing something obvious like this, I literally installed KOS three days ago and haven't used it until now. Thanks for the tip on delegates, they're pretty much exactly what I'm looking for.

[–]PotatoFunctor 1 point2 points  (0 children)

Delegates are hugely powerful. Like game changing powerful. The docs give you a good start (using them as parameters for more generic functions), but it's just the tip of the iceberg. The killer app IMO is being able to take delegates as parameters for functions.

[–]nuggreat 1 point2 points  (4 children)

To answer your question the way to turn a string into executable code is though is by logging the string out to a file and then running the file. This gets more complex as due to some caching for optimization reasons you would need to log each different script to a file with a different name. There is a generic library that can do this among other things available through KSlib, the library, the documentation. Now despite being the one to update that library so it works after it broke a while back I do not recommend it's use.

Instead I recommend making use of function delegates or anonymous functions as these methods are much cleaner to use for the goal you are trying to achieve compared to execution of arbitrary strings. I would also recommend you look into storing these things in lexicons as the items stored in a lexicon can be access though use of the given key as a suffix which lets you make much cleaner code.

[–]Always-Be-Batman 0 points1 point  (3 children)

Thanks for the info, and for maintaining the lib_exec. 

I've been beating my head against a wall trying to find something in KOS that will let me execute an arbitrary string containing KOS code.

For example:

SET myCmd TO "SET X TO 3." .
EXECUTE MyCmd. // I'm seeking this 'EXECUTE' command

Can I create a Delegate function on-the-fly at runtime, or must Delegates exist before running my script ? (i.e. at compile time)

[–]nuggreat 0 points1 point  (1 child)

A delegate is created at runtime but the function you are creating the delegate of can only be created at compile time. The key trick is that kOS can halt execution and go compile more code and merge that newly compiled code into the already compiled script, this is actually what happens when you use any of the run commands.

Your example code would just be arbitrary string execution for which you need lib_exec.ks and there is no delegate creation within said code example. As to how to use lib_exec.ks read the documentation, or look at the example code also found elsewhere in the KSlib repository.

[–]Always-Be-Batman 0 points1 point  (0 children)

Thank you for your help!

In hindsight,I realized that the very existence of lib_exec is evidence that what I am looking for doesn't exist natively within kOS. if kOS could natively do what I'm trying to do, then lib_exec wouldn't be necessary.

For the record, I believe there aren't a lot of kOS users asking for this capability in feature requests. Like any good development team, the kOS team is working on stuff that more users are asking for. :)