Scripts / Getting Collision Details

Author
Message
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 17:11
Hi, everyone! I'm learning LUA scripting and wanna use the P.GetEntityObjectCollisionDetails( e ) function. And it doesn't wanna work in my script.

The values this function returns don't update. Where did I make a mistake here?
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Aug 2020 18:11 Edited at: 23rd Aug 2020 18:12
P.AddEntityCollisionCheck( e ) should be used first, this must only be called once.

Then you can use P. GetEntityTerrainCollisionDetails to get details about collisions of that object with the terrain or P.GetEntityObjectCollisionDetails( e ) to get details about collisions with other physics objects.

Both these functions return a list containing the collisions detected since the last time you called the function.

For terrain collisions the list is:
{ { x = <val>, y = <val>, z = <val> },
{ x = <val>, y = <val>, z = <val> } ...
}

This gives the world position of the collision with the oldest collision being the first entry and the latest collision the last entry.

For object collisions the list is:
{ { obj = <val>, x = <val>, y = <val>, z = <val>, f = <val> },
{ obj = <val>, x = <val>, y = <val>, z = <val>, f = <val> } ...
}

obj is the object Id of the object it collided with, x,y,z is the world position of the collision and f is the force of the collision.

When you are no longer interested in the collisions you should call P.RemoveEntityCollisionCheck( e )
Been there, done that, got all the T-Shirts!
PM
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 18:20
@AmenMoses
Thanks, I'm gonna experiment with this
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Aug 2020 18:21


There is an example of usage.



And that is it in action, the 'boing' noises are triggered by that bit of script.
Been there, done that, got all the T-Shirts!
PM
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 18:58
@AmenMoses
Thanks again!
My script works partly. When the object with this script collides with a tree, I have an error "attempt to concatenate an upvalue 'object_indic' (a nil value). I thought I did everything not to have a nil value in Text(). Can you, please, correct my code to make the Text() function show the latest object collided with, the coordinates and force.
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Aug 2020 19:18 Edited at: 23rd Aug 2020 19:23


Something like that, I haven't tested it btw.

I added a 200ms pause to allow GG to get up and running fully before adding the collision checking.
It only prints out the first collision detected from the list.
When using this in anger I suggest only checking for collisions every so often rather than every frame, maybe every tenth of a second or so should be enough for most uses.
Oh and text is only going to flash up briefly but you can fix that by copying the variables and printing them out every frame.
Been there, done that, got all the T-Shirts!
PM
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 19:33
@AmenMoses
Thank you! I'm gonna test it.
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 20:20 Edited at: 23rd Aug 2020 20:20
@AmenMoses
I've got an error on this line

Attachments

Login to view attachments
osiem80
5
Years of Service
User Offline
Joined: 24th Jan 2019
Location: Poland
Posted: 23rd Aug 2020 20:40 Edited at: 23rd Aug 2020 20:41
Didnt know there is a hover like that i gameguru, looks cool
PM
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 20:46
@AmenMoses
I changed P.GetEntityCollisionDetails( e ) to P.GetEntityObjectCollisionDetails( e ) but I have this nil error when trying to access colList[ 1 ].obj
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 20:49
osiem80
Me neither. Wanna get one
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 21:27
@AmenMoses
I changed P.GetEntityObjectCollisionDetails( e ) to P.GetObjectCollisionDetails( obj ) and have nothing. What is the difference between an object and entity btw?
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Aug 2020 21:48 Edited at: 23rd Aug 2020 21:50


Btw, the objectId is the id of the actual object in the game, i.e. the mesh. The entityId is a number given to dynamic entities so that the script can identify them.

g_Entity[ e ].obj gives you the objectId for an entity.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Aug 2020 21:53
It is P.GetEntityObjectCollisionDetails( e ) btw, if you look at physlib.lua you will see that basically it just convertes the entityId to the objectId and calls GetObjectCollisionDetails( obj ).

When called it clears the collision information so you cannot call it twice the way you were doing as the second call will find nothing and return nil.

Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 23rd Aug 2020 21:56
The hovercar belongs to someone who was making a game with it, I just provided the script.

So it's not really 'in GG' as such, i.e. not on the store.

But if you can acquire a similar model from somewhere it can be made to hover in GG in the same way.
Been there, done that, got all the T-Shirts!
PM
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 23rd Aug 2020 23:05
@AmenMoses
Spasibo! Thank you! It works!
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 25th Aug 2020 21:03
@AmenMoses
Hello! One more question! I've got this script so far

I made the entities with this script explodable. When the force is more than 0.02 the health of the entity becomes 0 and it explodes. But at the same time all of the entities with this script explode, too. I don't want it. Don't know what to do. And after having exploded the PromptLocal(e,"target") doesn't disappear! How can I correct it?
Thanks in advance!
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Aug 2020 21:35 Edited at: 25th Aug 2020 21:46
Last thing first, the coorddest_exit function will never get called, they are only for characters (ai iow).
Put the P.RemoveEntityCollisionCheck( e ) just before the Destroy(e) call.

f_detected is a global variable so will trigger in all entities running the script.

Change it to a local variable, i.e.
local f_detected = 0 just after _main
Been there, done that, got all the T-Shirts!
PM
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 25th Aug 2020 21:42
Thanks! Since I don't want to see the force numbers any more, I changed the script a little and it works.

I thought you could write local at the beginning and then just write the variables one after another separating them with a comma )
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Aug 2020 21:45 Edited at: 25th Aug 2020 21:46
Yes but those are local to the script, i.e. shared by all entities using the script!

Only variables declare local within the _main function are local to that entity.

But the other part of my reply still stands, move P.RemoveEntityCollisionCheck( e ) to just before Destroy( e) and delete the coorddest_exit function.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
8
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 25th Aug 2020 21:58
So to recap for anyone earwigging:

Any variable declared without the 'local' will automatically be global, i.e. visible to all scripts on all entities.

Avoid globals like the plague as they are very slow and dangerous! **

Any variable declared as local in the body of a function is not visible outside that function.

Any variable declared outside a function body will be visible to any entity using that script.

** by dangerous I mean that if you accidentally define a global variable with the same name as one in another script Lua will simply replace that existing variable with your new one! (Lua assumes you know what you are doing!)

As an example of this if in your script you did this:

ai_bot_state = nil

Then you would find any ai scripts would probably crash and burn and it may not be obvious from the error messages why!
Been there, done that, got all the T-Shirts!
PM
IvanKats
3
Years of Service
User Offline
Joined: 28th Jul 2020
Location: Russia
Posted: 25th Aug 2020 22:23
@AmenMoses
Thanks! I did that. And thank you for that valuable information!!!

Login to post a reply

Server time is: 2024-04-24 19:38:06
Your offset time is: 2024-04-24 19:38:06