A couple of coding questions

Talk about whatever topic you'd like, RPG related or not. (But please discuss things related to our software in the Tools section, below.)

Moderators: dorpond, trevor, Azhrei

Post Reply
evilC
Cave Troll
Posts: 49
Joined: Thu Mar 21, 2013 3:29 pm
Location: London, UK

A couple of coding questions

Post by evilC »

OK, I am starting to pick up MT, but there are a few things that are hampering me. I thought I would ask a few questions...

1) From another thread, so I don't hijack it:
wolph42 wrote:by the way, ANY variable attached to the token can be used for this trick. Using the property type is pretty invasive, you could also use a state or a property or even a hidden property.
OK, could you explain, or at least direct me to docs here? I have tried to set variables on tokens, and I do not get errors, but the variables do not seem to hold when I then access that token from another macro.
The only way I can find to store data on a token is to use setProperty or setLibProperty. In fact, I am having real problems altering token properties from lib functions. If I for example create a bunch of tokens, the only way I can move them around is to set the update property when I do the copy - once I create the token, I cannot alter it's properties from the same function that created it.

2)Is it possible to move a token to the current viewport somehow? Or get viewport coordinates?

3) is there a quick way to get/set values in a JSON tree? eg this.that.whatIwant = 100
If not, has anyone written a function to do this? eg setjson(json_data,"this.that.whatIwant",100)

4) Are there any docs explaining in detail the [h:], [r:], CODE:{ etc syntax in depth and concretely? The wiki docs say that h or r must always be followed by a colon, but if commands only work like [h,if.. or [r:, if...

5) What level of IDE integration does MT have? I found some docs on getting notepad++ to work, but I am not sure *what* is meant to work. Some of the files in the instructions were dead links, so I could not complete the setup for np++ - Can it be set up such that you click edit macro, and the macro appears inside np++, and when you hit save in np++, it saves the macro into the campaign?

Enough for now, I am sure I can add more later.

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: A couple of coding questions

Post by CoveredInFish »

1) you can store values in properties. These dont have to be defined in the token property type. You can create any property using setProperty(). If you use a unique prefix you'll have very little to fear with this approach (all properties created by my macros are named "cif.something" or even "cif.project.something".

2) I dont think so.

3) there are the user created json.pset/json.pget by lmarkus

4) H, R, CODE, IF are roll options. A roll [] can have options. If so they are written at the beginning, separated by comma , from eacht other and separated by colon : from the "content" of the roll - or the actual function/code.

5) There is no real IDE integration for MT-script! But there is a drop-in that lets you import/export all macros from a token to a text editor using copy-paste (in a single action, not per macro). This is a tool started by me but vastly advanced by aliasmask .. search for RPEdit. Using that dropin you'll come as close to that workflow as you'll get.

Also by aliasmask has been done a set of code highlighting and syntax auto-completion (?) for notepad++. I loved that, but its Win only. There is some code highlighting around for VIM or something like that ...

HTH

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: A couple of coding questions

Post by wolph42 »

what he said 8) .

1) requires some elaboration. To edit a token property you;ll need to know that there defined and hidde properties. Defined show up when you double click on the token and check the properties. Hidden can only be accessed by get/setProperty.
The defined properties can be accessed by:
(assuming that 'variable' is a token property of the token "tokenname":
getProperty("variable", "tokenname")
OR
token("tokenname"): getProperty("variable")
OR
switchToken("tokenname")
getProperty("variable")
OR
switchToken("tokenname")
variable

evilC
Cave Troll
Posts: 49
Joined: Thu Mar 21, 2013 3:29 pm
Location: London, UK

Re: A couple of coding questions

Post by evilC »

CoveredInFish wrote:1) you can store values in properties. These dont have to be defined in the token property type. You can create any property using setProperty(). If you use a unique prefix you'll have very little to fear with this approach (all properties created by my macros are named "cif.something" or even "cif.project.something".
Can anyone tell my why, when you copy a token, I cannot seem to alter the token properties except through the update feature of the copy command? This only allows editing of x/y, name etc.
CoveredInFish wrote: 3) there are the user created json.pset/json.pget by lmarkus
Sign-up required for download :( Thanks anyway, but I think I will make my own and put it on my site.
CoveredInFish wrote: 4) H, R, CODE, IF are roll options. A roll [] can have options. If so they are written at the beginning, separated by comma , from eacht other and separated by colon : from the "content" of the roll - or the actual function/code.
Ah, makes sense now - thanks :)
CoveredInFish wrote: 5) There is no real IDE integration for MT-script! But there is a drop-in that lets you import/export all macros from a token to a text editor using copy-paste (in a single action, not per macro). This is a tool started by me but vastly advanced by aliasmask .. search for RPEdit. Using that dropin you'll come as close to that workflow as you'll get.

