Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 10-17-2011, 11:42 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Trash Spawning Script Problem

What I am trying to accomplish is a script that spawns trash across the gmap and then the trash is pickupable but my script(s) seem to have an issue.
They seem to be spawning WAY more than its supposed to.
Anyway here are the scripts.

The npc trashspawner
PHP Code:
function onCreated() {
  
this.spawntime 0.05;
  
onTimeout();
}

function 
onTimeout() {
  if (
server.trashspawned 10) {
    
server.trashspawned 10;
  }
  if (
server.trashspawned 10) {
    
onSpawning();
  }
  
settimer(this.spawntime);
}

function 
onSpawning() {
  
this.spawn int(random(16));
  if (
this.spawn == "1") {
    
this.item "Tire";
    
this.itemimage "bc_trash-tire.png";
  }
  if (
this.spawn == "2") {
    
this.item "Bottle";
    
this.itemimage "bc_trash-bottle.png";
  }
  if (
this.spawn == "3") {
    
this.item "Paper";
    
this.itemimage "bc_trash-paper.png";
  }
  if (
this.spawn == "4") {
    
this.item "Motherboard";
    
this.itemimage "bc_trash-motherboard.png";
  }
  if (
this.spawn == "5") {
    
this.item "Soda";
    
this.itemimage "bc_trash-sodacan.png";
  }
  if (
this.spawn == "6") {
    
this.item "Wires";
    
this.itemimage "bc_trash-wires.png";
  }
  if (
server.trashspawned 10) {
    
temp.spawnx random(0448);
    
temp.spawny random(0448);
    
temp.putnpc2(temp.spawnxtemp.spawnynull);
    
temp.t.itemimage this.itemimage;
    
temp.t.itemname this.item;
    
temp.t.join("gunderak_trashdrop");
  }

The class gunderak_trashdrop
PHP Code:
function onCreated() {
  if (
server.trashspawned 10) {
    
destroy();
  }
  
setshape(13232);
  
setimg(this.itemimage);
}

function 
onActionGrab() {
  
client.trash += 1;
  
server.trashspawned -= 1;
  
triggerclient("gui""-System/Message""msg""Server""You gained [" this.itemname "]! You now have: " client.trash " Trash!""orange");
  
destroy();
}

//#CLIENTSIDE
function onCreated() {
  if (
server.trashspawned 10) {
    
destroy();
  }
  
triggerserver("weapon""-System/Trash""spawn");
  
dontblock();
  
drawunderplayer();
  
setshape(13232);
  if (
onwall(this.xthis.y)) {
    
destroy();
    
triggerserver("weapon""-System/Trash""unspawn");
  }

The weapon -System/Trash
PHP Code:

function onActionServerSide() {
  if (
params[0] == "unspawn") {
    if (
server.trashspawned 0) {
      
server.trashspawned--;
    }
  }
  if (
params[0] == "spawn") {
    if (
server.trashspawned 10) {
      
server.trashspawned++;
    }
  }

Any help is appriciated
PS: yes I know im probably doing this completely wrong, I cannot think of any other way to accomplish my goal.
If you would like put in an easier way your more than welcome.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #2  
Old 10-17-2011, 04:01 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
I wonder how many people have stolen this half working script..
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #3  
Old 10-17-2011, 05:49 PM
ffcmike ffcmike is offline
Banned
Join Date: Jul 2004
Location: London
Posts: 2,029
ffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond repute
Send a message via AIM to ffcmike Send a message via MSN to ffcmike
Quote:
Originally Posted by Gunderak View Post
I wonder how many people have stolen this half working script..
It's a pretty poor and inefficient set of scripts.

Looking at NPC "trashspawner" you have multiple checks to determine the item where it's possible it will have already been determined.
This could be made more efficient by either using a switch statement or storing the item names and images within an array, and then retrieving this.array[this.spawn - 1];.
It also looks as if you're setting a 0.05 second timeout which shouldn't even work Serverside, where again you have a potentially unnecessary condition being checked where a conflicting condition could be true.

Within "gunderak_trashdrop" you are checking a server. variable Clientside when these are only readable Serverside.
It also appears as if you are communicating from Serverside to Clientside, and then back to Serverside just to increment a server. variable when this can be done at the point of dropping the NPC. Not only is this totally unnecessary and suggesting that you're not really aware of what you're doing, it will also result in a triggerserver for every single client that creates the NPC. It can also result in both the spawn and unspawn trigger being sent at the same time when (even if it was a good idea) you should only have one of these triggers occurring. Calling destroy(); Clientside will also only destroy the NPC on your client, you have no mechanism to permanently destroy the NPC and prevent it from loading on yours or other peoples clients.

You yet again within "-System/Trash" you have the potential for unnecessary condition checks where params[0] == "spawn" can be checked for despite params[0] == "unspawn" already being determined as true, albeit you don't need this trigger anyway.
Reply With Quote
  #4  
Old 10-17-2011, 11:31 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
Thankyou for the feedback I have taken alot of that into considoration and am going to re script it from scratch, may I ask if there would be any way to do like a triggerserver or a triggerclient on a NPC? this was one of my main problems.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #5  
Old 10-17-2011, 11:44 PM
0PiX0 0PiX0 is offline
Coder
0PiX0's Avatar
Join Date: Jan 2011
Posts: 130
0PiX0 is a jewel in the rough0PiX0 is a jewel in the rough
http://forums.graalonline.com/forums...ad.php?t=87732
__________________
Reply With Quote
  #6  
Old 10-18-2011, 12:01 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
You don't even need to use client-side or a timeout to spawn trash at all.

DB

PHP Code:
function onCreated() {
  
// Configuration
  
this.max_trash 20;
  
// Initialize Trash DB
  
destroyTrash();
  
spawnTrash();
}

function 
destroyTrash() {
  
// Loop through each piece of Trash
  
for (temp.trashthis.spawned_trash) {
    
// Destroy Trash NPC
    
temp.trash.destroy();
  }
  
// Initialize Spawned Trash Array
  
this.spawned_trash = {};
}

function 
spawnTrash() {
  
// Spawn Trash until Maximum Reached
  
while (this.spawned_trash.size() < this.max_trash) {
    
// Create Trash
    
temp.trash createTrash();
    
// Add Trash to DB
    
this.spawned_trash.add(temp.trash);
  }
}

function 
createTrash() {
  
// Create Trash NPC
  
temp.trash putnpc2();
  
temp.trash.join("trash");
  return 
temp.trash;
}

public function 
trashInDB(obj) {
  return (
obj in this.spawned_trash);
}

public function 
pickedUpTrash(obj) {
  
// Remove and Destroy Trash
  
this.spawned_trash.remove(obj);
  
obj.destroy();
  
// Spawn More Trash
  
spawnTrash();

Class: trash

PHP Code:
function onPlayerEnters() {
  
// Check if Trash in DB
  
if (!TrashDB.trashInDB(this)) {
    
// Not found so destroy it
    
destroy();
  }
}

function 
onActionGrab() {
  
// Notify DB of Trash Picked Up
  
TrashDB.pickedUpTrash(this);

__________________
Quote:

Last edited by fowlplay4; 10-18-2011 at 12:17 AM..
Reply With Quote
  #7  
Old 10-18-2011, 07:36 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
You make things so much simpler.
All I have to do now is add the images of random trash and presto.
Would you possible know how to put names and images in an array?
maybe something like.
PHP Code:
temp.trash = {
  {
"trashone.png"Name},
  {
"trashtwo.png"Name}
}; 
I believe this is how a "3D" array works?
But how would you read the information from that and pick a random trash object O_O
PHP Code:
this.chosen random(0temp.trash.size()); 
Yet again im probably horribly wrong.
I was just thinking doing an array would make it look much nicer and be easier to add more items, rather than doing this.
PHP Code:
temp.chosen random(13);
if (
temp.chosen == "1") {
  
this.item "Garbage 1";
  
this.image "Image for it";
}
if (
temp.chosen == "2") {
  
this.item "Garbage 2";
  
this.image "Image for it";
}
if (
temp.chosen == "3") {
  
this.item "Garbage 3";
  
this.image "Image for it";

__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #8  
Old 10-18-2011, 07:58 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Gunderak View Post
PHP Code:
this.chosen random(0temp.trash.size()); 
Yet again im probably horribly wrong.
I was just thinking doing an array would make it look much nicer and be easier to add more items, rather than doing this.
PHP Code:
temp.chosen random(13);
if (
temp.chosen == "1") {
   ...

The return value of random is a float. The probability that it returns a particular value is (almost) zero. So try int(random(...)).
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #9  
Old 10-18-2011, 08:08 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
wont using int(random(0, 5)); return 0, 1, 2, 3, 4 or 5 instead of random numbers like 4.719827 and 5.827591?
I shall have to see if it returns words xD
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #10  
Old 10-18-2011, 09:58 AM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
@Fowlplay i have made a few modifications to your system, it now works perfectly except for one slight problem. the items spawn on walls.
ive tried to put a if(onwall(this.x, this.y)){ and then destroy it but it does absolutely nothing.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #11  
Old 10-18-2011, 10:24 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by Gunderak View Post
@Fowlplay i have made a few modifications to your system, it now works perfectly except for one slight problem. the items spawn on walls.
ive tried to put a if(onwall(this.x, this.y)){ and then destroy it but it does absolutely nothing.
Try using onwall2 with this.height and this.width
Reply With Quote
  #12  
Old 10-18-2011, 10:31 AM
ffcmike ffcmike is offline
Banned
Join Date: Jul 2004
Location: London
Posts: 2,029
ffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond reputeffcmike has a reputation beyond repute
Send a message via AIM to ffcmike Send a message via MSN to ffcmike
Quote:
Originally Posted by Gunderak View Post
@Fowlplay i have made a few modifications to your system, it now works perfectly except for one slight problem. the items spawn on walls.
ive tried to put a if(onwall(this.x, this.y)){ and then destroy it but it does absolutely nothing.
Quote:
Originally Posted by Twinny View Post
Try using onwall2 with this.height and this.width
Is it possible that onwall(); may be detecting the NPC itself as blocking?
Within the original script dontblock(); is used Clientside, so on Serverside it would still be blocking.
It could always be made to do an onwall() check before dropping the NPC rather than destroying it afterwards.
Reply With Quote
  #13  
Old 10-18-2011, 01:06 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
the script is serverside and an onwall check skews it up.
so I can't put it in that particular script.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
  #14  
Old 10-18-2011, 01:24 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Gunderak View Post
the script is serverside and an onwall check skews it up.
How? You need to be more specific than "skews it up" or we can't help you.
__________________
Reply With Quote
  #15  
Old 10-18-2011, 02:34 PM
Gunderak Gunderak is offline
Coder
Gunderak's Avatar
Join Date: Jun 2011
Location: Australia
Posts: 795
Gunderak is on a distinguished road
if you do if onwall serverside it sais it is onwall constantly.
__________________

Gund for president.

Remote PM {P*}x (Graal813044) from eraiphone -> Stefan: I hav 1 qustion
*Gunderak: he hav 1
*Gunderak: qustion
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 05:53 AM.


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