rad.noire

Regular
Regular
Joined
Mar 19, 2017
Messages
37
Reaction score
6
First Language
English
Primarily Uses
N/A
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

  • Problem1.PNG
    Problem1.PNG
    15 KB · Views: 3
  • error1.PNG
    error1.PNG
    6.4 KB · Views: 2

Andar

Regular
Regular
Joined
Mar 5, 2013
Messages
39,883
Reaction score
11,841
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

Regular
Regular
Joined
Mar 19, 2017
Messages
37
Reaction score
6
First Language
English
Primarily Uses
N/A
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

Regular
Regular
Joined
Mar 19, 2017
Messages
37
Reaction score
6
First Language
English
Primarily Uses
N/A
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
 

NhatNguyen

Peaceful Days Dev
Regular
Joined
Sep 19, 2013
Messages
290
Reaction score
872
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

Regular
Regular
Joined
Mar 19, 2017
Messages
37
Reaction score
6
First Language
English
Primarily Uses
N/A
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

Regular
Regular
Joined
Sep 24, 2015
Messages
1,502
Reaction score
655
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

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

Heirukichi

Regular
Regular
Joined
Sep 24, 2015
Messages
1,502
Reaction score
655
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

Regular
Regular
Joined
Mar 19, 2017
Messages
37
Reaction score
6
First Language
English
Primarily Uses
N/A
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

Regular
Regular
Joined
Sep 24, 2015
Messages
1,502
Reaction score
655
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.
 

Latest Threads

Latest Profile Posts

So the concept for my Game Jam project is way more than I can complete before the deadline. But I think I found a way to get the core done and make the incomplete parts either not really noticeable or make them a part of the story for now. That sneaky, dastardly Krampus! Then I can complete the rest. Did I mention that this is fun?
I've been working on a game called "Eternal Sunset". Is there anywhere here i can "show it off" maybe? Wondering what others think.
What do you do when there are lots of offers online but you don't have even a slight chance of ever playing those myriads of games?
Ugh, mispelled my username.

Forum statistics

Threads
136,542
Messages
1,267,338
Members
180,212
Latest member
cameron_ekiert
Top