Graal Forums  

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

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #7  
Old 08-06-2011, 07:39 PM
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
Perhaps my Active Record system would be of help.

In your case you would create a DB-NPC called: Item

PHP Code:
function onCreated() { 

  
// DB Migrations 
  
this.migrations = { 
    {
"new""itemname:string:unique""description:string""price:float"}, // Creates Table 
    
{"add_index""itemname"},  // Adds Index to Table 
    
{"echo""Migrations Completed!"// Optional Message 
  
}; 
   
  
// Configuration 
   
  // Model Object's Specific Class 
  
this.model_class "activerecord_object_" this.name.lower(); 
   
  
// Model Functionality 
  
this.join("activerecord_model"); 
}

public function 
validations(obj) { 
  
// Model specific validations 
  
return true

class: activerecord_object_item

PHP Code:
public function setItemName(new_name) {
  
this.itemname new_name;
}

public function 
setItemDescription(new_desc) {
  
this.description new_desc;
}

public function 
setItemPrice(new_price) {
  
this.price new_price;
}

public function 
getItemName() {
  return 
this.itemname;
}

public function 
getItemDescription() {
  return 
this.description;
}

public function 
getItemPrice() {
  return 
this.price;
}

public function 
pretty() {
  return 
format("Name: %s Description: %s Price: %s"this.itemnamethis.descriptionthis.price); 

You can then create Item objects like this:

PHP Code:
function onCreated() {
  
// Creates Item Object
  
temp.item Item.init();

  
// Sets Item Variables
  
temp.item.setItemName("Sword");
  
temp.item.setItemDescription("It's sharp and will poke your eye out!");
  
temp.item.setItemPrice(100);

  
// Saves Item to DB
  
if (temp.item.save()) {
    echo(
"Item saved with ID " temp.item.id);
  }

and look them up and modify them like this:

PHP Code:
function onCreated() {
  
// Lookup all items
  
temp.items Item.all();
  for (
temp.itemtemp.items) {
    echo(
temp.item.pretty());
  }
  
// Lookup specific item
  
temp.sword Item.where("itemname = 'Sword'").get();
  if (
temp.sword != NULL) {
    echo(
temp.sword.pretty());
  }
  
// If you know the ID
  
temp.sword Item.find(1);
  if (
temp.sword != NULL) {
    
// Increase the price for good measure... (example)
    
temp.sword.setItemPrice(temp.sword.getItemPrice()+100);
    if (
temp.sword.save()) {
      echo(
temp.sword.pretty());
    }
  }

__________________
Quote:
Reply With Quote
 


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 04:01 AM.


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