Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Custom Projectile (https://forums.graalonline.com/forums/showthread.php?t=82987)

Pelikano 12-03-2008 04:38 PM

Custom Projectile
 
PHP Code:

function onPlayerChats() {
  if (
player.chat == ":destroy")
    
this.destroy();
}
//#CLIENTSIDE
function onCreated() {
  
setimg("era_pl9-bullet.png");
  
setshape(1,32,32);
  
setTimer(0.05);
  
this.owneraccount player.account;
  
this.dird player.dir;
  
this.spread random(-client.spread,client.spread);
  
scheduleevent(5,"Destroy","");
}
function 
onTimeOut() {
  
this.+= vecx(this.dird) + (player.dir in {02} ? this.spread/16 0);
  
this.+= vecy(this.dird) + (player.dir in {13} ? this.spread/16 0);
  
setTimer(0.05);


I am trying to make a custom projectile.
Well, it works fine unill now, the only part that is missing, is the damage part.

Well, I am stuck, how can I make it damage on Hit, without using shoot()?

xXziroXx 12-03-2008 05:26 PM

You'll have to detect in a timeout if the projectile is on a player/npc, and thus... BOOM.

Codein 12-03-2008 05:26 PM

I'd probably do something like this in the timeout loop:

PHP Code:

for (plplayers) {
  if (
this.x in |pl.xpl.3| && this.y in |pl.ypl.y3|) {
    
//hit stuff
  
}


I think you'd do something similar for NPCs.

cbk1994 12-04-2008 02:12 AM

Might be easier or more efficient to use findnearestplayer()

Pelikano 12-04-2008 11:53 AM

Quote:

Originally Posted by Codein (Post 1446461)
I'd probably do something like this in the timeout loop:

PHP Code:

for (plplayers) {
  if (
this.x in |pl.xpl.3| && this.y in |pl.ypl.y3|) {
    
//hit stuff
  
}


I think you'd do something similar for NPCs.

You're a genius dude.

Thanks.

xXziroXx 12-04-2008 12:04 PM

PHP Code:

for (temp.0temp.2temp.i++) {
  for (
temp.objnpcs players) {
    if (
this.x in |obj.xobj.3| && this.y in |obj.yobj.y3|) {
      
//hit stuff
    
}
  }


There, now you have a loop for both players and NPC's.

Pelikano 12-04-2008 12:13 PM

Quote:

Originally Posted by xXziroXx (Post 1446632)
PHP Code:

for (temp.0temp.2temp.i++) {
  for (
temp.objnpcs players) {
    if (
this.x in |obj.xobj.3| && this.y in |obj.yobj.y3|) {
      
//hit stuff
    
}
  }


There, now you have a loop for both players and NPC's.

Why would I need a loop for the player and for the Projectile?
o_O

xXziroXx 12-04-2008 12:15 PM

Quote:

Originally Posted by Pelikano (Post 1446633)
Why would I need a loop for the player and for the Projectile?
o_O

So you're saying your projectiles will only be able to damage players, and not NPC's? If that's the case, by all means, use Codein's loop, but otherwise you'd need mine.

Pelikano 12-04-2008 12:18 PM

Well, I don't have any NPCs :D

[email protected] 12-04-2008 06:39 PM

you will if you're making an era type server, trust me.

Codein 12-04-2008 06:53 PM

I'd also use a gani for the bullet, rather than it being a static image.

Pelikano 12-13-2008 08:30 PM

Well.
Thanks again, I got another question tho:

I am trying to make the projectile destroy after 3 seconds, so I'm using:

scheduleevent(3,"StarDestroy","");

function onStarDestroy() {
triggeraction(this.x + 6/16, this.y + 6/16, "StarDestroy", nil);
}

function onActionStar() {
this.destroy();
}

And it doesnt work x_X, am I triggering it wrong, or what? :o

DustyPorViva 12-13-2008 08:49 PM

Why are you triggering an action to destroy it? Just destroy it in the function.

Pelikano 12-14-2008 12:40 PM

Uhm..
Well, because when I use this.destroy(); clientside, it doesn't really destroy it o_O

Chompy 12-14-2008 12:51 PM

Quote:

Originally Posted by Pelikano (Post 1449227)
Uhm..
Well, because when I use this.destroy(); clientside, it doesn't really destroy it o_O

Then just do something like this:

PHP Code:


function onCreated() {
  
setTimer(3);
}

function 
onTimeout() {
  
destroy();
}
//#CLIENTSIDE

// All your clientside code here 


Pelikano 12-14-2008 01:26 PM

Good idea, but I'm using a function onCreated Clientside already and I need that function onCreated Clientside, because I start a timer clientside with that function, which is needed oO

Also "destroy();" would destroy the whole script I heard xD

Codein 12-14-2008 01:31 PM

Quote:

Originally Posted by Pelikano (Post 1449235)
Good idea, but I'm using a function onCreated Clientside already and I need that function onCreated Clientside, because I start a timer clientside with that function, which is needed oO

You can have an onCreated() serverside and an onCreated() clientside at the same time.

Quote:

Originally Posted by Pelikano (Post 1449235)
Also "destroy();" would destroy the whole script I heard xD

It should only destroy the NPC, not the entire script.

Pelikano 12-14-2008 01:33 PM

Really?
Awesome, thank you.
I'll try that :D

Works perfect.
Thanks.

Pelikano 12-14-2008 02:56 PM

Ok another thing.
I decided to move the projectile serverside, because I was told that I had to do it like that, so it looks somewhat like this:

PHP Code:

function onCreated() {
  
setimg("shuriken_icon.gif");
  
setshape(1,12,12);
  
this.dird player.dir;
  
this.spread random(-client.spread,client.spread);
  
dontblock();
  
scheduleevent(3,"StarDestroy","");
  
setTimer(0.1);
}
function 
onTimeOut() {
  if (!
onwall(this.6/16 vecx(this.dir)*7/16this.6/16 vecy(this.dir)*7/16)) { 
    
this.+= vecx(this.dird) + (player.dir in {02} ? this.spread/16 0);
    
this.+= vecy(this.dird) + (player.dir in {13} ? this.spread/16 0);
    
setTimer(0.1);
  }
  
triggeraction(this.6/16 vecx(this.dir)*8/16,this.6/16 vecy(this.dir)*8/16,"StarDamage",nil);


Well, it does not work at all anymore!
The projectile gets spawned in front of my player, yes, but it doesn't move anymore.
While debugging I figured out that "this.spread" and "this.dird" are both 0 now and won't get set to what I want them to get set oO

x_X
Anyone?

xXziroXx 12-14-2008 03:14 PM

That's because "onCreated" serversided in a local NPC isn't an event triggered related to a player, so "player" equals NULL.

You'll need to pass the player object through your putnpc function, similar to this:

PHP Code:

temp.foobar putnpc2(xy"");
foobar.owner player

Then you can just replace "player" with "this.owner" in the script.

Pelikano 12-14-2008 03:20 PM

I don't get it :(

xXziroXx 12-14-2008 03:25 PM

Post the part of your code that you use to spawn projectiles please. :whatever:

Pelikano 12-14-2008 03:27 PM

function onActionServerSide() {
putnpc2(params[0],params[1],"join bullet;");
}

Codein 12-14-2008 03:30 PM

Quote:

Originally Posted by Pelikano (Post 1449263)
function onActionServerSide() {
putnpc2(params[0],params[1],"join bullet;");
}

Yeah. You should do this:

PHP Code:

function onActionServerside() {
  
temp.proj putnpc2(params[0], params[1], NULL);
  
temp.proj.owner player;
  
temp.proj.join("bullet");
 


Then, for the dir, type this.owner.dir instead of player.dir.

xXziroXx 12-14-2008 03:32 PM

Change that, too...

PHP Code:

function onActionServerSide() {
  
temp.npc putnpc2(params[0],params[1],"");
  
npc.owner player;
  
npc.join("bullet");


... and your projectile script too...

PHP Code:

function onCreated() {
  
setimg("shuriken_icon.gif");
  
setshape(1,12,12);
  
this.dird this.owner.dir;
  
this.spread random(-this.owner.client.spreadthis.owner.client.spread);
  
dontblock();
  
scheduleevent(3,"StarDestroy","");
  
setTimer(0.1);
}

function 
onTimeOut() {
  if (!
onwall(this.6/16 vecx(this.dir)*7/16this.6/16 vecy(this.dir)*7/16)) { 
    
this.+= vecx(this.dird) + (this.owner.dir in {02} ? this.spread/16 0);
    
this.+= vecy(this.dird) + (this.owner.dir in {13} ? this.spread/16 0);
    
setTimer(0.1);
  }
  
triggeraction(this.6/16 vecx(this.dir)*8/16,this.6/16 vecy(this.dir)*8/16,"StarDamage",nil);


:)

Pelikano 12-14-2008 03:34 PM

Yes I know.. it works fine.
But I don't understand it lol x_X

Why "this.ower"?

What's this? x_X:

"temp.proj.owner = player;"


Why "NULL"?!

Wtf?! temp.proj.join("bullet");

Pelikano 12-14-2008 03:35 PM

Can't I just set "this.dird" and "this.spread" in a "function onCreated" Clientside and then send it to the server?

If I can do that, how do I send it to the server using triggeraction?

Omg I am so confused lol

Codein 12-14-2008 03:39 PM

Quote:

Originally Posted by Pelikano (Post 1449268)
Yes I know.. it works fine.
But I don't understand it lol x_X

Why "this.ower"?

What's this? x_X:

"temp.proj.owner = player;"


Why "NULL"?!

Wtf?! temp.proj.join("bullet");

The player object is being assigned to temp.proj.owner. Then, in the NPC, you can access the player object who created it, through this.owner.

xXziroXx 12-14-2008 03:39 PM

Quote:

Originally Posted by Pelikano (Post 1449268)
Why "this.ower"?

You can name it whatever you want, I chose owner to represent it's the person who spawned it.

Quote:

Originally Posted by Pelikano (Post 1449268)
"temp.proj.owner = player;"

As I said, you can't access "player" in a serversided onCreated(), since it's not an event related to a player but rather when the script is executed the first time.

Quote:

Originally Posted by Pelikano (Post 1449268)
Why "NULL"?!

NULL and "" is the same thing, more or less. In this case it doesn't matter which you use.

Quote:

Originally Posted by Pelikano (Post 1449268)
Wtf?! temp.proj.join("bullet");

Yes, let me explain:

temp.npc = putnpc(x, y, ""); - This spawns an NPC on the coordinates x and y. By doing it like this you can change the values of the spawned NPC by accessing "temp.npc".

npc.owner = player - This sets the this.owner in the projectile to the player object, so you can access data from the player who spawned the projectile.

npc.join("bullet"); - This joins the newly spawned NPC to your projectile class.


Just try the code I gave you man, it'll work.

Pelikano 12-14-2008 03:42 PM

Thank you.

I'll read this 50 times now untill I understand it :D

Also what you gave me doesn't work.

xXziroXx 12-14-2008 03:59 PM

Quote:

Originally Posted by Pelikano (Post 1449235)
Also "destroy();" would destroy the whole script I heard xD

By the way, that only happens if you use destroy() serversided in a Weapon or DB NPC.


All times are GMT +2. The time now is 08:20 AM.

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