Scripts / Tutorial: Extending script functionality

Author
Message
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 11th Aug 2015 15:31 Edited at: 12th Aug 2015 20:44
So, you have a script but want to add some functionality to it. The main script for characters is ai_soldier.lua and even I can't understand every bit of it so modifying it is really a pain in the..., so I figured out a neat way to make it work in a bit different way while not actually touching ai_soldier.lua file.

In this example we want a guard which will stand still unless he is being shot at or he sees us with a weapon. Something like this:



Let's create a new file called guard.lua. Now for him to stay neutral we will need ai_neutral.lua and for him to attack the player we will need ai_soldier.lua, so let's require and initiate them in our new file:



Now we will add a simple code to make it call appropriate script when certain conditions are met. If we don't have a weapon in our hands (key 0) our g_PlayerGunID variable will be 0, and if we have a weapon it'll be greater than 0. Similarly, if our guard can't see us it'll have g_Entity[e]['plrvisible'] set to 0 and if he can see us it'll be 1. So let's just check if we have weapon (g_PlayerGunID greater than 0) and if guard can see us carrying the weapon (g_Entity[e]['plrvisible'] equals to 1). Since we are working with "plrvisible", make sure you set "View Cone Angle" and "View Range" on your soldier to some reasonable values (I have used 100 and 1000 respectively)!



That's basically it! When we get the weapon out and the guard can see us, it will call ai_soldier_main(e) and otherwise it will call ai_neutral_main(e). There's one thing though: If we wave a weapon in front of a guard and then holster it (in real life) I don't believe the guard will forget about it and with this code it will. So we need to set a flag for the guard to remember he has seen a weapon.

Create a global variable guard_flagged right below our require statements and initialize it to false



And then we need to modify our check to include this variable AND set it to "true" if our conditions are met:



Note the parenthesis, they are very important here. We want our guard to attack if he is flagged OR both latter checks are true (player is carrying a weapon and is visible). To make him remember, we also set guard_flagged[e] to true.

However, there's still one thing left: If we are behind the guard and he can't see us, and we take out our weapon and shoot at him, he won't respond. To make him respond we will simply place his strength to 100 and when it gets below 100 we will change guard's flag state. This isn't a perfect solution if we have more guards with different strengths, but it'll be fine for the sake of this example:



That's it now! So the main idea is to require the scripts we are going to use, initialize them and make our script call those scripts' main functions depending on the conditions we define. Whole script (without debug info) is attached.

Note: Initializing two or more complex ai_ scripts may cause conflict and unpredicted behavior. But you're on your own with that

P.S. For debug info (like in my video above) add this line of code right below the newly added code (health check):

Attachments

Login to view attachments
Kesstryl
9
Years of Service
User Offline
Joined: 29th Jul 2015
Playing:
Posted: 11th Aug 2015 19:31
This is brilliant! I love includes, and this is a great way to utilize them!
PM
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 11th Aug 2015 19:55
Yeah, it's all about re-usable code. Why copy and modify when you can simply include and call functions as needed
SoUlFaThEr
9
Years of Service
User Offline
Joined: 20th Jul 2015
Location:
Posted: 12th Aug 2015 12:01
This scripot does not work for me. on loading the testmap using it on 4 guards I get a rather large error screen complaining about loading the ai_nuetral.lua and ai_nuetral.dlls and all kinds of other related things......when i click ok on that it givers another error that there is no function main called guard..........

I have all the required scripts of course. and added yours as is.......

is it possible that this requires your displayed in signature OOP LUA framework?
I am afraid to use such things because the performance update may ruin them.
The only stupid questions are those that were never asked.
PM
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 12th Aug 2015 12:13 Edited at: 12th Aug 2015 14:15
No, the script is not using my OOP LUA framework, it's plain GG LUA. Just make sure you have ai_neutral.lua and ai_soldier.lua in your scriptbank folder. You wrote ai_nuetral not ai_neutral so double check that (it may be only typo here in forum, but who knows).

If it still doesn't work, please try to get text (or make a screenshot) of any error messages you are encountering.

btw. Feel free to use my OOP LUA framework, it's simple LUA, no dlls or anything... it can't be broken so easy and I'll try to keep up with GG development.

edit: Oh, I just remembered - it might be using *part* of my OOP LUA framework. It's actually just one line of the code and I keep it at the bottom of global.lua, but you may put it at the very beginning of your script (before "require" statements):

SoUlFaThEr
9
Years of Service
User Offline
Joined: 20th Jul 2015
Location:
Posted: 12th Aug 2015 14:44
well the neutral/nuetral was an error in the forum

