[Solved] Moving NPC in Background

Started by ShinjinNakamura, Tue 18/06/2013 19:04:42

Previous topic - Next topic

ShinjinNakamura

Salam,

Iam trying to move an NPC up and down in the background in a constant loop, and have applied the following code in the room script file:
-----------------------------------------------------
Code: ags

// room script file
function room_load()
{
cBoram.y=370;
}

function repeatedly_execute() 
{
cBoram.y+=1;
if (cBoram.y=380) cBoram.y=370;
}

-----------------------------------------------------
Everytime I run it, the following error message appears:
_____________________________________________________
Failed to save room room1.crm; details below
room1.asc(10): Error (line 10): Parse error in expr near 'cBoram'
______________________________________________________
line 10 is  the last one with "if"
c.Boram is the NPC



Scavenger

Code: AGS
// room script file
function room_load()
{
cBoram.y=370;
}

function repeatedly_execute()
{
cBoram.y+=1;
if (cBoram.y==380) cBoram.y=370; // Comparitives always use ==, not =.
}


You almost had it, but when declaring a new value for a variable, you use =. If you compare a variable, you use ==.

ShinjinNakamura

Unfortunately when I run the code now, the following error message appears:
____________________________________________________________________________
An internal error has occured, please note down the following information. If the problem still persists, post the details on the AGS Technical Forums. (ACI version 3.21.1115)

Error: prepare_script_error: error -18 (no such function in script) trying to run 'room_RepExec' (Room 1)
_____________________________________________________________________________
I wanted to post this in technical forum, but I assumed there was a mistake in the script itself.
Is there something I should add to the global script too?

Crimson Wizard

Quote from: ShinjinNakamura on Wed 19/06/2013 14:47:10
Error: prepare_script_error: error -18 (no such function in script) trying to run 'room_RepExec' (Room 1)

This happens when you type in a function name on Room Events pane, but do not have (e.g. occasionally deleted or renamed) such function in the script.
You should make sure that the function is named properly (same way) in both Events list and script text.

Phemar

Yep, some functions such as a room's repeatedly_execute need to be linked to the Room Events pane.


Go to the events pane. If there's something already written there, erase it and click on the '...' button.

It should jump to the room's script file and create a function like this:


Now just copy your script into that function.

ShinjinNakamura

Aah, I see. Both have to be linked. That solves the problem, thanks.

Oh wait, one more thing. Can multiple script functions fall under the same event list.
As in for "repeatedly execute", could I link it to two things in the script?

Crimson Wizard

Quote from: ShinjinNakamura on Wed 19/06/2013 15:55:59
Oh wait, one more thing. Can multiple script functions fall under the same event list.
As in for "repeatedly execute", could I link it to two things in the script?
You can link same script function to multiple events (by typing same name into respected fields), but you can't link multiple script functions to same event.

If you need the latter, you can make another function and call several functions from that:
Code: ags

function call_multiple_functions()
{
   function1();
   function2();
   function3();
}

Khris

Just a note:
You can add a repeatedly_execute function to a room script, but it won't get called automatically. (it will in every other script, but room's have their own event for this).
What you can put in room scripts: on_key_press, on_mouse_click, on_event, repeatedly_execute_always.

ShinjinNakamura

#8
Oh no. Although my NPC is moving repeatedly like I want it, but now my main character does not respond immediately.
Iam using eBlock style, but when I use eNoBlock, although my main character moves but my NPC stands still then.
Here is my code:
-------------------------------------------------------
Code: ags

function room_AfterFadeIn()
{
cBoram.y=370;
}

function room_RepExec()
{
cBoram.Move(705, 380, eBlock, eAnywhere);
if (cBoram.y==380) cBoram.Move(705, 370, eBlock, eAnywhere);
}

---------------------------------------------------------------
Does this have something to do with what Khris was saying about the room script?

Khris

#9
No.

There are two problems here: you are calling the first move every single loop, regardless of the circumstances.

At least in theory, this is how to do it:
Code: ags
function room_RepExec()
{
  if (cBoram.y == 370) cBoram.Move(705, 380, eNoBlock, eAnywhere);
  if (cBoram.y == 380) cBoram.Move(705, 370, eNoBlock, eAnywhere);
}


The other problem is that afaik, doing the above still won't work, because the character won't actually start moving until the next frame -> their .y is still unchanged -> .Walk() is called again -> previous .Walk is dropped -> rinse, repeat. Basically, the character will never get moving.

Try this:
Code: ags
int boram_delay;

function room_RepExec()
{
  if (boram_delay) boram_delay--;
  else {
    if (cBoram.y == 370) {
      cBoram.Move(705, 380, eNoBlock, eAnywhere);
      boram_delay = 10;
    }
    if (cBoram.y == 380) {
      cBoram.Move(705, 370, eNoBlock, eAnywhere);
      boram_delay = 10;
    }
  }
}

The delay of 10 frames should give the character time to leave the coordinate.


Edit:
Weird, I could've sworn that a check for ! .Moving isn't enough, but apparently it is.

Ryan Timothy B

Replace  eBlock  with eNoBlock
Since you're now checking if cBoram's Y position is 380, there is no need for a blocking call. BUT you also need to check if he's walking first.

Code: ags
function room_RepExec()
{
  if (!cBoram.Moving) {  // condition continues if he's not moving
    if (cBoram.y != 380) cBoram.Move(705, 380, eNoBlock, eAnywhere);
      else cBoram.Move(705, 370, eNoBlock, eAnywhere);
  }
}


Very basic, this is only assuming your character only moves up and down, and that's all.

ShinjinNakamura

Thanks for the explanation Khris, but I got Ryan's code to work.

From what I understand, the "move" action will only occur so long as the the NPC is not walking (ref to !cBoram.Moving).
In this case my NPC does not walk, and so the y coordinate movement is executed. All this without affecting my main character movement (which was my main concern.)

Thanks a lot again guys. This problem is officially solved.

SMF spam blocked by CleanTalk