Archived

This topic is now archived and is closed to further replies.

HarryC

Some "how-to" questions about LUA

Recommended Posts

A battle in my game takes place in a separate battle screen (away from the game world) and will be much like the D&D type games where a group of monsters battle against the group of players. I have selected Lua as my scripting language to control my monsters. This is my first time using Lua and I have some questions that I cannot find answers to via google. Each monster type (eg. orc warrior or orc healer) in my game will have a Lua script created to control it during battle. I understand how to create this script file and run it from C code. What I don''t quite understand is how to have these individual scripts running for each monster (probably in their own thread). Each creature will make decisions based upon what the other monsters in the battle are doing, so they can''t operate in isolation. Each usable object and spell in my game will also have its own Lua script. This script is meant to override different decision points provided in the template for each monster. What I can''t figure out is how to "merge" the item AI into the monster AI. I think an example is necessary here, so assume for a second that an orc warrior has the following AI code defined for an action during battle:
     If (winning_battle) {
            keep_fighting();
     } 
     else {
           run_away();
     }
 
Now assume that the orc has a healing potion. I would like this to modify the current script running for the orc such that a check is now done to see if the orc should heal himself before running away. I don''t want to modify the templates to be aware of all possible objects -- this makes it a nightmare to add a new item. What I''d like to be able to do is create a new item or spell, define the AI for that item/spell, and not have to do anything else. Make sense what I''m asking?

Share this post


Link to post
Share on other sites
It seems like what you are asking for is a generic system that can accommodate all your possible objects, even ones not created yet. Modifying the existing script each time a creature acquires an object is not really practical.

This is really just about providing a generic algorithm that items and situations and creatures all slot into without alteration. In turn this probably involves deciding on some basic interfaces. Here''s a very basic alteration to your script:


If (winning_battle) {
keep_fighting()
}
else {
run_away_now = true
object = find_obj_to_use_when_losing()
if object ~= nil then
run_away_now = object:use()
end
if run_away_now == true then
run_away()
end
}


You will of course have to make alterations so that that compiles (it''s been a while since I wrote any working Lua code). But the basic idea is that you write a function that looks for items to use when you''re losing, and that in turn will call a standard ''good_for_losers'' function on the object or something. Then you can use the relevant object, should one be found, and the return value can tell you whether you still need to run away or not.

Basically, it''s just building in a bit more flexibility into your script by adding in an interface so that the script can ask objects if they''re useful at the current time. New objects just have to provide the relevant functions.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites
quote:
Original post by Kylotan
It seems like what you are asking for is a generic system that can accommodate all your possible objects, even ones not created yet.


If only I could have said it so well. :-)
quote:

Modifying the existing script each time a creature acquires an object is not really practical.


Can you elaborate on this point? Assuming I don''t need the code precompiled, Lua seems to support the dynamic type of modification that I''m after. Assuming that the templates for all creature types have common "sections", I can just do a simple replace of the section(s) affected by an item and reload the script.

For example, a creature with a bow will have completely different targetting logic than a creature with an axe. However, let''s assume the axe is magical and allows the bearer to attack twice in one round. Given a "static" approach to the AI code, I would have to know in advance that it is possible for a weapon to allow for more than a single attack a round, but in a dynamic approach I could just replace the "attack" part of the code so that the two-attack feature happens automatically.

quote:

This is really just about providing a generic algorithm that items and situations and creatures all slot into without alteration. In turn this probably involves deciding on some basic interfaces. Here''s a very basic alteration to your script:


If (winning_battle) {
keep_fighting()
}
else {
run_away_now = true
object = find_obj_to_use_when_losing()
if object ~= nil then
run_away_now = object:use()
end
if run_away_now == true then
run_away()
end
}


You will of course have to make alterations so that that compiles (it''s been a while since I wrote any working Lua code). But the basic idea is that you write a function that looks for items to use when you''re losing, and that in turn will call a standard ''good_for_losers'' function on the object or something. Then you can use the relevant object, should one be found, and the return value can tell you whether you still need to run away or not.

Basically, it''s just building in a bit more flexibility into your script by adding in an interface so that the script can ask objects if they''re useful at the current time. New objects just have to provide the relevant functions.


I see where you are coming from, and that could work, but I think that the code required would become very messy once all decision points have been put in. I''m hoping that my approach is possible, and don''t mean to poo-poo your advice. Perhaps someone can authoritativly state that what I what to do is impossible with Lua and that will settle the question for me right there.

Share this post


Link to post
Share on other sites
I''m sure what you want to do is possible in Lua. I just think it''ll become more messy than the alternative. Surely replacing a certain section of code can''t be any cleaner than having a standardised piece of code of comparable length that won''t change? What about when you have 2 items that both need to alter the same section of code - how will they work together? If one takes precedence over the other, how will you decide which, and when you lose one of those items, how will you know what to put back into the template? And doesn''t all this make the items very dependent on the layout of the template, making the basic script harder to change?

If you are determined to go down this route of direct code-modification then all you need to do is store the code as a string and call lua_dostring() on the modified version. This function is in lauxlib.h. (The funny thing is, the version 5.0 manual says that dostring() is deprecated and replaced by loadstring - but the C API doesn''t define a function called loadstring, only the Lua API does. Odd.) Issues associated with that are more to do with the Lua language than AI specifically and you may receive more help (should you need it) over on the Scripting Languages and Mods forum.

[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]

Share this post


Link to post
Share on other sites