Day Triggers: During AI turn, and detecting End Turn

Maps and the art of mapmaking.
jwallstone
Leprechaun
Leprechaun
Posts: 21
Joined: 21 Aug 2008

Day Triggers: During AI turn, and detecting End Turn

Unread postby jwallstone » 12 Oct 2009, 05:30

I've been playing around with the editor, and I can't seem to figure out how to do these two things:

1. The New Day trigger seems to only work during the player's turn. Anyway to do something during an AI turn?

2. Is it possible to detect and run a function when the player Ends the turn?

Thanks for any help!

User avatar
darknessfood
Round Table Hero
Round Table Hero
Posts: 4009
Joined: 02 Jul 2007
Location: Netherlands
Contact:

Unread postby darknessfood » 12 Oct 2009, 06:42

Perhaps it's nice to say what editor you are talking about (HoMM 1, 2, 3, 4 or 5)...
You can either agree with me, or be wrong...

Franzy
War Dancer
War Dancer
Posts: 362
Joined: 07 Sep 2007
Location: Moscow, Russia
Contact:

Unread postby Franzy » 12 Oct 2009, 07:25

1. Correction: new day trigger triggers not on human player's turn, but on the turn of a player who moves first. In singleplayer it's always human, in multiplayer - differs :).

What do you need to do on AI turn? You can try using GetCurrentPlayer function, it might help.

2. Not possible directly. Can be arranged with GetCurrentPlayer, endless thread and a cycle :) Only works if there are more then 1 player.

jwallstone
Leprechaun
Leprechaun
Posts: 21
Joined: 21 Aug 2008

Unread postby jwallstone » 12 Oct 2009, 16:32

Sorry, I keep forgetting that this is a forum for all HOMM versions, so I forget to write which game I'm talking about.

Thanks Franzy for the help. That helps clarify it. I believe I should be able to make it work, either with the endless loop or with a workaround just using the Day Trigger.

Thanks for the great help!

jwallstone
Leprechaun
Leprechaun
Posts: 21
Joined: 21 Aug 2008

Unread postby jwallstone » 13 Oct 2009, 02:08

One more question:

It seems that the Heroes V engine does not support the pairs() and ipairs() functions that are part of Lua for generalized 'for' loops iterating through lists. Is there someway to get this to work, like some workaround or a way to manually write these functions?

Thanks.

Franzy
War Dancer
War Dancer
Posts: 362
Joined: 07 Sep 2007
Location: Moscow, Russia
Contact:

Unread postby Franzy » 13 Oct 2009, 04:59

Yes, H5 lua engine is somehow cut. What does "pairs()" do?

jwallstone
Leprechaun
Leprechaun
Posts: 21
Joined: 21 Aug 2008

Unread postby jwallstone » 13 Oct 2009, 05:17

pairs() just produces index, value pairs for a table, to use in a 'for' loop.

as in:
a = {10, 20}
for i,v in pairs(a) do
--some code, i are indices (1,2), v are values (10,20)
end

Is there another way to iterate through a table?

I see this example in the Editor documentation under the function GetObjectCreaturesTypes:

types = GetObjectCreaturesTypes( "my_garrison" );
for index; type in types do
if type == CREATURE_PEASANT then
...
end;
end;

But the odd thing is, this "index; type" syntax isn't in Lua, (I don't think it is at least) and gives an error if I try to use it in game.

I think I'll just try to use a workaround with explicitly defined 'for' loops, like:

for i = 1, length(array) do
...
end

jwallstone
Leprechaun
Leprechaun
Posts: 21
Joined: 21 Aug 2008

Unread postby jwallstone » 13 Oct 2009, 05:26

By the way, is there a way to directly edit the Map Script without opening the editor? It gets time-consuming to have to always close the game, then open the editor, then vice versa just to make every change.


EDIT: I just found out that Heroes used 0 as the first index of arrays. Very annoying, seeing as how all of Lua uses 1-based arrays.

EDIT: One more question: When the game console displays an error, it gives a line for the error like:
"last token read: xxxx at line N in string "DoString script"

Of course, the line N given doesn't correspond to the line in the script when viewed through the map editor, but it always seems to a certain number off. Is there an offset to subtract from N to give you the real line number?

Franzy
War Dancer
War Dancer
Posts: 362
Joined: 07 Sep 2007
Location: Moscow, Russia
Contact:

Unread postby Franzy » 13 Oct 2009, 12:14

I see this example in the Editor documentation under the function GetObjectCreaturesTypes:

types = GetObjectCreaturesTypes( "my_garrison" );
for index; type in types do
if type == CREATURE_PEASANT then
...
end;
end;
No, index referring works fine in HoMM5, there is just an error in docs. GetObjectCreaturesTypes returns 7 values instead of an array. It should be used like this:
c1,c2,c3,c4,c5,c6,c7 = GetObjectCreaturesTypes...
By the way, is there a way to directly edit the Map Script without opening the editor? It gets time-consuming to have to always close the game, then open the editor, then vice versa just to make every change.
Well of course! Map script is stored in the MapScript.lua file in the map file (it's zip-archive actually). You can extract it, open it in any external program (I personally use SciTE), edit and then pack back. But there is even better way!
Just place lua file in the data folder, run the game, stat your map, open console and enter command @doFile('myscript.lua') and your script will be executed! This is very useful for debugging, as you can run the game and edit the script simultaneously. Forementioned method with extracting/packing only works if both game and editor are closed.
I just found out that Heroes used 0 as the first index of arrays. Very annoying, seeing as how all of Lua uses 1-based arrays.
It's even worse, my friend. Functions return 0-based arrays while manually created are 1-based. That's stupid, but we have to live with that :(
EDIT: One more question: When the game console displays an error, it gives a line for the error like:
"last token read: xxxx at line N in string "DoString script"

Of course, the line N given doesn't correspond to the line in the script when viewed through the map editor, but it always seems to a certain number off. Is there an offset to subtract from N to give you the real line number?
Don't know about lines in editor but line number in SciTE always fit.

jwallstone
Leprechaun
Leprechaun
Posts: 21
Joined: 21 Aug 2008

Unread postby jwallstone » 13 Oct 2009, 14:51

Oh man! You are amazing!

Thanks for all your help. This is great, I can use SciTe now.

The 0-based thing sucks. I'm glad I know about it now, it was annoying to debug. And thanks for the tip on GetObjectCreaturesTypes, that helps a lot.


Return to “Mapmaking Guild”

Who is online

Users browsing this forum: No registered users and 6 guests