createMacro()

Thoughts, Help, Feature Requests, Bug Reports, Developing code for...

Moderators: dorpond, trevor, Azhrei

Forum rules
PLEASE don't post images of your entire desktop, attach entire campaign files when only a single file is needed, or generally act in some other anti-social behavior. :)
Post Reply
koorimusha
Kobold
Posts: 9
Joined: Wed Jun 17, 2015 3:56 pm

createMacro()

Post by koorimusha »

So I've been attempting to do something that seems a little bit complicated. It's also a little hard to explain, but I shall try my best. When using the createMacro() function, it allows you to freely write macros and assign it to a token or wherever. Here's a problem, I'm trying to make the macro creation a little more "customizable". For example, I would use the input() function and type in very specific values of my desire. Easily, I could type in a custom name that depends on said input() function by doing something like this:

Code: Select all

[h:status=input("name")]
[h:abort(status)]
[h:createMacro(name, etc)]
As you can see here, writing this code will allow you to give the macro any name you desire by putting in a custom name into the name field when the initial prompt pops up. But, let's say you also wanted to include more values such as healhp and healmp to alter how much HP or MP one would recover? As I've tested, the new macro would not be able to read values from another macro that technically would not exist. By the time the new macro is created, the old macro's values no longer exists because healhp and healmp are solely dependent on what you type in the input() function. I will give a visual example below:

Code: Select all

[h:status=input("healhp","healmp","name")]
[h:abort(status)]
[h:createMacro(name,
"[h:healing1=json.get(macro.args, 'healhp')]
[h:healing2=json.get(macro.args, 'healmp')]
[h:jsonData=json.set('{}', 'healhp', healing1, 'healmp', healing2)]
[macro('healing@Lib:rules'): jsonData]"
)]
As you can see above, the code displayed is absolutely wrong. While there technically would be no error message, the code in the new macro will not function at all because of how the quotations are written and the json.get() values being nonexistent. Originally, what I wanted to do is, whatever I type in the healhp and healmp field for input(), that would be the amount of HP and MP recovered when the new macro is clicked on. (Assume that the [macro("healing@Lib:rules"): jsonData] function does everything correctly.) I'm wondering, is what I'm trying to do impractical? Is there a work around for this? Perhaps a completely different function that will do what I'm trying to do? Or must I stop all progress and code this in a completely different way? If more information is required, please let me know. I know this might be rather confusing especially with my way of explaining things, but hopefully it is not too much. Your feedback will be much appreciated!

Also, before I finish off my questions, I wanted to know an extra bit of information, but when using the removeMacro() function, would it be possible to make a macro delete itself as soon as it performs its necessary function? If so, how would I go about doing this without getting the "removeMacro(): You do not have permission to call this function." error, if possible? Thanks again.

User avatar
metatheurgist
Dragon
Posts: 364
Joined: Mon Oct 26, 2009 5:51 am

Re: createMacro()

Post by metatheurgist »

Try:

Code: Select all

[h:status=input("healhp","healmp","name")]
[h:abort(status)]
[h:createMacro(name,
"[h:jsonData=json.set('{}', 'healhp',"+healhp+", 'healmp',"+healmp+")]
[macro('healing@Lib:rules'): jsonData]"
)]

koorimusha
Kobold
Posts: 9
Joined: Wed Jun 17, 2015 3:56 pm

Re: createMacro()

Post by koorimusha »

Hey, thanks a bunch. Learning about "+this+" really helped me and opened up more opportunity for more flexible code. I didn't even know that was a thing that I could do, despite seeing it on the wiki's page almost everywhere. From what I can gather, it will allow you to write the value of a string within the next macro. Is this its only use? Are there any limitations to it? What are its other uses? I got a lot of questions and I could learn maptools all day, but you don't have to sit here and explain that to me if it's too much of a hassle because I pretty much learned what I needed to.

Also, about the createMacro() function, I figured it out. I just had to add "playerEditable=0" to my code and all was grand. Kind of a newbie mistake, I feel, but you gotta learn sometime. Anyway, thanks.

User avatar
metatheurgist
Dragon
Posts: 364
Joined: Mon Oct 26, 2009 5:51 am

Re: createMacro()

Post by metatheurgist »

""+x+"" is basic string manipulation. It's mostly common to a lot of programming languages. The trick is understanding the context to apply it to get the value of a variable instead of the name of the variable. That's something you learn to do as a coder, whether you self-teach or take a course.

Post Reply

Return to “MapTool”