Announcement: Why not try our official idea generator for inspiration when making puzzles?


Caravel Forum : DROD Boards : Architecture : Scripting NPCs that follow an other NPC automatically
New Topic New Poll Post Reply
Poster Message
Dying Flutchman
Level: Smiter
Avatar
Rank Points: 406
Registered: 01-27-2017
IP: Logged
icon Scripting NPCs that follow an other NPC automatically (+2)  
Dear Drod Folk,

I am scripting a somewhat extensive project right now. Hope to put the first level into playtesting soon.

However, I run into a problem right now.

My main character 'radiates a glow' outwards to the 8 surrounding tiles. So the default script of the NPC generates 8 characters around it (basically just images).

Now here's the problem: when the NPC moves, the surrounding NPCs should move as well. My solution is to generate a new 'glow' each turn and let the old glow NPCs die.
This works like a charm, does exactly what it should.

However, by generating 8 net NPCs each turn for each of the main characters, the number of NPC quickly rises and at some point the game slow down considerably. Appearantly, letting the old NPCs die does not free up the game's memory.

Alternatively, I could hand-script these NPCs. The main NPC could set its coordinates using <X_whatever,Y_whatever>, and the glow NPC could just reference these vars.
The problem with this solution lies with hand-scripted. I want to place the main NPC and not bother with hand-tuning the glow NPCs. Also, I might want to actually generate the NPC in-game.

Is there any way to set a variable name dynamically? This would solve all my problems. I would then be able to define two X- and Y-vars with names that depend on the place of entity generation. The glow NPCs could then also initialize to those vars and track the main NPC location automatically.

Still bearing with me? I hope so! Because any thoughts on this would be greatly appreciated.

As it is, my NPCs work very nicely, but I should be really considerate about using only a small number (more than 4 is already tough on game speed in my setup).

Thanks!

____________________________
Autocorrect is not my friend. Apologies for the typos.

[Last edited by Dying Flutchman at 12-07-2018 07:43 PM]
12-07-2018 at 07:43 PM
View Profile Send Private Message to User Show all user's posts This architect's holds Quote Reply
Xindaris
Level: Smitemaster
Avatar
Rank Points: 1527
Registered: 06-13-2015
IP: Logged
icon Re: Scripting NPCs that follow an other NPC automatically (+3)  
Let's see whether I can understand what exactly it is you want...

If you only ever needed one of NPC that's supposed to be surrounded by glows in each room, then having it set global vars that the glows follow along with would work just fine. So I guess you're saying that you want to be able to have as many as you like?

I would guess that since what you're after is basically graphical in nature and not actually related to the gameplay behavior, the "easiest" solution might be using Image Overlays all managed by the actual character. But I can't help you with that because I've never really understood them.

If you want to definitely use characters then..well, maybe you could have the "glow" characters read off their orientation (there's one in each of the 8 directions, make its orientation the same as the direction it's in) and from that calculate where the "center" of the glows should be (where the actual character would be if it hadn't moved). Then, if the character is still there, great! If not, scan the 3x3 block within which it could possibly have moved to for an instance of that entity to determine where it moved to, and then have the glow move that way too. Just make sure that the glows have a processing order after the central entity. The only problem then is that if two of this central entity are ever standing next to each other and both move, the glows will behave pretty much chaotically and probably at least half will lose their "target" in favor of following the wrong guy, depending on the order in which the 3x3 block is processed.

Likely the only way to get truly, totally consistent behavior out of an implementation involving a bunch of characters is to do something like what I did for water barrel/oil barrel tracking in S A N D and Immolation, where you designate a "first", "second", "third", and so on and give each entity its own individual copy of the script along with its own individual variables ("centralguy1X", "centralguy1Y"; "centralguy2X", etc.), and have the glows that are supposed to be associated with the entity read off the correctly-numbered variables. This works and it becomes easy to copy up to the number of guys you put in a previous room and paste into the next one, and at least reasonably straightforward to add another, but it is admittedly kind of janky in nature and far less convenient than just plopping down a thing with a default script that always does what you want. The only thing this probably can't do is give some third entity the ability to generate arbitrarily many of the central guy/glow pairs, but it seems unlikely that any reasonable puzzle you could use that for couldn't be built in a way that doesn't require it.

____________________________
109th Skywatcher

Here are some links to Things!
Click here to view the secret text


[Last edited by Xindaris at 12-07-2018 08:32 PM]
12-07-2018 at 08:25 PM
View Profile Send Private Message to User Show all user's posts High Scores This architect's holds Quote Reply
Dying Flutchman
Level: Smiter
Avatar
Rank Points: 406
Registered: 01-27-2017
IP: Logged
icon Re: Scripting NPCs that follow an other NPC automatically (+1)  
Hi Xindaris,

You have understood my problem exactly. Thanks for thinking along here.

Now I thought of your solution to check nearby squares and just pair up with the nearest NPC. Dismissed that option for the same reason you have: chaos lurks when more entities are present.

I have actually seen your scripting in S A N D: yes, this would be a solution. However, I also dismissed that one already. It is kind of semi-manual, but not automatic enough for me. I'd then rather carry the burden of a slight game-speed impairment.

In fact, I do only need graphics. The glow interacts with the player and other elements, but this can be handled easily enough by the central NPC. In fact, it is implemented this way already.

So, I guess I'll have to look into image overlays. That could give me an elegant solution perhaps.

____________________________
Autocorrect is not my friend. Apologies for the typos.

[Last edited by Dying Flutchman at 12-07-2018 08:45 PM]
12-07-2018 at 08:43 PM
View Profile Send Private Message to User Show all user's posts This architect's holds Quote Reply
Dying Flutchman
Level: Smiter
Avatar
Rank Points: 406
Registered: 01-27-2017
IP: Logged
icon Re: Scripting NPCs that follow an other NPC automatically (+4)  
Update for those who might be interested.

I managed to combine the best characteristics of several solutions.

I do use a separate "ID" for each entity that is then used by the glow NPCs to track location. However, instead of defining multiple base entities to pick from, I actually let them find and define their ID on the fly. The main entity now places the glow NPCs all at the same location. The x-coordinate of this location tells them the ID of their 'master' entity, which they start to follow right away, like good little puppies.

I only need a single initializing character for this to work flawlessly. When that character is present, it's just placing NPCs galore.

Sound ridiculously complicated, but actually was very easy to do. If anybody else runs into a situation like this, I'd be happy to do some explaining of my solution.

____________________________
Autocorrect is not my friend. Apologies for the typos.

[Last edited by Dying Flutchman at 12-10-2018 06:56 AM]
12-09-2018 at 06:42 PM
View Profile Send Private Message to User Show all user's posts This architect's holds Quote Reply
New Topic New Poll Post Reply
Caravel Forum : DROD Boards : Architecture : Scripting NPCs that follow an other NPC automatically
Surf To:


Forum Rules:
Can I post a new topic? No
Can I reply? No
Can I read? Yes
HTML Enabled? No
UBBC Enabled? Yes
Words Filter Enable? No

Contact Us | CaravelGames.com

Powered by: tForum tForumHacks Edition b0.98.8
Originally created by Toan Huynh (Copyright © 2000)
Enhanced by the tForumHacks team and the Caravel team.