Scripts / Some questions on my dubious code here.. (I'm an absolute newbie)

Author
Message
Phodis
8
Years of Service
User Offline
Joined: 2nd Feb 2016
Location:
Posted: 10th Jun 2020 00:57
Hi guys.... I wanted to drop a simple AI code onto an object into GameGuru. I have a few questions on my "Idiot AI" I have written if anyone could recommend anything to add onto this that would be great... I'll post the code first then ask the questions..
...........................................................
-- TrackPlayer
function trackplayer_init(e)
end
function trackplayer_main(e)
PlayerDist = GetPlayerDistance(e)
if PlayerDist > 1000 then
MoveForward(e,40)
end
if PlayerDist < 1000 then
RotateToPlayerSlowly(e,4)
end
if PlayerDist < 700 then
LookAtPlayer(e)
MoveForward(e,60)
end
if PlayerDist < 400 then
FireWeapon(e)
end
end
..........................................................................

Yeah I know real basic beginner stuff.. I come from Game-maker and the code is completely different so I am trying to piece together some basic AI to get to grips with LUA. You will notice the closer the player gets to the attached object for this code, the more aggressive it gets.
Firstly off the cuff... does anyone know why... when I attach this code to an inanimate object like say an ammo-box... it ~works..but spins like crazy!.. I found when I went to the properties..and changed "IsImobile" to NO it faces the player without spinning... but obviously the problem is then it wont actually move...where I can attach this same code to a character object, and the movement is always smooth. NO SPINNING erratically....and Although they don't animate they do track the player which is cool.

I tried for HRS to get a simple thing to work here, but I can't work it out so far.... what I want to get working is like the following (obviously the following doesn't work...but it's how I ~thought it might have.).

if PlayerDist>100 and playerdist<400 then <<<< can anyone show me the right context here please?
FireWeapon(e)
end

Another question...

I had some code in there as well to kill the object if it got to close to me...

If PlayerDist<10 then
Destroy(e)
end

And while this worked the object just comes back when I move away after it is destroyed!!... like it is not ~really destroyed to begin with.
I want to be able to destroy something for good! >
Ohh crap I forgot to ask.... is there a way to attach an item to a character?. Like just giving it the same co-ords, even keeping it invisible until it's called.. the plan was to make an ammo-box fly from the enemy at me as fast as possible when i get in range (basically shooting projectiles at me).

I attached something like the following code to an ammo crate and stuck it in the game.... but it moves so slowly (also it spins as it's not a character object for some reason exactly like the problem I had up above).

If PlayerDist<400 then
LookAtPlayer(e)
Moveforward(e,400) <<<< adjusting this speed seems to make so little difference...always seems to go a max of 80-100 no-matter what.
end even after adjusting the speed of the object in the properties to match what I put in the code here.

... I'm trying to get an object to act like a missile I guess would be the best way to put it.
If anyone has any ideas, it would be greatly appreciated.
Cheers!
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 10th Jun 2020 17:47
Pretty much all this is fairly easy once you get into the jiff of things.
For starters, for testing--
Inside the init function (one line above end ) add a; CollisionOff(e)
and see what happens?
Then in properties turn physics on = no. This is for testing again.

------ I don't know what FireWeapon(e) does- I think that's just for
characters that are assigned an actual rigged weapon.
If you make the box explodable=yes in properties and you destroy it,
it should explode // actually I think it's SetEntityHealth(e,0)
PM
GubbyBlips
5
Years of Service
User Offline
Joined: 14th Jan 2019
Location:
Posted: 10th Jun 2020 17:51
Next, you can get player coordinates with...
g_PlayerPosX
g_PlayerPosY
g_PlayerPosZ
g_PlayerAngY

You are browsing through the Global.lua file?? Not all things are explained--
unfortunately, but you might be able to guess at a few.
PM
3com
9
Years of Service
User Offline
Joined: 18th May 2014
Location: Catalonia
Posted: 10th Jun 2020 23:38
Silly question, have you tried the bow and arrows that are in stock?
Laptop: Lenovo - Intel(R) Celeron(R) CPU 1005M @ 1.90GHz

OS: Windows 10 (64) - Ram: 4 gb - Hd: 283 gb - Video card: Intel(R) HD Graphics
cpu mark: 10396.6
2d graphics mark: 947.9
3d graphics mark: 8310.9
memory mark 2584.8
Disk mark: 1146.3
Passmark rating: 3662.4

PM
Phodis
8
Years of Service
User Offline
Joined: 2nd Feb 2016
Location:
Posted: 11th Jun 2020 17:23
Hi guys, thanks for the responses.
GubbyBlips... when I played with my Ai and didnt include the line FireWeapon(e) ... the (cartoon rabbit with a gun), didn't fire at me... but with this line he does. Scared the crap outta me when it first happened as I wasn't expecting it to work lol.
I tried messing around with both the physics and colliosion modes; pretty cool thank you for that. And especially for the player position co-ords.... now that will be handy for a lot of things I imagine!.

3com... Do you mean the out of the box bows and arrows... yes I did try them. To me they look like they are just guns?.. in the sense the arrows immediately arrive at the targets with no arcing of the arrows themselves on the way to said target. My hopes were for a bow and arrow that could be edited so I can see the arrows flying at the target..even watching them on their way as I strafe....that kinda thing. In all honesty I have not looked at editing their code yet tho...just in case they actually do function this way already but are just going so fast they cant be seen.... But I will dabble into it when i learn a little more. ~getting there

The whole reason I wanted to get a "Bow and arrow" style "missile" was to make shooters where I could dodge incoming trajectiles and fire back... be it laser guns/ship to ship missiles/bows and arrows...any of that, just to tinker with.
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 11th Jun 2020 18:26 Edited at: 11th Jun 2020 18:29
I did a darts throwing script a while back.

Obviously you need the dartboard and darts to go with it but they don't belong to me, you might find them on the GraPhiX free models thread.

At least it demonstrates how to do it.
Been there, done that, got all the T-Shirts!

Attachments

Login to view attachments
PM
Teabone
Forum Support
17
Years of Service
User Offline
Joined: 8th Jun 2006
Location: Earth
Posted: 11th Jun 2020 18:33 Edited at: 11th Jun 2020 18:41
Quote: " is there a way to attach an item to a character?. Like just giving it the same co-ords, even keeping it invisible until it's called.. the plan was to make an ammo-box fly from the enemy at me as fast as possible when i get in range (basically shooting projectiles at me)."


You could make an item script that starts with Hide(e) in the initial function. Have it triggered to appear when the enemy dies. You will need an "exit" function added to your AI script. This when if your AI reaches 0 health/strength it will move off the main function and go to exit. In that function, you can call a global variable to say its dead and have the item use something like a if enemydead == 1 then Show(e) end

You will need the item to use collision off and Set Position to match the enemy AI if you want it to move with the enemy but invisible. Otherwise, you could just make the item sit somewhere on the map and wait to be called to transport. Though this means setting it to AlwaysActive.

I played around with something similar a few years ago just playing around with decals and particles:

Store Assets - Store Link

i7 -2600 CPU @ 3.40GHz - Windows 7 - 8GB RAM - Nivida GeForce GTX 960

Login to post a reply

Server time is: 2024-04-25 13:56:28
Your offset time is: 2024-04-25 13:56:28