Scripts / Nearest Object?

Author
Message
GubbyBlips
User Offline
Joined: 14th Jan 2019
Location:
Posted: 11th Jul 2019 06:49
Hi there. I'm looking into the available scripts to see the technique on how to
find the nearest object on the map (from scripted entity). I don't want AI npc's,
just a regular entity-- maybe better to have a "name" set like "wall" in properties
or something to separate from others. I looked at ally and radar scripts-- whoa.

So what always gets me is GG throws me under the bus every time I try to use
global variables, and always says if I don't assign them {} they are nil.
And so I really stick with local variables. Are globals required for this-- I think probably.
If I can just get the coordinates to the nearest named object- I think that will do.

Here's some pieces of script that I am trying to get to collaborate together...
hmmmm.... any ideas? Thanks.

Attachments

Login to view attachments
PM
smallg
Community Leader
13
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 11th Jul 2019 14:45
utillib.lua has some functions at the bottom which can be used
looking at your script i guess you mean closest to the player?
so to continue your script you basically just loop through and store the distance between objects and check if it's within a minimum range - if it is then that now becomes the result and the distance if is at becomes new minimum range and repeat til the end of your list.
i.e.

i'm not sure but i think this may only be looping through dynamic objects.
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
GubbyBlips
User Offline
Joined: 14th Jan 2019
Location:
Posted: 11th Jul 2019 21:09
Hey, thanks for the response smallg, I'm sure that is the trick,
but I keep getting error that tdist is nil or that line there is an
"attempt to compare number to nil" error

I need more understanding on assigning globals because this seems
to be always the case when I attempt to use them.

I am going to try it after a computer reboot and see if it's still there.
PM
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 12th Jul 2019 00:08 Edited at: 12th Jul 2019 00:10
{} is a list btw.

Anything not declared as local is by default global and should be avoided if at all possible, i.e. in that script snippet smallg posted ddx, ddy etc should all be made local, in fact:


