How to: Check if inv item used before NPC gets too close

Started by Grim, Mon 22/10/2007 22:47:36

Previous topic - Next topic

Grim

I need help with this as quickly as possible.
Ok. I'm a bit stuck right now.
In the scene in my game character walks into a room. There is a short dialogue with a NPC there and then this NPC walks towards character. This is a time for player to do something quickly to prevent being killed. When NPC reaches player he's supposed to kill him.  I was going to use region but it looks like it only works for player character( player walks onto region) and as I'm supposed to give player some time to act I really don't know how to solve this... So please HELP!!!:)

GarageGothic

The easiest thing would be to put a statement in your repeatedly_execute to check how far the NPC is from the player, or (in case the player can't move during the scene) just check whether the NPC has reached his walkto destination.

Something like:

Code: ags
fuction repeatedly_execute {
   if ((npcname.x - player.x)*(npcname.x - player.x)+(npcname.y - player.y)*(npcname.y - player.y) < 25) { //25 is the squared value of the wanted distance, here I chose 5 pixels
      npcname.Say("You're dead meat, player");
      }
}


If the player is static, there's really no need for the whole trigonometry bit, but it should still work. You should also put in a conditional to avoid the check running before and after it's needed.

Grim

Thanks, I'm tryin' it now.
Erm....ANy chance you could explain it a bit more? I'm dumb when it comes to scripting and I think I've done a lot of it already in the game but this is still a bit unclear. IS that x supposed to be player's x coordinate where he stands as he enters the room? There is no need for me to make player move around in this particular moment. All that matters is that he has few seconds to use inv before NPC reaches him.
I know, I make myself look like a fool but I really apreciate your help.

GarageGothic

player.x returns the current x coordinate of the player character, wherever he is at that moment. You're not supposed to substitute it with anything else. The only thing you should change is npcname, which should be changed to the script-o-name of the NPC character. And of course you can experiment with the number (25) to make the distance greater or smaller.

The whole math part is just simple trigonometry (Pythagoras) to calculate the distance between the player and the NPC. Since the player character is static, that's not really necessary (you could just check if the NPC reached his destination x and y coordinates), but the code is a bit more flexible this way. You could for example have an NPC chasing the player like in 7 Days a Skeptic.

Scummbuddy

A) Don't double post. I removed your unnecessary bump post. We'll try to help as fast as we can.

B) Don't type in all caps. This is poor net-iquette. It's rude and implies yelling.

C) Use descriptive titles. This helps when others are searching the forums for past help.
---------

Now, according to your new information, you could simply set up a timer to start when the NPC is "coming to get him" and "when timer expired" (the NPC has arrived) and if the character has used item with whatever before timer has expired..... do something.

- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

Grim

I'm sorry. I didn't mean yelling, I just got really annoyed with scripting 'cos every time I'm trying to do something more complicated my brain is melting... It's quite funny with AGS that it requires from the maker to be a great storyteller, programist and artist at the same time. When it comes to programming I suck, and I know it.... But still I'm quite proud of what I've achieved so far.

Thanks for all the help so far.

And that timer sounds like a good idea too. I used the command pause command processor few times but it stops everything. I nver realised that there is such thing as timer you can set for a certain amount of time while things are happening. How do I use the timer then?

Khris

Quote from: Grim Reaper on Mon 22/10/2007 23:54:36It's quite funny with AGS that it requires from the maker to be a great storyteller, programist and artist at the same time.
Creating an adventure game requires those things, AGS just makes the job much easier.

Look up SetTimer and IsTimerExpired in the manual.

Grim

I must be doing something wrong here.
I decided to go with the whole timer option. So I make my NPC move without waiting for move to finish and then run following script:
SetTimer (1, 240);
if (IsTimerExpired(1)==1) 
    {NewRoom (18);}

and it bloody doesn't work! Pardon my language... but nothing happens when 6 seconds passes. NPC just stops at the end of his route and stands there as if the bloody script wasn't there. 240 corresponds to 6 in game seconds, am I right?
Please tell me if I've messed it up or what.

Ashen

