Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

How do you convert a string to a function call?

A topic by dusty-deck created 8 hours ago Views: 21 Replies: 2
Viewing posts 1 to 2

from https://beyondloom.com/decker/lil.html :

"Types and Conversions

There are 8 types of value in Lil: numbers, strings, lists, dictionaries, tables, functions, interfaces, and the nil value.

"


I have a string type that I want to convert into a function call. How do I do it?

Starting with:

"canvas_16.paste[images_color.widgets.canvas_1.copy[]]"

How do I get:

canvas_16.paste[images_color.widgets.canvas_1.copy[]]

?


I've tried and failed with the following:

> eval["canvas_16.paste[images_color.widgets.canvas_1.copy[]]"]  # bugged.
> "canvas_16.paste[images_color.widgets.canvas_1.copy[]]"[0]     # "c". Not what I wanted.
> first "canvas_16.paste[images_color.widgets.canvas_1.copy[]]"  # "c". Not what I wanted.

select a canvas, e.g. canvas_16, then in listener:

> me.event["paste" "images_color.widgets.canvas_1.copy[]"]        # nil. (No event named "paste"?)


Help?

Developer

It would be best to explain more broadly what you're trying to accomplish. Is the entire statement dynamically constructed from text, or are you simply trying to, for example, use indirect references to the widget names?

n1:"canvas_16"
n2:"canvas_1"
cw:images_color.widgets
c1:cw[n1]
c2:cw[n2]
c1.paste[c2.copy[]]

The eval[] function can be used to evaluate a dynamic snippet of Lil; it returns a dictionary with any syntax error information (if the snippet was malformed) or a result and variable bindings:

eval["2+3"]
# {"value":5,"vars":{}}

By default, eval[] is only aware of variable bindings (including definitions of built-in functions like eval[] itself, deck parts, etc) that you explicitly pass into it as a dictionary- its second optional argument. This makes eval[]-ed code "safe by default"; it is sandboxed to play only with what you give to it.

eval["a+b" ("ab" dict 11,22)]
# {"value":33,"vars":{"a":11,"b":22}}
z:99 eval["z+5"]
# {"value":5,"vars":{}}

If you do want to include local bindings, a truthy third argument will opt into including them; this means everything that was in scope from the caller's perspective will be available to the code snippet:

a:11
b:33
eval["a+b" () 1]
# {"value":44,"vars":{}}

As always, when exploring unfamiliar scripting functionality I recommend trying things step-by-step in the Listener.

Per your question, something like the following might work:

eval["canvas_16.paste[images_color.widgets.canvas_1.copy[]]" () 1].value

But, again, please take a step back and make sure this is what you really want to do. As problem-solving tools go, eval[] is something of a sledgehammer, and you may only need scissors or a pair of tweezers.

Make sense?

(+1)

Thank you, your top code example contained a solution I was looking for.

I'm not certain what was confusing me, and attempting to explain my confusion might not be useful...

I guess I'd been stuck trying to reference a widget with the dot notation, e.g., home.widgets.canvas_8, not realizing that I should have been using the bracket notation, home.widgets["canvas_8"]. The latter is clearly happy with a string, while the former is not.

You have revived me from mental collapse, much appreciated!