I copied your script guard.lua and only changed the distance and added a --comment about what its doing
See attached screens.

You can also see in the one that my ai_neutral.lua and ai_soldier.lua both exist.


I actually thought the command was "Include" and not "Require". what is the real difference here?
The only stupid questions are those that were never asked.

Attachments

Login to view attachments
PM
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 12th Aug 2015 15:34 Edited at: 12th Aug 2015 16:58
Please see edit on my previous message:

Avram wrote: "edit: Oh, I just remembered - it might be using *part* of my OOP LUA framework. It's actually just one line of the code and I keep it at the bottom of global.lua, but you may put it at the very beginning of your script (before "require" statements):

"


The difference between include and require is that first one will not fail if included file is not found and the second one will fail.

Let me know if it works so I can update the code.
SoUlFaThEr
9
Years of Service
User Offline
Joined: 20th Jul 2015
Location:
Posted: 12th Aug 2015 20:27
ok awesome im doing this now but im knee deep in sound engineering a ton of voices right now from a script.

The only stupid questions are those that were never asked.
PM
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 12th Aug 2015 20:49 Edited at: 12th Aug 2015 20:51
OK, I have edited first post (and attached script) and added this line of code. Whoever users my OOP LUA framework does not need the first line of the script; however even if they have it - it shouldn't make any problems.

Now I might improve this script, but I'll keep it as is in this thread as this is tutorial topic, I might open new thread instead.
3com
10
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 13th Aug 2015 15:14
Nice tuto mate!

Quote: "This isn't a perfect solution if we have more guards with different strengths"


this may help, in fact it work for me.



3com
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 8.1 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics

PM
Avram
GameGuru TGC Backer
18
Years of Service
User Offline
Joined: 3rd Sep 2006
Location: Serbia
Posted: 13th Aug 2015 15:45
That's even better because they would then respond not only when hit, but also when they hear the gunfire.

You just need this line above your code to make it work:

Peri
Game Guru Backer
14
Years of Service
User Offline
Joined: 6th Jan 2010
Location:
Posted: 1st Sep 2015 22:50
Hi great script

Where I need to put the line to make them respond to gunfire?

thanks.
Kesstryl
9
Years of Service
User Offline
Joined: 29th Jul 2015
Playing:
Posted: 2nd Sep 2015 00:44 Edited at: 2nd Sep 2015 00:45
I don't think you need to add anything Peri, the includes call up the response to gunfire from the included scripts. Basically an include is attaching the "included" scripts to this one, so then the guard will respond to gunfire from the ai_soldier script when it's triggered in this one.
PM
Peri
Game Guru Backer
14
Years of Service
User Offline
Joined: 6th Jan 2010
Location:
Posted: 2nd Sep 2015 19:38
I downloaded the "guard.lua"

its without the responding to gunfire sound

I try to add the line "local EntObjNo = g_Entity[e]['obj'];
if AIGetEntityHeardSound(EntObjNo) == 1 then
guard_flagged[e] = true
end"

but its not works maybe cause I don't know where to add it.
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 2nd Sep 2015 20:07
life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11
Peri
Game Guru Backer
14
Years of Service
User Offline
Joined: 6th Jan 2010
Location:
Posted: 2nd Sep 2015 21:40
Still not working for me what I am doing wrong?
Old Larry
GameGuru TGC Backer
12
Years of Service
User Offline
Joined: 26th Apr 2012
Location: Bucharest, Romania
Posted: 3rd Sep 2015 10:14 Edited at: 3rd Sep 2015 10:45
Great script !!! Working for me



Peri wrote: "Still not working for me what I am doing wrong?"

What part of script doesn't work for you ?
Smile today, tomorrow could be worse

http://bestradiolarry.ro/fps/

"The best forum, game software, operating system or web platform, it's that software which can give you most of the features and speed, not just amazing graphics."
Peri
Game Guru Backer
14
Years of Service
User Offline
Joined: 6th Jan 2010
Location:
Posted: 3rd Sep 2015 15:52
The part they should to hear the gun fire sound

If they see my gun its works

if I shoot them its works

but if I shoot near them and they don't see me they should to hear me
smallg
Community Leader
19
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 3rd Sep 2015 18:39
works fine, you just need to be shooting kinda "at" them for them to hear it
i would suggest adding some extra code to adjust hp automatically so you dont need to set them all at 200 and also maybe an aggro update to alert nearby guards as an extra to the sound detection



life\'s one big game

windows vista ultimate

i5 @3.3ghz, 4gb ram, geforce gtx460, directx 11

Login to post a reply

Server time is: 2024-12-22 05:05:01
Your offset time is: 2024-12-22 05:05:01