Running script from script library based off value in dataset or document tag(Macros?)

So I have finally gotten back to working on the Fallout themed 2D20 DND campaign built in the makers edition of Ignition. I’m starting to dig in to how I want to the inventory system to work, and I think I’ve come up with something but I don’t know if it’s possible. My character sheets as well as most items are built off of document tags. Is there a way to read a string value off of the sheet and then execute a script or list of scripts. The hope is that this will save me a bunch of redundant coding i.e. one item might increase health by 6 points, but another different item could reduce them by 3. I’ll also explain a bit how I think it should work(in my head anyways).

Basically I’d have a button to look at the value of the item selected from inventory then run a script along the lines of:

tag=system.tag.read("[default]DM/Characters/CaddyMan").value.toDict()
tag['Inventory']['Bag'][2]

The hope is that this would run the script stored in the list but obviously it just returns my ‘script’ as a string. Is there any way to do something similar to this?

Hi @Jordan_Richardson, from what I remember you can’t call scripts dynamically from python, which is what I think you are trying to do?

You can call a script dynamically using an expression (from an expression binding), but I don’t think this would be a good solution. It would be better to create a generic function that uses specific parameters to perform changes to the inventory and then return the result IMO. Hopefully that makes sense :sweat_smile:

Sure you can. Python functions are first class objects. They can be put into dictionaries and lists, but more importantly, they can be retrieved dynamically from a script module using python's getattr built-in function.

In other words, instead of storing the script itself in the document tag, store a function name from a project script. Then you can do:

getattr(path.to.script, tag['Inventory']['Bag'][2])(some params)
3 Likes

exec() or eval() ?
image

Blegh. Dangerous.

1 Like

true, but this is just for a game, idk how much security maters here

Apologies, you are completely correct. Back to the books…

I assume the danger is in the risk of there being an ‘injection attack’ similar to what can happen in SQL? Victor is right though, this is just for a game hosted on my home network with 4 people of whom none code. Also, I will be the only one adding ‘Items’ to the master list so as long as I don’t goof somewhere we should be alright.

I’d still do it the other way. Setting up context for exec() or eval() is annoying.

1 Like

Yeah, ive used getattr() before and its really nice and flexible for structured code.

But if he wants to make a new item on the spot with a new effect, he could directly code it in the string…
Ofc then he needs to be really good at coding and knowing his stuff, because it will break easy xD

Or maybe combine different predefined functions. Like Heal and Buff…
But probably would be better to split up the effects in an array and trigger all of them in order with getattr()

I’m back on my home gateway now, and I can see what you were talking about with the context on exec() . It’ll take a little bit of creativity on my part as far as structuring goes, but I can see this working. Thanks Phil!

1 Like