Also by aliasmask has been done a set of code highlighting and syntax auto-completion (?) for notepad++. I loved that, but its Win only. There is some code highlighting around for VIM or something like that ...

HTH
Yeah, one of the download links for RPTool Notepad++ was broken :(
I didn't really understand it either, lots of @@ etc in the text, but I got highlighting going.
Could someone explain to me what the workflow is meant to be?

evilC
Cave Troll
Posts: 49
Joined: Thu Mar 21, 2013 3:29 pm
Location: London, UK

Re: A couple of coding questions

Post by evilC »

And i think I had some problems getting token manipulation via ID working as well. To try and solve my couldn't-edit-after-copying problem, I tried switching to the ID of the token that was passed back by the copyToken function, but I couldn't get that to work either.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: A couple of coding questions

Post by wolph42 »

1. note my earlier post.
2. copytoken: you cannot update a copied token after copytoken. longs story why, but you can't. Its ONLY possible when:
a) the macro is finished and then you run a new one
b) you put a break in the macro and then continue. The only break that exists is: input()
in case you find this out later: deferred macro call does NOT work.

so:
copyToken
input()
edit properties

is the flow. I usually make use of the pause() macro for the break. But preferably design my macro such that the input() appears naturally.

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: A couple of coding questions

Post by CoveredInFish »

1) ah yes, sorry, I've forgotten to cover that point. wolph said the relevant bits ... its one of the quirks of the script language and is caused by timing when tokens are distributed over the network.

3) oh ... didnt noticed that. Since the latest download i'm aware of is in my dropbox anyway I can provide a link: MT1.3.61_WhisperHandoutsMapMove01.zip

5) Sure thing. The @ stuff is some meta-language aliasmask and me invented to denote macro beginning, name, properties and such stuff. You dont need to handle that by hand.

The workflow is ... you drop the dropin in your campaign and "install" it (copy a specific macro to campaign macros, reload or run "onCampaignLoad"). Next you can select a token and run that "edit" macro RPEdit comes with. A dialog will appear where you can load the text of all macros on that selected token into an textfield. Now copy-paste that text into your favorite text editor - notepad++ is a good one, not only for the MTscript support.
Edit your code, leave the meta-tags (@@macroName and alike) as they are. When you're done, copy-paste the code back into that textfield and import it via the dialog. The macros will get recreated.
I'm not currently coding in MT a lot, so I might have forgotten the correct lingo here or there, but thats essentially how its done :)

meshon
Cave Troll
Posts: 75
Joined: Fri Mar 26, 2010 10:54 am
Location: AB, Canada

Re: A couple of coding questions

Post by meshon »

evilC wrote: Sign-up required for download :( Thanks anyway, but I think I will make my own and put it on my site.
I understand your reluctance, but I will just say that there are some functions there that will really save you time. I decided to sign up just to download the functions and they are really worthwhile.

Does anyone know if it would be allowable to just post the code here in the forums for the individual functions? Or would that be disrespecting the hard work of Lmarkus?

cheers,
Meshon

Edit: CiF comes through for the solution! Just ignore me then :oops:

evilC
Cave Troll
Posts: 49
Joined: Thu Mar 21, 2013 3:29 pm
Location: London, UK

Re: A couple of coding questions

Post by evilC »

Thanks for providing a link, but I still could not seem to get his code to work - at least, not just by dragging the lib token in and pushing data to his json commands.
Also, I had trouble understanding his code, and it was rigged for using "/" separators, like a directory structure rather than "." as is normal in JSON.

So I wrote my own.
After many, many hours of wrestling with the quirks of MT, jsonLib is working! I can finally do jl.set(json_data,"this.that.theother",10) !!

I think my code is a bit inelegant in places, but once I have a chance to tidy up and comment, I will post it up.

Regarding comments, I learned the hard way that commenting code out does not disable it. I saw some remarks somewhere that this was the case, but is there a "safe" way to enter textual comments?
I was thinking something like <!-- 'This is a comment, it can include things.that.appear.to.be.commands("even.this")' -->

As long as I keep the same format of command, and the same result, I figure I can now store my data using JSON much more easily in my code. If someone can work out more elegant / efficient code for my set and get routines, I get an upgrade :)

