About Script Box & Variable

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
Hi, I'm pretty basic in scripting, so here is a problem i encounter last night,

so i decided to make two(or more) main character in my game and will tells their story respectively. and of course it need a separated inventory.

the way i am doing it is by collecting all the item that gotten so far from the inventory and save it to variables (a lot of variables) and subtract all the item in my inventory by the amount of their own value so it will erase them (giving 0 value). and whenever i need it, i will just add all the item by the amount that saved in the variables.

my problem is: the way i am doing item save is using a loop and increase in variable. err.. maybe i will show you the event script instead
Problem1.PNG

So by the logic i use there,
Variable 24 = 22
Variable 25 = 1
So they will add variable 24 and 25 by 1 as long as variable 24 is less than 24(value)


and the script will results in as far as i know
$game_variables[22] = $game_party.item_number($data_items[1]);
$game_variables[23] = $game_party.item_number($data_items[2]);

and so on.

*variable 22 and 23 is variables to save the amounts of certain item. on above, i uses it to save Potion and Hi-Potion.

but somehow when i play test it, it will return
error1.PNG

My real question is:
1. Am i doing it wrong on implementing my logic into the above script?
2.
Is the script box use default concatenation or it read each line as different line? since the script was like:
line n: $game_variables[$game_variables[24]] =

line n+1: $game_party.item_number($data_items
line n+2: [$game_variables[25]])
3. Is there any efficient way to implement lots of inventory save, inventory delete, or inventory add?


Any help is appreciated.
 

Attachments

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
31,365
Reaction score
7,675
First Language
German
Primarily Uses
RMMV
your problem is that you allowed the linebreak at the wrong position, the interpreter doesn't understand that it needs to continue on the next line

the array bracket needs to begin on the line of data_items, not on the following line like in your screenshot
 

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
in the script box, i insert the script like:
$game_variables[$game_variables[24]] = $game_party.item_number($data_items [$game_variables[25]])

but then the script box break them into
: $game_variables[$game_variables[24]] =
: $game_party.item_number($data_items
: [$game_variables[25]])


i don't understand what make me allowed the linebreak in the first place since, i mean i just insert it and they break by themself.

how do i fix that?
 

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
your problem is that you allowed the linebreak at the wrong position, the interpreter doesn't understand that it needs to continue on the next line

the array bracket needs to begin on the line of data_items, not on the following line like in your screenshot
damn i'm such a noob, after some sleep and reading your word carefully now i understand what you mean.
thank you
 

dsiver144

Peaceful Days Dev
Veteran
Joined
Sep 19, 2013
Messages
263
Reaction score
826
First Language
Vietnamese
Primarily Uses
RMVXA
You can do like this in script box.
v = $game_variables
p = $game_party
i = $data_items
v[v[24]] = p.item_number(i[v[25]])
 

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
You can do like this in script box.
v = $game_variables
p = $game_party
i = $data_items
v[v[24]] = p.item_number(i[v[25]])
oh, basic iteration. i don't know if i can do that in the script box.

thank you again dsiver144, it's make my life easier now.
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
In the scriptbox you can put almost any code that is written Ruby. I am also pretty sure you can store a whole inventory in a single variable as an array of quantities. I never tried to load a save where a variable has been saved in such a way but it should work
 

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
yeah, i think i need to learn to use array inside a variable to make matters more efficient rather than to loops it
 

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
Well, something like this should work:
Code:
my_array = [0] #having at least one element in the array makes it easier to keep track of items IDs.
for i in 1..($data_items.size - 1)
  my_array << $game_party.item_number($data_items[i])
  $game_party.gain_item($data_items[i], my_array[i], false)
  #when set to false it does not remove equipped items, change to true to remove them.
  #however this only applies to something that can actually be equipped (like weapons for example).
end
$game_variables[id] = my_array

This automatically removes an item from player's inventory after storing that item quantity in the array. After checking for every item it saves the whole array in a variable. Change "id" to be the id of the variable you want to use.

