Jump to content
Sign in to follow this  
Darth

Reseting a function

Recommended Posts

Darth

I'm making an inventory / shop system that I may or may not use later and the sell function only works the first time, afterward it always shows 0 for the item name

#include <GUIConstants.au3>
#include<array.au3>

$test = 15
$price = 7
Dim $Items[20] = ["Potion",2,"Mind-up",4,"Bob",1,"epic",8,"monkey",0,"lol",12,"stuff",0,"apple",999,"qwerty","number","test",$test]
Dim $SItems[30] = ["Potion",20,10,"Mind-up",40,20,"Bob",100,50,"epic",85,25,"monkey",70,82,"lol",2,96,"stuff",45,12,"apple",0,1,"qwerty","number","money","test",$test,$price]

$pgold = 1000
$sgold = 2000
Opt("GUIOnEventMode", 1)
GUICreate("item test",200,180,-1,-1)
$edit = GUICtrlCreateEdit("",0,0,200,150,BitOr($ES_READONLY,$WS_VSCROLL))
$G = GUICtrlCreateLabel("Gold: " & $pGold,100,155)

GUISetState (@SW_SHOW)

Func Item_list($array,$size,$start,$advance,$output)
    $var = $start
    GUICtrlSetData($output,"")
    While ($var <= $size)
        If $array[$var] > 0 Then
            GUICtrlSetData($output,GUICtrlRead($output) & $array[$var-1] & ": " & $array[$var] & @CRLF)
        
        EndIf
        $var = $var + $advance
    WEnd
EndFunc

Item_list($items,20,1,2,$edit)

GUICreate("Shop",200,230,-1,-1)
$shop = GUICtrlCreateEdit("",0,0,200,150,BitOr($ES_READONLY,$WS_VSCROLL))
$GS = GUICtrlCreateLabel("Gold: " & $sGold,0,155)
$PSI = GUICtrlCreateInput("",1,175)
$PS = GUICtrlCreateButton("Price",60,172)
$BI = GUICtrlCreateInput("",1,200,59,20)
$buy = guictrlcreatebutton("Buy",60,197)
GUISetState (@SW_SHOW)
GUICtrlSetOnEvent($PS, "Price")
GUICtrlSetOnEvent($buy,"Buy")

Item_list($SItems,30,1,3,$shop)

Func Price()
    $search = GUICtrlRead($PSI)
$find = _ArraySearch($SItems,$search)
MsgBox(0,"","A " & $SItems[$find] & " costs " & $SItems[$find + 2] & " Gold")
EndFunc

Func Buy()
    $search = GUICtrlRead($BI)
    $find = _ArraySearch($SItems,$search)
    $ammount = InputBox("Buy","How many " & $SItems[$Find] & " would you like to buy?",""," M")
    $cost = ($ammount * $SItems[$find + 2])
    $pgold = $pgold - $cost
    $sgold = $sgold + $cost
    $SItems[$find + 1] = $SItems[$find + 1] - $ammount
    $find = _ArraySearch($Items,$search)
    $Items[$find + 1] = $Items[$find + 1] + $ammount
    Item_list($items,20,1,2,$edit)
    Item_list($SItems,30,1,3,$shop)
    GUICtrlSetData($g,$pgold)
    GUICtrlSetData($GS,$sgold)
    msgbox(0,"Buy","You have bought " & $ammount & " " & $Items[$find] & " for " & $cost & " gold.")
EndFunc
while(1)
    sleep(100)
WEnd

Share this post


Link to post
Share on other sites
enaiman

For a reason which escapes my judgement the zero's in your arrays are seen as matches when doing _ArraySearch. That's why the result is messed up.

As a workaround you might store the numbers as strings and do all the math by converting them to numbers.

To see how it works without zero's, just run the script.:

#include <GUIConstants.au3>
#include<array.au3>

$test = 15
$price = 7
Dim $Items[20] = ["Potion",2,"Mind-up",4,"Bob",1,"epic",8,"monkey",10,"lol",12,"stuff",10,"apple",999,"qwerty","number","test",$test]
Dim $SItems[30] = ["Potion",20,10,"Mind-up",40,20,"Bob",100,50,"epic",85,25,"monkey",70,82,"lol",2,96,"stuff",45,12,"apple",10,1,"qwerty","number","money","test",$test,$price]

$pgold = 1000
$sgold = 2000
Opt("GUIOnEventMode", 1)
GUICreate("item test",200,180,-1,-1)
$edit = GUICtrlCreateEdit("",0,0,200,150,BitOr($ES_READONLY,$WS_VSCROLL))
$G = GUICtrlCreateLabel("Gold: " & $pGold,100,155)

GUISetState (@SW_SHOW)

Func Item_list($array,$size,$start,$advance,$output)
    $var = $start
    GUICtrlSetData($output,"")
    While ($var <= $size)
        If $array[$var] > 0 Then
            GUICtrlSetData($output,GUICtrlRead($output) & $array[$var-1] & ": " & $array[$var] & @CRLF)
        
        EndIf
        $var = $var + $advance
    WEnd
EndFunc

Item_list($items,20,1,2,$edit)

GUICreate("Shop",200,230,-1,-1)
$shop = GUICtrlCreateEdit("",0,0,200,150,BitOr($ES_READONLY,$WS_VSCROLL))
$GS = GUICtrlCreateLabel("Gold: " & $sGold,0,155)
$PSI = GUICtrlCreateInput("",1,175)
$PS = GUICtrlCreateButton("Price",60,172)
$BI = GUICtrlCreateInput("",1,200,59,20)
$buy = guictrlcreatebutton("Buy",60,197)
GUISetState (@SW_SHOW)
GUICtrlSetOnEvent($PS, "Price")
GUICtrlSetOnEvent($buy,"Buy")

Item_list($SItems,30,1,3,$shop)

Func Price()
    $search = GUICtrlRead($PSI)
$find = _ArraySearch($SItems,$search)
MsgBox(0,"","A " & $SItems[$find] & " costs " & $SItems[$find + 2] & " Gold")
EndFunc

Func Buy()
    $search = GUICtrlRead($BI)
    $find = _ArraySearch($SItems,$search)
    $ammount = InputBox("Buy","How many " & $SItems[$Find] & " would you like to buy?",""," M")
    $cost = ($ammount * $SItems[$find + 2])
    $pgold = $pgold - $cost
    $sgold = $sgold + $cost
    $SItems[$find + 1] = $SItems[$find + 1] - $ammount
    $find = _ArraySearch($Items,$search)
    $Items[$find + 1] = $Items[$find + 1] + $ammount
    Item_list($items,20,1,2,$edit)
    Item_list($SItems,30,1,3,$shop)
    GUICtrlSetData($g,$pgold)
    GUICtrlSetData($GS,$sgold)
    msgbox(0,"Buy","You have bought " & $ammount & " " & $Items[$find] & " for " & $cost & " gold.")
EndFunc
while(1)
    sleep(100)
WEnd

I don't know - maybe it's not a good idea to mix strings and numbers in the same array. You might consider as well using multi-dimensional arrays.


SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites
Darth

I found the problem, it's because it has both numbers and strings in the same array. I just split up the arrays

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×