Where is that code?
Go back and re-read the manual (or any of the threads about timers already posted on the forums) - IsTimerExpired only returns true at the exact moment (game loop) the Timer expires, which means it needs to be checked in repeatedly_execute (the manual isn't too clear here, but that's what the forum's for). Checking it immediatly after you start the timer obviously won't do anything - the Timer hasn't had a chance to start properly, nevermind expire. (And if you're calling SetTimer in rep_ex - well, of course it's not expiring :))
Also, what version are you using? NewRoom is WAY out of date... (Not that that would affect the Timer.)

240 is 6 game seconds, at the default game speed (40 loops/second). If your game speed is different, it might be more or less than 6 seconds - try X*GetGameSpeed() (where X is the number of seconds you want), for a little more accuracy.
I know what you're thinking ... Don't think that.

Grim

I'm using AGS 261.
Both set timer and iftimerexpires I put in first time player enters the room one after another... so I guess that's the reason it doesn't work. I've never really used repeatedly execute so I'm gonna read up on it.
Also it's quite amazing how much you can do with the basic options in AGS. This is the first time I have to complicate things and I've been ags user for quite some time... It's just there is always many ways in which you can do things, almost like in a good adventure game. You just have to find the way that is most aproachable and easiest to you. It's a bit like if when english is your second language you learn to say things using words that you know, in your very own way... And after a while you do it without thinking to much about it but it's still there somewhere, in the back of your head. I guess this particular problem I encountered with ags now is something I really need your help with..
So if I understand right what I have to do is to:
1)Set timer when player enters the room
2)In rep_execute put iftimerexpires like I did before
3)and straight after in r_execute put NewRoom or whatever command is used these days

And will that work?

Khris

If by "straight after" you mean "inside the if(IsTimerExpired(X))-block" then yes, it will work.

NewRoom is the correct command for AGS 2.6.1, the current command is player.ChangeRoom.

Ashen

Quote from: Grim Reaper on Thu 25/10/2007 20:17:00
I'm using AGS 261.

That explains it - 2.61 is way out of date too, so as Khris said NewRoom is fine. There's no need to update if you don't need any of the features of the newer versions, but it might save time to mention that you're using an older version in any future posts. (Most people will provide help for the current official version - 2.72 - which might not work for you.)
I know what you're thinking ... Don't think that.

Grim

I couldn't get this timer to work, even after I put istimerexpired in rep_execute!
So I decided to go with GothicGarage's option and input the following in the repeatedly execute of the room:

if ((AXE.x - player.x)*(AXE.x - player.x)+(AXE.y - player.y)*(AXE.y - player.y) < 25) {   
     AXE.Say("You're dead meat, player");
      }
Where AXE is the script name of a npc.

But the error message keeps popping up- parse error in expr near "14". What's that all about? Where did I go wrong?

Ashen

I'm guessing 'AXE' is the Script name of the character, and that it's character number 14, right?

This is partially what I was talking about in my last post - GarageGothic's code wants the Script-o-name of the character, which doesn't exist pre-2.7. You need to use the character array directly to check character's coordinate, e.g character[AXE].x, character[EGO].y, character[BOB].x, etc. That, you should've been able to find by looking up character.x in your version of the manual. You'll also need to change AXE.Say to the appropriate command (DisplaySpeech(AXE, "You're dead meat, player"), IIRR) - although you couldn't have looked that up as the manual won't contain commands from future versions. (You'd have gotten an 'undefined token' error, when the script got that far.)
Also, you might not be able to use the player pointer - if you change the player character at any point, I don't think 2.61 updates it (unlike the post-2.7 versions) so it'll always refer to the character that WAS player at the start of the game.

Updating old code to work in new versions is pretty common - backdating code to make it work makes a change :)

What exactly did you end up using for the tmer code, and what was the problem? (Running at wrong time, still not running at all, etc.)
I know what you're thinking ... Don't think that.

Grim

I went with GarageGothic's option and with Ashen's advice it all works great! Thanks! Now it's even better than I thought it would be. I still don't know about the timer though... I think I'd find it very useful if I knew how to get it to work. I set timer in player enters room. Then in rep_execute I put iftimerexpires and apropraite action afterwards but nothing happens. I guess this doesn't really matter anymore that much 'cos I solved my problem but if you could say something more about timers it'd great.

Well, anyway big thanks to you guys!

Khris

There really isn't anything more to timers.
Post your code and we will tell you where the error is.

SMF spam blocked by CleanTalk