As in the comment box, you can set my_array to have one element from the beginning, this helps since items IDs starts from id number 1 and so having the same index for both items and their respective quantity is much better than having to calculate the index every time.

NOTE:
If you wanna set my_array to have no elements from the beginning (like this: "my_array = [ ]") remember that every quantity stored in my_array is related to the item with index (quantity_index + 1).

Example: my_array[1] is the quantity of $data_items[2]

After saving your inventory like this you can just use something similar to load it:
Code:
qty = $game_variables[id] #id is the id of the variable where you stored the inventory you wanna load.
for i in 1..(qty.size - 1)
  $game_party.gain_item($data_items[i], qty[i], false)
end

I didn't test the code but it should work.
 
Last edited:

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
Well, something like this should work:
Code:
my_array = [0] #having at least one element in the array makes it easier to keep track of items IDs.
for i in 1..($data_items.size - 1)
  my_array << $game_party.item_number($data_items[i])
  $game_party.gain_item($data_items[i], my_array[i], false)
  #when set to false it does not remove equipped items, change to true to remove them.
  #however this only applies to something that can actually be equipped (like weapons for example).
end
$game_variables[id] = my_array

This automatically removes an item from player's inventory after storing that item quantity in the array. After checking for every item it saves the whole array in a variable. Change "id" to be the id of the variable you want to use.

As in the comment box, you can set my_array to have one element from the beginning, this helps since items IDs starts from id number 1 and so having the same index for both items and their respective quantity is much better than having to calculate the index every time.

NOTE:
If you wanna set my_array to have no elements from the beginning (like this: "my_array = [ ]") remember that every quantity stored in my_array is related to the item with index (quantity_index + 1).

Example: my_array[1] is the quantity of $data_items[2]

After saving your inventory like this you can just use something similar to load it:
Code:
qty = $game_variables[id] #id is the id of the variable where you stored the inventory you wanna load.
for i in 1..(qty.size - 1)
  $game_party.gain_item($data_items[i], qty[i], false)
end

I didn't test the code but it should work.
So this is how i separate the inventory in my game, there will be 2 party for example, [party A] and [party B]
- [Party A] Save all the item into variable
Code:
## For Items
my_array = [0] #having at least one element in the array makes it easier to keep track of items IDs.
for i in 1..($data_items.size - 1)
  my_array << $game_party.item_number($data_items[i])
end
$game_variables[1] = my_array

##For Weapons
my_array = [0]
for i in 1..($data_weapons.size - 1)
  my_array << $game_party.item_number($data_weapons[i])
  $game_party.gain_item($data_weapons[i], my_array[i], false)
end
$game_variables[2] = my_array

##For Armors
my_array = [0]
for i in 1..($data_armors.size - 1)
  my_array << $game_party.item_number($data_armors[i])
  $game_party.gain_item($data_armors[i], my_array[i], false)
end
$game_variables[3] = my_array

- Delete all the item
Code:
qty = $game_variables[1] # could be 1, 2 , or 3
for i in 1..(qty.size - 1)
  $game_party.lose_item($data_items[i], qty[i], false)
end

- and then switch to [party B]

- When the story reach a certain part, the party will be switched to the [party A], and will return their item
Code:
qty = $game_variables[1] #could be 1 or 2 or 3
for i in 1..(qty.size - 1)
  $game_party.gain_item($data_items[i], qty[i], false)
end

alright, if use these, it will reduce the usage of many variables, instead i will only use 3 variable for item, weapon and armor

EDIT: It perfectly worked, thank you Heirukichi
 
Last edited:

Heirukichi

Veteran
Veteran
Joined
Sep 24, 2015
Messages
1,421
Reaction score
596
First Language
Italian
Primarily Uses
RMVXA
You got it perfectly, that's exactly what I meant. I am glad I have been able to help you.

If you want to reduce your variables usage even further you can store weapons, armors and items in the same array. However since you have plenty of variables I don't think it will be necesssary.

Let me know if you need anything else.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,865
Messages
1,017,059
Members
137,575
Latest member
akekaphol101
Top