Heroes of Might and Magic Community
visiting hero! Register | Today's Posts | Games | Search! | FAQ/Rules | AvatarList | MemberList | Profile


Age of Heroes Headlines:  
5 Oct 2016: Heroes VII development comes to an end.. - read more
6 Aug 2016: Troubled Heroes VII Expansion Release - read more
26 Apr 2016: Heroes VII XPack - Trial by Fire - Coming out in June! - read more
17 Apr 2016: Global Alternative Creatures MOD for H7 after 1.8 Patch! - read more
7 Mar 2016: Romero launches a Piano Sonata Album Kickstarter! - read more
19 Feb 2016: Heroes 5.5 RC6, Heroes VII patch 1.7 are out! - read more
13 Jan 2016: Horn of the Abyss 1.4 Available for Download! - read more
17 Dec 2015: Heroes 5.5 update, 1.6 out for H7 - read more
23 Nov 2015: H7 1.4 & 1.5 patches Released - read more
31 Oct 2015: First H7 patches are out, End of DoC development - read more
5 Oct 2016: Heroes VII development comes to an end.. - read more
[X] Remove Ads
LOGIN:     Username:     Password:         [ Register ]
HOMM1: info forum | HOMM2: info forum | HOMM3: info mods forum | HOMM4: info CTG forum | HOMM5: info mods forum | MMH6: wiki forum | MMH7: wiki forum
Heroes Community > Heroes 5 - Modders Workshop > Thread: HoMM5 code tester/pseudo-Debugging
Thread: HoMM5 code tester/pseudo-Debugging
Zortag
Zortag

Tavern Dweller
posted August 13, 2007 08:56 AM

HoMM5 code tester/pseudo-Debugging

One of the major issues I have had with making maps in HoMM5, is that there are absolutely no facilities for testing/debugging a script.  I have spent so much timegoing back and forth between the Map Editor, to add/modify some code, then have to exit the editor, start up HoMM5, select the map, wait for it to start up, and finally turn on the console, just to find I forgot another comma, misspelled a word, used the wrong variable the wrong way, etc.  I have already given up several times in sheer frustration.  But I have finally found a way around that problem!

HoMM5 seems to have diabled a great many functions that are a part of the LUA language; however I did, out of sheer luck, stumble upon an undocumented, but supported, function that enables one to run some code that is in an external text file, as if the code was in the map file.  Therefore I have figured out a method for debugging or testing code, while within the game, but with the ability to change the code at any time without leaving the game.  Thus, enabling one to finally tweak the action and the syntax to get a properly working function, that can then be put into the map file, and be guarenteed to work.

To do this one needs to create a trigger that will execute a function, whose only purpose is to run the code in the external text file.  I have been using the OBJECT_TOUCH_TRIGGER, but one could just as easily use either of the REGION_ENTER_..._STOP_TRIGGER's, basically any trigger that can be tripped easily, multiple times and have no other impact on the game.

First - Create the external text file for the test code, you can put it anywhere on any hard drive, you just have to know the full file specification (complete path).  This file MUST be an ASCII text file, be careful, Windows like to make UNICODE files, and UNICODE files just don't work!  I have only tested files witha ".lua" extension, others may or may not work.  The following example file works and on my system it's full file spec is "C:\Documents and Settings\Zortag\My Documents\Games_Info\HeroesOfMightAndMagic\HoMM_5\Script\Test_File.lua", it merely prints 3 (actually 5, but two are empty) lines of text to the console.


--          T E S T _ F I L E . L U A
--
--  Pseudo-Debugger for HoMM5
--

   print( "\n** Begin Test/Debug" );
   print( "  No tests defined!" );
   print( "** End Test/Debug\n" );


Next - Once you have your test code file created, and know its full name, open the HoMM5 Map Editor, open a new map or an existing one.

Next - Create the object or region that will fire off the trigger.  Drop an "Eye of the Magi" (not "Hut of the Magi") onto the map near your staring hero (it can be anywhere, but why waste all the time getting to it just to test some code).  Give it a name that is unique on the map (left-side, object property sheet), for example I use the name "objTestDebugger", what the name is doesn't matter as long as it's unique and you can remember the name you gave it.

Next - Create the trigger and its "receiver" function.  Open up the Map Properties gizmo, and select the Script tab to open the map's script.  Then create a new function be the receiver of the trigger, as follows, remember to put in the full file spec of the code text file you created in the first step; also note that each "\" in the file spec MUST be replace with "\\", or it won't be able to find the file (looks like it is a 'nix environment). (the doFile argument is really on one line)


--  Trigger receiver function to execute an external text file.
function ftrTestDebugger()
doFile( "C:\\Documents and Settings\\Zortag\\My Documents\\Games_Info\\HeroesOfMightAndMagic\\HoMM_5\\Script\\Test_File.lua" );
end;


Then - Create the trigger code, as follows (remember to change the object name to whatever you called it, smae wiht the name of the receiver function.


-- Setup the script tester/debugger
Trigger( OBJECT_TOUCH_TRIGGER, "objTestDebugger", "ftrTestDebugger" );


Next - Save the map and close the map editor.
Next - Start up your text editor and open the code text file you created in the first step.  Start HoMM5 and select the map you just edited, turn on the console, and move your hero to touch the "Eye of the Magi", and just like magic you should seel the following lines appear on the console:

** Begin Test/Debug
 No tests defined!
** End Test/Debug

Finally - Alt-Tab to get out of the game and back into the editor, change the code in the text file.  Alt-Tab back into HoMM5,move the hero to again touch the "Eye of the Magi", and the new code executes; warning, errors and all.

The code in the text file runs "as a function" within the game, so any and all variables in the game map are available for use/display/modification etc.  Once you've fully developed a function externally, you can then copy-paste the code from the text editor into the Map Editor's script gizmo.

Errors
If the trigger sucessfully fires, but all you get on the console is an error that says something about "last token read" and that token is a lower-case "y" with two dots atop it, then that means that the text file is UNICODE and not ASCII.  Save the text file in ASCII format and try it agin.

If the trigger sucessfully fires, but you get an error message that says the it can't find the file.  Look carefully at the file spec in the error message and there are probably missing "\" characters (remember, each of them MUST be doubled in the doFile argument), or you didn't put in the full file spec correctly.


 Send Instant Message | Send E-Mail | View Profile | Quote Reply | Link
Maurice
Maurice

Hero of Order
Part of the furniture
posted November 03, 2007 06:05 PM

The doFile command! I've seen it several times and I've even seen multiple .lua files calling other .lua files, but never connected the dots as you have.

This is great!

Very nice work!

 Send Instant Message | Send E-Mail | View Profile | Quote Reply | Link
sfidanza
sfidanza


Promising
Supreme Hero
posted November 04, 2007 10:48 PM

Nice explanation indeed.

However, I have 2 comments (actually one comment and one question):
- if you place your test script in your data/ directory, the file path is simpler, as it's only "Test_File.lua". That was the silly comment.

- why not just open the console and type (assuming the above file path)
@doFile("Test_File.lua")

 Send Instant Message | Send E-Mail | View Profile | Quote Reply | Link
Jump To: « Prev Thread . . . Next Thread »
Post New Poll    Post New Topic    Post New Reply

Page compiled in 0.0234 seconds