Is what it should look like.
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
User Offline
Joined: 14th Jan 2019
Location:
Posted: 12th Jul 2019 20:46
WOW! This is infuriating. So I struggled with this (I guess) simple script for
an hour or two hybridizing between smallg and AM because it never liked
several parts to either one. (that means I kept getting error codes.) Then I
validated small portions of AM script (particularly the >>for k, v in pairs( g_Entity ) do<<
portion, and yes, it worked, and so did the >>g_EntityElementMax<< function.

A note-- before this, I got the map to open error free, but my Text(x,y,z) debuggers
never appeared- like it was ignoring everything!

Well, after validating that small part of the code (which was NOT working a moment ago!!)
I went back to it, and without further changes-- suddenly it worked! (it ran) It's not perfect,
something is wrong, but at least it is running and the debugging text suddenly appears!
My COW!
You probably think that I also have Bigfoot cousins that I go picnic lunch with and talk about
their adventures on UFO's with... and maybe so. But this kind of thing has happened
before, I walk away from the computer for a day or so, and this script that wouldn't
function at all-- throwing out completely irrational error code behavior, the next day works!--
without any changes.

I've even come across the experience of it saying "unexpected symbol" or "nil value" or
some kind of error on line 81 -- I go to line 81-- it's completely blank-- it's a space.

This is my life with lua lately, the more complex the worst. I get breaks once in a while
without these glitches, but when they prevail-- I want to throw a brick through the computer!
Okay- happy GG scripting everyone. I just had to rant about that.
PM
Belidos
3D Media Maker
3
Years of Service
User Offline
Joined: 23rd Nov 2015
Location: Hertfordshire
Posted: 12th Jul 2019 22:16
Multiple test game loads will often over time result in some scripts not being removed from memory as they should, so even if you fix a script if you have been going in and out of test a lot the change might not work unt you restart gameguru.

Primary Desktop:
i7 7700,k NV1070 8GB, 16GB 3200mhz memory, 1x 2TB Hybrid, Win10.

Secondary Desktop:
i5 4760k, NV960 2GB, 16GB 2333mhz memory, 1x 2TB Hybrid, Win10.

Laptop:
i3, Intel 4000 series graphics, 6GB memory, 1x 500gb HDD, Win8.1.
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 12th Jul 2019 23:03
It would make it a lot easier to help you if you could post a script that doesn't work for you and then I can show you what you've done wrong.
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
User Offline
Joined: 14th Jan 2019
Location:
Posted: 13th Jul 2019 03:38 Edited at: 14th Jul 2019 18:19
Yes. Seems to be true.
Now we must do these, 1 is that the player is ignored when searching
for the nearest object. This script is for the script to find where is close
object to find object that is the nearest.

So I tried to ignore the player with:

if k ~= e and k ~= g_PlayerObjNo then ----- don't seek scripted entity or player

it seems like g_PlayerObjNo returns no value >> Text(g_PlayerObjNo) returns nothing.
I tried a slew of techniques to switch the search over to the nearest object:
('near' represents the entity #)

if _____conditions are met_____ then ----- go to next enemy

CollisionOff(near)
SetPosition(near,99, 699, 99)
SetEntityHealth(near,g_Entity[near]['health']-2) ----- damage object
Destroy(near)
end
PM
GubbyBlips
User Offline
Joined: 14th Jan 2019
Location:
Posted: 13th Jul 2019 05:15
Can I use this technique for a flag?

local isgone[] = 0
if isgone[k] ~= 1 then

where isgone is local and k is global
EXAMPLE:
for k, v in pairs( g_Entity ) do
if isgone[k] ~= 1 then letsgo[e] = "YES"

PM
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 13th Jul 2019 12:03 Edited at: 13th Jul 2019 12:11
Use Boolean values, i.e. 'isgone[ k ] = true', then you can simply say 'if isgone[ k ]'.

If you are in first person there is no player object and if you are in third person GetGamePlayerControlThirdpersonCharactere() gives you the entity id for the player character.

Edited to add -
Having reread the thread I think I see what you are getting at, you want the nearest entity to another entity, not the nearest to the player, so that would be:


Btw, in 'for k, v in pairs( list ) do … end' the k and v are both local, i.e. they are only in scope until the 'end' statement.
Been there, done that, got all the T-Shirts!
PM
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 13th Jul 2019 14:08
Alternatively, assuming you don't care about height, you could use the existing function in utillib:


Now elist will contain the entity ids of the 10 nearest entities in order of distance.
So elist[1] is the closest, elist[2] the next nearest etc.
You could loop through elist in order and find the first entity that is of interest with extra checks.


Been there, done that, got all the T-Shirts!
PM
3com
5
Years of Service
Recently Online
Joined: 18th May 2014
Location: Catalonia
Posted: 13th Jul 2019 18:36 Edited at: 13th Jul 2019 20:35
This code type on screen and write on a plain file, all the nearest entities ids.
AM's script based.


Items should be dynamic since static ones, returns nothing.

Edit: you can find nearest_List.txt file withing GG>Files folder.

If you don't need the txt file list so just comment these lines as follow:



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
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 13th Jul 2019 18:58 Edited at: 13th Jul 2019 19:00
Which is the same as:



Note that this will run every frame!
Been there, done that, got all the T-Shirts!
PM
GubbyBlips
User Offline
Joined: 14th Jan 2019
Location:
Posted: 13th Jul 2019 20:26
Aha-- bummer to me that I didn't first check your Utility libraries!
Thanks for the instructions.

PM
3com
5
Years of Service
Recently Online
Joined: 18th May 2014
Location: Catalonia
Posted: 13th Jul 2019 20:38
Quote: "Which is the same as:"

Not exactly.
Mine just copy 10 items on the txt file.
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
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 13th Jul 2019 22:04
U.ClosestEntities( 3000, 10, Ent.x, Ent.z )

The '10' means return the 10 nearest …
Been there, done that, got all the T-Shirts!
PM
3com
5
Years of Service
Recently Online
Joined: 18th May 2014
Location: Catalonia
Posted: 13th Jul 2019 22:23
Yes I know.
Referring to the code writting 10 values again an again, therefore you end up with a full filled txt file.
If you has for example 10 nearest items, let's say (4,1,3,2,5,8,7, and son)
you get in txt file:
Quote: "
1
4
1
3
2
5
8
7

1
4
1
3
2
5
8
7

1
4
1
3
2
5
8
7"


and so on. I've re-writed my code, so now fixed that issue. Now just copy 10 items, or whatever number of items, you want to.
Edited above code.
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
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 13th Jul 2019 22:27
Ah, that's what I meant by it being called every frame, the better solution would be to only do the whole thing once in a while. In fact if the entities are all static (as in not moving) then you only need to call it once.
Been there, done that, got all the T-Shirts!
PM
3com
5
Years of Service
Recently Online
Joined: 18th May 2014
Location: Catalonia
Posted: 13th Jul 2019 23:22
Quote: "the better solution would be to only do the whole thing once in a while. In fact if the entities are all static (as in not moving) then you only need to call it once."

Yah, that's the idea, now my code do so.
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
smallg
Community Leader
13
Years of Service
User Offline
Joined: 8th Dec 2005
Location:
Posted: 14th Jul 2019 00:00
Quote: "Aha-- bummer to me that I didn't first check your Utility libraries!"

i mean if only someone had reminded you in the very first reply...
at least you have a full explanation and extra options now though
lua guide for GG
https://steamcommunity.com/sharedfiles/filedetails/?id=398177770
windows 10
i5 @4ghz, 8gb ram, AMD R9 200 series , directx 11
AmenMoses
GameGuru Master
3
Years of Service
User Offline
Joined: 20th Feb 2016
Location: Portsmouth, England
Posted: 14th Jul 2019 01:08 Edited at: 14th Jul 2019 01:55
Funny, it's almost as if I predicted someone might need that function when I created the library, maybe I am psychic?

Btw, and I have offered this before, if there are any general utility functions that people need that I haven't catered for; let me know and I will add them.
Been there, done that, got all the T-Shirts!
PM
3com
5
Years of Service
Recently Online
Joined: 18th May 2014
Location: Catalonia
Posted: 14th Jul 2019 01:17
LOL
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
GubbyBlips
User Offline
Joined: 14th Jan 2019
Location:
Posted: 14th Jul 2019 18:23
Yes. The explanations are very great to know.
Thanks for all the responses.
PM

Login to post a reply

Server time is: 2019-07-18 01:42:05
Your offset time is: 2019-07-18 01:42:05