Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   2 questions (server warp & putnpc2) (https://forums.graalonline.com/forums/showthread.php?t=85704)

Jiroxys7 05-24-2009 03:24 AM

2 questions (server warp & putnpc2)
 
Okay. Firstly, I've made an NPC that when the player says /serverlist, a GUI window pops up. with a long list of servers. The player can select one, click connect, and will warp to that server.

Everything works just fine. Except for the fact that i don't know how to make serverwarp read a var.
Currently it's: serverwarp this.selectedserverconnect;
though i've tried serverwarp @ this.selectedserverconnect; and a few more.
I just need the format.


Also, in regards to putnpc2. This has drivin me crazy.
Offline, I used putnpc to place a custom NPC (like, lets say it puts a bomb script that lays a bomb, that puts a few randomly sized explosions, in random areas within a certain area. I would put something like:
PHP Code:

if(weaponfired&&playerbombs>0){
if(
playerdir=0){
play put.wav;putnpc custombombimg.png,custombomb.txt,playerx+0.5,playery-1;
playerbombs-=1;
}
if(
playerdir=1){
play put.wav;putnpc custombombimg.png,custombomb.txt,playerx-1.5,playery+1;
playerbombs-=1;
}
if(
playerdir=2){
play put.wav;putnpc custombombimg.png,custombomb.txt,playerx+0.5,playery+2.5;
playerbombs-=1;
}
if(
playerdir=3){
play put.wav;putnpc custombombimg.png,custombomb.txt,playerx+2.5,playery+1;
playerbombs-=1;


Or whatever. The problem is, I've had -no- luck translating this to use putnpc2


I started with rescripting it online in GS2. Though as I kept searching every available resource I could find for what I was doing wrong, and as my frustration grew, I eventually started rescripting it in GS1 (So I can get what I got in GS1, then once I have everything else set up, I could try attempting to convert it back to GS2.) Still had no luck of course.

So could anyone please explain to me the correct format of putnpc2?
(I.e. the format for putnpc (in GS1) is: putnpc image.png, script.txt, x, y; )

Stryke 05-24-2009 04:11 AM

You should really indent your code, it helps us (and you) read the script easier,.

Basically the GS2 equivalent is:

PHP Code:

function onWeaponFired()
{

  
play("put.wav");
  
player.bombs--;

  if (
player.dir == 0) {
    
temp.putnpc2(player.0.5player.1"");
    
temp.i.join("classname");
  }
  else if (
player.dir == 1) {
    
temp.putnpc2(player.1.5player.1"");
    
temp.i.join("classname");
  }
  else if (
player.dir == 2) {
    
temp.putnpc2(player.0.5player.2.5"");
    
temp.i.join("classname");
  }
  else if (
player.dir == 3) {
    
temp.putnpc2(player.2.5player.1"");
    
temp.i.join("classname");
  }



Now all you have to do is add the script for the bomb inside the class from the .txt file.

Also, it should be:
serverwarp(this.selectedserverconnect);

cbk1994 05-24-2009 04:15 AM

Stryke is correct, but I would do something like

PHP Code:

temp.npc putnpc2(player.1.5 vecx(player.dir) * 2player.vecy(player.dir) * 2"");
npc.join("classname"); 

though you'd have to play with the coordinates.

As far as the script, you create a new class called 'classname' or whatever you specify it as in the other script, and put in your script as (for example)

PHP Code:

function onCreated() {
  
setimg("bomb.png");
  
  for (
temp.30--) {
    
sleep(1);
  }
  
  
putexplosion(2this.xthis.y);
  
destroy();


When you use putnpc2, you create a new NPC. I'll explain the rest in comments.

PHP Code:

putnpc2(3232""); // third param is the script, you can also do
putnpc2(3232"this.x = 10;"); // or any other script you want, but it's really annoying to script like that, and is considered bad practice.

// normally, you should join a class; there are a few ways to do this.
putnpc2(3232"join classname;"); // this isn't preferred since it's using GS1

  // the following class joining examples are all GS2
  
putnpc2(3232"join(\"classname\");"); // notice the confusing escape slashes; the other ones are much easier.
  
  // or ...
  
with (putnpc2(3232"")) {
    
join("classname");
  }
  
  
// or ...
  
  
temp.npc putnpc(3232"");
  
npc.join("classname"); // I prefer this way, because it keeps the stuff in scope, which means you can do
  
npc.dropper player.account// this

  
// Since NPCs are objects, you can access them from the laying script.

echo(npc.x); // would echo 32
echo("Chat:" SPC npc.chat); // would echo "Chat:" because there is no chat. If there was chat, it would be "Chat: Hello, world!" or whatever the chat is

// you can also perform functions on the npc or call events
npc.warpto("level.name"3029);
npc.chat "Hello, world!";
npc.trigger("Test"); // calls the event "onTest()" in the script.
npc.destroy(); // removes the NPC; make sure to do this when done with it, or within the class (which is preferred) 


DustyPorViva 05-24-2009 04:20 AM

Why do:
for (temp.i = 3; i > 0; i --) {
sleep(1);
}

instead of just doing sleep(3)?

napo_p2p 05-24-2009 04:44 AM

Quote:

Originally Posted by DustyPorViva (Post 1493560)
Why do:
for (temp.i = 3; i > 0; i --) {
sleep(1);
}

instead of just doing sleep(3)?

Probably just left out a countdown (this.chat = temp.i)

Jiroxys7 05-24-2009 05:02 AM

Thanks you guys. I'll try that out.

Quote:

Originally Posted by DustyPorViva (Post 1493560)
Why do:
for (temp.i = 3; i > 0; i --) {
sleep(1);
}

instead of just doing sleep(3)?

Isn't the for (temp.i = 3; i > 0; i --) {} thing an array? (correct me if i'm wrong but i think thats what its called)
I've seen these alot. and apparently it can be very useful in certain script.
Could someone explain how this works? Its always seemed very confusing to me. reading how "for" works in the offline editor helps a little, but what's it checking? and what exactly is that script doing?

i would guess it's checking if temp.i = 3. or is it setting it to 3? then telling it it's greater than 0 then subtracting 1?

or, by looking at Dusty's comment, is it producing three one second sleeps?
then once temp.i == 0, it tells it to stop telling it to sleep.. or something?

and where's it pulling the initial value for temp.i from anyway?

cbk1994 05-24-2009 05:59 AM

Quote:

Originally Posted by DustyPorViva (Post 1493560)
Why do:
for (temp.i = 3; i > 0; i --) {
sleep(1);
}

instead of just doing sleep(3)?

You're right, I did mean to add a count-down, but I wasn't thinking.
Quote:

Originally Posted by Jiroxys7 (Post 1493580)
Isn't the for (temp.i = 3; i > 0; i --) {} thing an array? (correct me if i'm wrong but i think thats what its called)

It's called a for loop.
Quote:

I've seen these alot. and apparently it can be very useful in certain script.
Could someone explain how this works? Its always seemed very confusing to me. reading how "for" works in the offline editor helps a little, but what's it checking? and what exactly is that script doing?

i would guess it's checking if temp.i = 3. or is it setting it to 3? then telling it it's greater than 0 then subtracting 1?

or, by looking at Dusty's comment, is it producing three one second sleeps?
then once temp.i == 0, it tells it to stop telling it to sleep.. or something?

and where's it pulling the initial value for temp.i from anyway?
Let's say you have something like
PHP Code:

for (temp.010++) {
  echo(
i);
  
sleep(1);


It's going to output this in RC, with a one second delay between each number.
Quote:

0
1
2
3
4
5
6
7
8
9
What it does is loop through something a certain number of times.

PHP Code:

function countTo(numberdelay) {
  for (
temp.0number++) {
    echo(
i);
    
sleep(delay);
  }


would let you count to any number.

PHP Code:

temp.0;

for (
temp.05++) {
  
+= int(random(15));
  
sleep(0.5);


Here's what this would be doing in English.

Quote:

SET v TO 0
START FOR LOOP
SET n TO 0
SET v to 2
SLEEP .5 SECONDS
SET n TO 1
SET v to 3
SLEEP .5 SECONDS
SET n to 2
SET v to 6
SLEEP .5 SECONDS

[and all the way to 4]
Here's the basic structure.

PHP Code:

for (variable startvariable (inequalitymax/minvariable change) {
  
// code to be executed


so

PHP Code:

for (temp.30--) {
  echo(
x);


would say that x starts at 3. It would then execute the code in the middle by outputting '3'. Then, x is decreased to 2. It checks if 2 is more than 0, which it is, so it keeps going and executes the code. x now equals 1, which is more than 0, so it goes again. x now equals 0, which is not more than 0, so it breaks out of the for loop.


Another, different type of for loop is like a foreach loop in other languages. It's done like this:

PHP Code:

for (variable : array) {
  echo(
variable);


Arrays are defined like this

PHP Code:

temp.array = {"one""two""three""hello""world"}; 

which means you can also do

PHP Code:

for (variable : {"one""two""three""hello""world"}) {
  echo(
variable);


however, you need to specify a variable

PHP Code:

for (temp.number : array) {
  echo(
number);


would output

Quote:

one
two
three
hello
world
Be sure not to get the two types of for loops mixed up. The for each loop does the same thing as

PHP Code:

temp.array = {"one""two""three""hello""world"};

for (
temp.0< array.size(); ++) {
  
temp.variable = array[i];


heh, long post

Jiroxys7 05-24-2009 08:24 AM

Ah, I understand it now! Much thanks for the explaination Chris :D

Edit:
Okay, as for the putnpc2 thing, i'm still having trouble.
I got:

PHP Code:

function onWeaponFired() 
{
if(
player.bombs>0){ 

  
play("put.wav"); 
  
player.bombs--; 

  if (
player.dir == 0) { 
    
with(putnpc2(player.0.5player.1"")){
    
join("firebomb"); 
   }
  }
  else if (
player.dir == 1) { 
    
with(putnpc2(player.1.5player.1"")){
    
join("firebomb"); 
   }
  } 
  else if (
player.dir == 2) { 
    
with(putnpc2(player.0.5player.2.5"")){
    
join("firebomb"); 
   }
  } 
  else if (
player.dir == 3) { 
    
with(putnpc2(player.2.5player.1"")){
    
join("firebomb"); 
   }
  } 

 }


Since using the other way didnt work. Clientsiding only plays the wav, and nonclientsiding won't trigger the onWeaponfired function.
So i'm still stuck in the same place x_x

cbk1994 05-24-2009 08:42 AM

onWeaponFired() and play() are both clientside functions.

PHP Code:

function onActionServerSide(cmd) {
  if (
cmd == "dropFireBomb") {
    
player.bombs --;
    
    
// you can add back your long list here if you want
    
temp.npc putnpc2(player.1.5 vecx(player.dir) * 2player.vecy(player.dir) * 2"");
    
npc.join("firebomb");  
  }
}
//#CLIENTSIDE
function onWeaponFired() {
  if (
player.bombs <= 0) {
    return;
  }
  
  
triggerserver("gui"name"dropFireBomb");
  
play("put.wav");
  
freezeplayer(1);



Jiroxys7 05-24-2009 08:03 PM

awesome, it works now :D

quick question though. that's the purpose of the "gui" in triggerserver?

cbk1994 05-24-2009 09:06 PM

Quote:

Originally Posted by Jiroxys7 (Post 1493717)
awesome, it works now :D

quick question though. that's the purpose of the "gui" in triggerserver?

It specifies that you're triggering a Weapon/GUI-Script. You can also use "weapon", but "gui" is shorter.

"npc" is the other form that is used, and that is to trigger DB NPCs.

Jiroxys7 05-24-2009 11:49 PM

alright. and yet another question if i might add, I fixed one of my other weapons. When fired, it creates an npc char that moves across the screen shooting arrows. then, once it has moved a certain amount, it calls its movement finished thing, then is told to destroy(); itself. The problem is, after that,if i use the update level command or reconnect, the npc will reappear. like it never actually got deleted. Do i need to destroy(); serverside or something?

cbk1994 05-25-2009 12:25 AM

Quote:

Originally Posted by Jiroxys7 (Post 1493793)
alright. and yet another question if i might add, I fixed one of my other weapons. When fired, it creates an npc char that moves across the screen shooting arrows. then, once it has moved a certain amount, it calls its movement finished thing, then is told to destroy(); itself. The problem is, after that,if i use the update level command or reconnect, the npc will reappear. like it never actually got deleted. Do i need to destroy(); serverside or something?

Can you post the code that is placing the NPC?

napo_p2p 05-25-2009 12:57 AM

Quote:

Originally Posted by Jiroxys7 (Post 1493793)
Do i need to destroy(); serverside or something?

Yup. If you don't destroy() it serverside, then it won't get removed from the level.

cbk1994 05-25-2009 01:05 AM

Quote:

Originally Posted by napo_p2p (Post 1493816)
Yup. If you don't destroy() it serverside, then it won't get removed from the level.

That's correct. You can use triggeraction to trigger the location the NPC is at (or started at if you're moving clientside).

Sorry I didn't see that, I think you edited your post.

EDIT: Nevermind, you didn't edit it, I quoted you.


All times are GMT +2. The time now is 08:07 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.