Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Loading Variables into Objects (https://forums.graalonline.com/forums/showthread.php?t=73406)

killerogue 04-10-2007 09:16 AM

Loading Variables into Objects
 
Ok, here's what Im' doing, I'm loading variables from a text file into an object then I'm using a for each loop to go through each var starting with "whatever" like this"

PHP Code:

blah = new TStaticVar();
blah.loadVars("thistextfile.arc"

Now my questions is do I do this...

PHP Code:

for (igetstringkeys("inv_")); 

Or this....

PHP Code:

for (igetstringkeys("blah.inv_")); 

Forgive me if it's a really simple or unintelligent questions, I just sort of started understanding objects today and I figured this would be somewhat of a cakewalk. :P

(What I'm doing is creating an item system using text files. :D

Admins 04-10-2007 10:08 AM

getstringkeys() accepts this., thiso., player., playero. etc., you can't do "anyobjectname.varname" though. For that you would need to use the with-command like
PHP Code:

with (objectname) {
  for (
keygetstringkeys("this.inv_"))
    ...



killerogue 04-10-2007 10:18 AM

Quote:

Originally Posted by Stefan (Post 1298691)
getstringkeys() accepts this., thiso., player., playero. etc., you can't do "anyobjectname.varname" though. For that you would need to use the with-command like
PHP Code:

with (objectname) {
  for (
keygetstringkeys("this.inv_"))
    ...



That's interesting. >.>

Seeing as how a friend of mine told me,

PHP Code:

for (keygetstringkeys("objectname.inv_")) 

Would work.

Edit: But if I did:

Here's what I'm doing:

PHP Code:

public function inventory()
{
  
invcontents = new TStaticVar();
  
invcontents.loadVars("levels/player_" player.account ".arc");
    for (
igetstringkeys("inv_"))
    {
      if (
== "item=")
      {
        
tokens getstringkeys("invcontents.inv_" i).substring(22).tokenize(",");
      }
      else if (
== "wep=")
      {
        
//stuff
      
}
      else if (
== "keyitem=")
      {
        
tokens getstringkeys("invcontents.inv_" i).substring(25).tokenize(",");
      }
      else if (
== "clothes=")
      {
        
tokens getstringkeys("invcontents.inv_" i).substring(25).tokenize(",");
      }
    }
  } 


Also, doing what you said and using, with, would I have to put "this.inv_" or could I just do "inv_".

Novo 04-10-2007 02:10 PM

Quote:

Originally Posted by killerogue (Post 1298692)
That's interesting. >.>

Seeing as how a friend of mine told me,

PHP Code:

for (keygetstringkeys("objectname.inv_")) 

Would work.

Edit: But if I did:

Here's what I'm doing:

PHP Code:

public function inventory()
{
  
invcontents = new TStaticVar();
  
invcontents.loadVars("levels/player_" player.account ".arc");
    for (
igetstringkeys("inv_"))
    {
      if (
== "item=")
      {
        
tokens getstringkeys("invcontents.inv_" i).substring(22).tokenize(",");
      }
      else if (
== "wep=")
      {
        
//stuff
      
}
      else if (
== "keyitem=")
      {
        
tokens getstringkeys("invcontents.inv_" i).substring(25).tokenize(",");
      }
      else if (
== "clothes=")
      {
        
tokens getstringkeys("invcontents.inv_" i).substring(25).tokenize(",");
      }
    }
  } 


Also, doing what you said and using, with, would I have to put "this.inv_" or could I just do "inv_".

"inv_" really just refers to temp.inv_, inv_ (global), or player.inv_. Not "this.inv_", your method works because you're making the variable global.

It is bad practice in general, and you should really just use with ( item ).

By glancing at your script, however, you seem to be making things hard for yourself. Why don't you use the index numbers?


PHP Code:

        tokens invcontents.(@"inv_" i); 

Personally, I do not understand what you're trying to accomplish here.

Chandler 04-10-2007 06:23 PM

getDynamicVarNames()?

killerogue 04-10-2007 09:05 PM

Basically, I have loaded variables from a text file into invcontents.

There maybe one or more variables in the text file starting with the prefix "inv_" now, then it uses the for loop to go through everything that starts with, "inv_". After that it checks what text is after "inv_".

Like for example, in the text file I might have this:

inv_item=name,icon,power,blah

After I check if "item=" is after inventory or not or any of the other words after it, I'm creating a substring of everything after the "=" and tokenizing it to turn it into an array.

This is what I'm trying to do, put all the information in the text file AFTER the "itemname=" into an array.



What Stefan suggested, so something like this would be fine?
PHP Code:

  with (invcontents)
  {
    for (
igetstringkeys("this.inv_"))
    {
      if (
== "item=")
      {
        
tokens getstringkeys("invcontents.inv_" i)
      } 


Rapidwolve 04-10-2007 09:30 PM

Quote:

Originally Posted by Chandler (Post 1298770)
getDynamicVarNames()?

He's not trying to get the variable names hes trying to return whatever is after the 'inv_'

killerogue 04-10-2007 09:43 PM

Quote:

Originally Posted by Rapidwolve (Post 1298821)
He's not trying to get the variable names hes trying to return whatever is after the 'inv_'

I'm trying to return whatever is after,

'inv_itemnamehere='

Like,

'inv_item=Name,ID,Icon'

I'm trying to get all that information after item, or weapon --which is represented by 'i'-- and put it into an array.

Rapidwolve 04-10-2007 09:50 PM

Hence like I said, everything after 'inv_', what your doing is right so far. Just use:
PHP Code:

with (findNPC("Database")){
  for (
temp.igetstringkeys("this.inv_"){
    echo(
temp.i); // Returns everything that starts with 'inv_'
  
}



killerogue 04-10-2007 09:53 PM

*sigh*

I'm NOT using a database, it's an object dude, and I'm trying to return the information AFTER 'inv_tempi='. 'temp.i' stands for either "item=" or "weap=" and the information after the "=" is what I'm trying to get and pur into an array.

Chandler 04-10-2007 10:32 PM

Load the arrays first
PHP Code:

temp.playerData = new TStaticVar(); 
temp.playerData.loadVars("thistextfile.arc");
for (
temp.currentDatatemp.playerData.getDynamicVarNames())
  if (
temp.currentData.starts("inv"))
    
temp.foundData temp.currentData

Then you would like to load the temp.foundData, with whatever. I think that should work

killerogue 04-10-2007 10:36 PM

Quote:

Originally Posted by Chandler (Post 1298843)
Load the arrays first
PHP Code:

temp.playerData = new TStaticVar(); 
temp.playerData.loadVars("thistextfile.arc");
for (
temp.currentDatatemp.playerData.getDynamicVarNames())
  if (
temp.currentData.starts("inv"))
    
temp.foundData temp.currentData

Then you would like to load the temp.foundData, with whatever. I think that should work


I really don't get this


TEXT FILE BELOW

[TEXTFILE]
inv_item=Name,Icon,ID
inv_item=Name,Icon,ID
inv_weap=Name,Icon,ID
[/TEXTFILE]

I load those vars from the text file into an object!
NOW,

OBJECT WITH BOLD
[OBJECT]
inv_item=Name,Icon,ID
inv_item=Name,Icon,ID
inv_weap=Name,Icon,ID
[/OBJECT]

Everything that's bolded and underlined, is what I'm trying to get from what I loaded into the object!




I loaded inv_item=blah into the object, now I'm trying to get "blah" back.

Rapidwolve 04-10-2007 10:39 PM

I'm not sure if that will work, your just overwriting the previous object. I would suggest 'item_id=Name, Icon'

killerogue 04-10-2007 10:43 PM

Quote:

Originally Posted by Rapidwolve (Post 1298853)
I'm not sure if that will work, your just overwriting the previous object. I would suggest 'item_id=Name, Icon'

._. ......I suppose that would be easier, and more sensible, wouldn't it.
Also, a more sensible question would be, if, when I load "inv_item=Blah,Blah1,Blah2" from the text file into the object, does it become an array?

E: Oh, it does! Thanks for helping, and sorry for flipping out on you guys. Just got quite frustrated. >_<

Rapidwolve 04-10-2007 10:48 PM

Quote:

Originally Posted by killerogue (Post 1298855)
._. ......I suppose that would be easier, and more sensible, wouldn't it.
Also, a more sensible question would be, if, when I load "inv_item=Blah,Blah1,Blah2" from the text file into the object, does it become an array?

I believe so, yes.

cbk1994 04-11-2007 01:09 AM

inv_item=Blah,Blah1,Blah2

If you meant something like

inv_item1337=id=1,quantity=1,foo=3

etc.

You could use getDynamicVarNames(); Use a str.starts(); and then use a ( "varname_" @ ITEMNAME ) = array;
then when accessing you could create a TStaticVar() and obj.LoadVarsFromArrays();

Inverness 04-11-2007 01:17 AM

Putting the variable names are part of the item array data is very bad in my opinion. What I've done with more simple systems like that is set a serverr variable that show whats at each index:
serverr.itemvars_<typename>=id,qty,name,image,weig ht
clientr.item<number>=23,1,"Fancy Sword",fancysword.png,5000

Then you use function like getitemvar(id, varname) and return the item index that matches the index of the varname in the serverr string.

Edit: With the simplest system I would have the type of item as the first variable, then I would find the right variable list for that type with a function before continuing so all items didn't require a master list.


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

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