- 18
- Posts
- 7
- Years
- Age 26
- California
- Seen Jan 29, 2023
Hello, this is more of a general coding question in Ruby, but at this point I'm stuck and need some help.
So what I'm currently trying to do is to make it so that a pokemart will only have a limited amount of an item on sale before a timer countsdown and the vendor restocks. So what I thought I would do would be to call a function when a global switch is on and have it countdown from there. The amount being sold would be stored in an array that coincides with the item's index. The problem is that the first time I talk to the cashier, it works pretty good, but when I talk to it the second time, without calling the function, the array would be stored as nil, and in ruby, we can't dup a nil. Can someone just push me in the right path?
Also, I've been trying to show the amount of items in the mart that they would have for sale, but the drawItem function in the Window_PokemonMart class doesn't have an index number I can use or a any returns I can call to use my array.
Here's the code I altered in PScreen_Mart.
Here's what I did to fix that issue.
I'm still having problems drawing the text though.
Thanks for checking out my problem.
Also, I've been trying to show the amount of items in the mart that they would have for sale, but the drawItem function in the Window_PokemonMart class doesn't have an index number I can use or a any returns I can call to use my array.
Here's the code I altered in PScreen_Mart.
Spoiler:
Code:
class PokemonMartScreen
def initialize(scene,stock,stk)
@scene=scene
@stock=stock
@adapter=$PokemonBag ? PokemonMartAdapter.new : RpgxpMartAdapter.new
@stk=stk.dup
end
def pbConfirm(msg)
return @scene.pbConfirm(msg)
end
def pbDisplay(msg)
return @scene.pbDisplay(msg)
end
def pbDisplayPaused(msg)
return @scene.pbDisplayPaused(msg)
end
def pbBuyScreen
@scene.pbStartBuyScene(@stock,@adapter)
item=0
loop do
item,[email protected]
quantity=0
break if item==0
[email protected](item)
[email protected](item)
if @adapter.getMoney()<price
pbDisplayPaused(_INTL("You don't have enough money."))
next
elsif @stk[max]<=0
pbDisplayPaused(_INTL("We don't have anymore in stock."))
next
end
if pbIsImportantItem?(item)
if !pbConfirm(_INTL("Certainly. You want {1}.\r\nThat will be ${2}. OK?",itemname,price))
next
end
quantity=1
else
maxafford=(price<=0) ? BAGMAXPERSLOT : @adapter.getMoney()/price
maxafford=BAGMAXPERSLOT if maxafford>BAGMAXPERSLOT
maxafford=@stk[max] if maxafford>@stk[max]
[email protected](
_INTL("{1}? Certainly.\r\nHow many would you like?",itemname),item,maxafford)
if quantity==0
next
end
price*=quantity
if !pbConfirm(_INTL("{1}, and you want {2}.\r\nThat will be ${3}. OK?",itemname,quantity,price))
next
end
end
if @adapter.getMoney()<price
pbDisplayPaused(_INTL("You don't have enough money."))
next
end
added=0
quantity.times do
if [email protected](item)
break
end
added+=1
end
if added!=quantity
added.times do
if [email protected](item)
raise _INTL("Failed to delete stored items")
end
end
pbDisplayPaused(_INTL("You have no more room in the Bag."))
else
@adapter.setMoney(@adapter.getMoney()-price)
for i in [email protected]
if pbIsImportantItem?(@stock[i]) && $PokemonBag.pbQuantity(@stock[i])>0
@stock[i]=nil
end
end
@stock.compact!
pbDisplayPaused(_INTL("Here you are!\r\nThank you!"))
@stk[max]-=quantity
if $PokemonBag
if quantity>=10 && pbIsPokeBall?(item) && hasConst?(PBItems,:PREMIERBALL)
if @adapter.addItem(getConst(PBItems,:PREMIERBALL))
pbDisplayPaused(_INTL("I'll throw in a Premier Ball, too."))
end
end
end
end
end
@scene.pbEndBuyScene
end
-------------------------------------
def pbSetStock(itemnum)
stk=Array.new
for i in 0...itemnum
stk.push[i]=rand(5+$Trainer.trlevel)
end
return stk
end
def pbPokemonMart(stock,speech=nil,cantsell=false)
for i in 0...stock.length
stock[i]=getID(PBItems,stock[i]) if !stock[i].is_a?(Integer)
if !stock[i] || stock[i]==0 ||
(pbIsImportantItem?(stock[i]) && $PokemonBag.pbQuantity(stock[i])>0)
stock[i]=nil
end
end
if $game_switches[60]==true
stk=pbSetStock(stock.length)
end
stock.compact!
commands=[]
cmdBuy=-1
cmdSell=-1
cmdQuit=-1
commands[cmdBuy=commands.length]=_INTL("Buy")
commands[cmdSell=commands.length]=_INTL("Sell") if !cantsell
commands[cmdQuit=commands.length]=_INTL("Quit")
cmd=Kernel.pbMessage(
speech ? speech : _INTL("Welcome!\r\nHow may I serve you?"),
commands,cmdQuit+1)
loop do
if cmdBuy>=0 && cmd==cmdBuy
scene=PokemonMartScene.new
screen=PokemonMartScreen.new(scene,stock,stk)
screen.pbBuyScreen
elsif cmdSell>=0 && cmd==cmdSell
scene=PokemonMartScene.new
screen=PokemonMartScreen.new(scene,stock,stk)
screen.pbSellScreen
else
Kernel.pbMessage(_INTL("Please come again!"))
break
end
cmd=Kernel.pbMessage(
_INTL("Is there anything else I can help you with?"),commands,cmdQuit+1)
end
$game_temp.clear_mart_prices
end
Here's what I did to fix that issue.
Spoiler:
Code:
class PokemonMartScreen
def initialize(scene,stock,stk)
@scene=scene
@stock=stock
@adapter=$PokemonBag ? PokemonMartAdapter.new : RpgxpMartAdapter.new
@stk=stk.dup
end
def pbConfirm(msg)
return @scene.pbConfirm(msg)
end
def pbDisplay(msg)
return @scene.pbDisplay(msg)
end
def pbDisplayPaused(msg)
return @scene.pbDisplayPaused(msg)
end
def pbBuyScreen
@scene.pbStartBuyScene(@stock,@adapter)
item=0
loop do
item,[email protected]
quantity=0
break if item==0
[email protected](item)
[email protected](item)
if @adapter.getMoney()<price
pbDisplayPaused(_INTL("You don't have enough money."))
next
elsif @stk[max]<=0
pbDisplayPaused(_INTL("We don't have anymore in stock."))
next
end
if pbIsImportantItem?(item)
if !pbConfirm(_INTL("Certainly. You want {1}.\r\nThat will be ${2}. OK?",itemname,price))
next
end
quantity=1
else
maxafford=(price<=0) ? BAGMAXPERSLOT : @adapter.getMoney()/price
maxafford=BAGMAXPERSLOT if maxafford>BAGMAXPERSLOT
maxafford=@stk[max] if maxafford>@stk[max]
[email protected](
_INTL("{1}? Certainly.\r\nHow many would you like?",itemname),item,maxafford)
if quantity==0
next
end
price*=quantity
if !pbConfirm(_INTL("{1}, and you want {2}.\r\nThat will be ${3}. OK?",itemname,quantity,price))
next
end
end
if @adapter.getMoney()<price
pbDisplayPaused(_INTL("You don't have enough money."))
next
end
added=0
quantity.times do
if [email protected](item)
break
end
added+=1
end
if added!=quantity
added.times do
if [email protected](item)
raise _INTL("Failed to delete stored items")
end
end
pbDisplayPaused(_INTL("You have no more room in the Bag."))
else
@adapter.setMoney(@adapter.getMoney()-price)
for i in [email protected]
if pbIsImportantItem?(@stock[i]) && $PokemonBag.pbQuantity(@stock[i])>0
@stock[i]=nil
end
end
@stock.compact!
pbDisplayPaused(_INTL("Here you are!\r\nThank you!"))
@stk[max]-=quantity
$stk.pbMinusStock(quantity,max)
if $PokemonBag
if quantity>=10 && pbIsPokeBall?(item) && hasConst?(PBItems,:PREMIERBALL)
if @adapter.addItem(getConst(PBItems,:PREMIERBALL))
pbDisplayPaused(_INTL("I'll throw in a Premier Ball, too."))
end
end
end
end
end
@scene.pbEndBuyScene
end
--------------
class MartStock
def initialize
@stk=Array.new
end
def pbSetStock(itemnum)
for i in 0...itemnum
@stk.push[i]=rand(5+$Trainer.trlevel)
end
end
def pbGetStock
return @stk
end
def pbMinusStock(qty,index)
@stk[index]-=qty
end
end
def getStock
$stk=MartStock.new
end
def pbPokemonMart(stock,speech=nil,cantsell=false)
for i in 0...stock.length
stock[i]=getID(PBItems,stock[i]) if !stock[i].is_a?(Integer)
if !stock[i] || stock[i]==0 ||
(pbIsImportantItem?(stock[i]) && $PokemonBag.pbQuantity(stock[i])>0)
stock[i]=nil
end
end
if $game_switches[60]==true
$stk.pbSetStock(stock.length)
mstk=$stk.pbGetStock
else
mstk=$stk.pbGetStock
end
stock.compact!
commands=[]
cmdBuy=-1
cmdSell=-1
cmdQuit=-1
commands[cmdBuy=commands.length]=_INTL("Buy")
commands[cmdSell=commands.length]=_INTL("Sell") if !cantsell
commands[cmdQuit=commands.length]=_INTL("Quit")
cmd=Kernel.pbMessage(
speech ? speech : _INTL("Welcome!\r\nHow may I serve you?"),
commands,cmdQuit+1)
loop do
if cmdBuy>=0 && cmd==cmdBuy
scene=PokemonMartScene.new
screen=PokemonMartScreen.new(scene,stock,mstk)
screen.pbBuyScreen
elsif cmdSell>=0 && cmd==cmdSell
scene=PokemonMartScene.new
screen=PokemonMartScreen.new(scene,stock,mstk)
screen.pbSellScreen
else
Kernel.pbMessage(_INTL("Please come again!"))
break
end
cmd=Kernel.pbMessage(
_INTL("Is there anything else I can help you with?"),commands,cmdQuit+1)
end
$game_temp.clear_mart_prices
end
I'm still having problems drawing the text though.
Thanks for checking out my problem.
Last edited: