Game Development Community

Behavior not giving me the information I want.

by Andy Hawkins · in Technical Issues · 10/06/2011 (3:54 am) · 3 replies

I have a behavior for my shop keeper in my RPG I am making that defines what items he holds and how much they cost. The listing is below. When I try to use the following code to access the actual item he is stocking it just returns either object, or t2dAnimatedSprite. I actually want the object the user put in there, such as HealthInstant or Sword. What code is required to get the actual value of the field entered by the user?
%inv = %srcObj.owner.getBehavior("InventoryBehavior");
%objectType = %inv.template.getBehaviorFieldUserData(2);
echo(%objectType);
returns...
t2dAnimatedSprite

Behavior code...
if (!isObject(InventoryBehavior))
{
   %template = new BehaviorTemplate(InventoryBehavior);
   
   %template.friendlyName = "Inventory";
   %template.behaviorType = "System";
   %template.description  = "Handles Inventory.";
   
   %template.addBehaviorField(desc, "Description", string, "");
   %template.addBehaviorField(info1, "Information", string, "");
   %template.addBehaviorField(item1, "Item 1", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price1, "Cost", int, "", 0);
   %template.addBehaviorField(item2, "Item 2", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price2, "Cost", int, "", 0);
   %template.addBehaviorField(item3, "Item 3", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price3, "Cost", int, "", 0);
   %template.addBehaviorField(item4, "Item 4", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price4, "Cost", int, "", 0);
   %template.addBehaviorField(item5, "Item 5", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price5, "Cost", int, "", 0);
   %template.addBehaviorField(item6, "Item 6", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price6, "Cost", int, "", 0);
   %template.addBehaviorField(item7, "Item 7", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price7, "Cost", int, "", 0);
   %template.addBehaviorField(item8, "Item 8", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price8, "Cost", int, "", 0);
   %template.addBehaviorField(item9, "Item 9", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price9, "Cost", int, "", 0);
   %template.addBehaviorField(item10, "Item 10", object, "", t2dAnimatedSprite);
   %template.addBehaviorField(price10, "Cost", int, "", 0); 
}


#1
10/06/2011 (7:44 am)
@Anday - Have you tried just accessing item1 or item2?
#2
10/06/2011 (7:53 am)
Well I've tried to iterate through them building them using this code, but it causes a syntax error.
for (%i = 1; %i < 10; %i ++)
{
     %item = "item" @ %i;
     echo( %inv.%item );
}
It doesn't like having the two variables used like that. If I just type %inv.item1 it works fine but then I have to use a switch or if statement for each item and if a shop has 20 items or more it makes the code too bloated. Also it's not going to be dynamic in that I cant have shop items of any number. I can't add and delete either.
#3
10/07/2011 (9:31 am)
The solution came from Hicks1 and [DFE]Ted on irc. Thanks guys.

Changed it to this and it worked...
for (%i=1; %i < 10; %i++)
{ 
    echo(%inv.item[%i]);
}
I just needed to update my item1 to item_1 as that's what [%i] outputs.