User avatar
CoveredInFish
Demigod
Posts: 3104
Joined: Mon Jun 29, 2009 10:37 am
Location: Germany
Contact:

Re: A couple of coding questions

Post by CoveredInFish »

Nice project :-)

EDIT: i dont make much use of lindays tools but I think they should work. So maybe you'd learn something from getting it working too. If you'd be interested we could figure that out, first idea: for the install process of a dropin/lib.token you usually have to run a macro "onCampaignLoad" by hand or save&reload the campaign file - have you done so?

@comments
yes, you actually use html comments that are only hidden, there are no real comments in MTscript.

There are two work arounds:

1) you use an hidden-roll-option with just a string in it - and by convention this string too is written like a html comment. This does get executed and so it has a small influence on runtime. Its really just small, so usually you're fine, but especially in large, nested loops you should consider that.

Code: Select all

[h: "<!-- this comment is better -->"]
2) you can hide output from user defined functions (see parameters of Wiki: defineFunction()) Practically you disable the output to chat pipe a macro usually has - so you can use plain html comment just fine. To set the return value you define macro.return. To create output to chat you have to use Wiki: broadcast().

This is actually a very good style as it leads you to separate data processing and output creation. Search for code by aliasmask in the forum to find examples.

evilC
Cave Troll
Posts: 49
Joined: Thu Mar 21, 2013 3:29 pm
Location: London, UK

Re: A couple of coding questions

Post by evilC »

Thanks for that.
As I lay in bed last night trying to get to sleep, I realised what lidsay's code was doing that threw me - of course it is a recursive function.

Now when I realised this I thought that yes, this was maybe a smart move because in order to set a leaf node of a json tree, you are going to need to drill down to the leaf, set the new value, then re-assemble the tree as you recurse back out the function.

However... thinking about it some more, I am not quite so sure.

Surely if you use a recursive function to in effect store each level of the tree as you drill down, if you are trying to set a leaf node say 10 layers down the tree, by the time you drill down into the 10th depth, you have 10 copies of that key in memory.

Currently, my code starts at the leaf node you are setting, then issues a json get for the parent, then a json get for the parent of that, etc...

So in other words, more memory reads, but as it drills "out", it is only ever holding one chunk of data at a time.

Maybe some pseudo code will help to explain what I am saying:

jd = {JSON TREE - contains this.that.theother = "large chunk of data"}

Normal method
============

LOOP 1
tmp = get(jd,"this")
tmp = get(tmp,"that")
out = json.set(tmp,"theother","large chunk of data")

LOOP 2
tmp = get(jd,"this")
out = json.set(tmp,"that",out")

In this version, lots of memory reads, but it is only used briefly then replaced with another chunk.

Recursive
========

LOOP 1 - IN
tmp = get(jd,"this")

LOOP 2 - IN
tmp2 = get(tmp2,"that")

LOOP 2 - OUT
out = json.set(tmp2,"theother","large chunk of data")

LOOP 1 - OUT
out = json.set(tmp,"that",out")

Notice in the recursive version, that tmp and tmp2 both contain a copy of the large data chunk and both need to stay in memory at the same time until the whole operation finishes.

It appears to me that recursive would use more memory but less CPU, whereas normal would be more CPU intensive, more memory access, but less memory overhead.

Isn't heap memory a big issue in MT? Would that not make recursive not so smart a choice?

evilC
Cave Troll
Posts: 49
Joined: Thu Mar 21, 2013 3:29 pm
Location: London, UK

Re: A couple of coding questions

Post by evilC »

I put up an initial version of libJSON in this thread.

Could someone move the above question regarding memory into that thread maybe?

Post Reply

Return to “General Discussion”