SphereCommunity
Item Counter - Printable Version

+- SphereCommunity (https://forum.spherecommunity.net)
+-- Forum: Sphere 0.56d (/Forum-Sphere-0-56d)
+--- Forum: Script Help (/Forum-Script-Help)
+--- Thread: Item Counter (/Thread-Item-Counter)



Item Counter - Chukwuemeka - 05-03-2017 11:54 AM

I'm trying to make an item counter to check resources amount in the game, etc
I'm probably doing something dumb which I can't figure out.

Code:
[FUNCTION findresources]
    IF (<isempty <ARGS>>)
    SRC.SYSMESSAGE You must set an ID.
    ELSE
    LOCAL.totalitemcount = 0
    FORINSTANCES <ARGS>
    LOCAL.totalitemcount = (<eval <LOCAL.totalitemcount> + <AMOUNT>>)
    ENDFOR
    SRC.SYSMESSAGE Success. Found <eval <LOCAL.totalitemcount>> items.
    ENDIF
ENDIF

When I set an invalid ID, it returns "Found 0 items", but when I set a valid ID, it does not work. Confused

[Bonus question lol]

How can I make it write a file with found <UID>s and <AMOUNT>s?
I tried with WRITEFILE and WRITELINE but sphere seems to ignore it.


RE: Item Counter - Coruja - 05-03-2017 02:56 PM

your code have an extra ENDIF at the end, maybe this is breaking the code execution early

also <isempty <ARGS>> will only tell if you had called the function using any args or not, but doesn't check if this arg is an valid item. Sphere stores all ITEMDEFs/CHARDEFs/etc on DEFs, so the best way to check if an item exists is just check if his DEF exists (eg: <DEF0.i_something>)

and you're also adding the <AMOUNT> to total value without use <eval>, so probably sphere is storing it as string (text) instead number (that's why it always return 0 even when the itemdef is valid)

I doesn't tested this code, but maybe it will work fine
Code:
[FUNCTION findresources]
IF !(<DEF0.<ARGS>>)
  SYSMESSAGE Invalid ID
  return 1
ENDIF
LOCAL.Total = 0
FORINSTANCES <ARGS>
  LOCAL.Total += <AMOUNT>
ENDFOR
SYSMESSAGE Success. Found <dLOCAL.Total> items.



RE: Item Counter - Chukwuemeka - 05-04-2017 09:28 AM

Thanks Coruja. I figured out how to make this work... kinda. Because now I'm having another problem with this.

It starts searching:

Code:
22:07:(k.scp,664)wooden box 04007056b 10
22:07:(k.scp,664)bag 0400706c4 30

Then I get a huge error:

Code:
22:07:DEBUG: thread (15272)  |  # | __ function _ | ticks passed from previous function start __
22:07:DEBUG:>>         15272     |  0 | NetworkManager::processAllInput | +0
22:07:DEBUG:>>         15272     |  1 |   NetworkInput::processInput | +0
22:07:DEBUG:>>         15272     |  2 |    NetworkInput::processData | +0
22:07:DEBUG:>>         15272     |  3 |    NetworkInput::processData | +0
22:07:DEBUG:>>         15272     |  4 | NetworkInput::processGameClientData | +0
22:07:DEBUG:>>         15272     |  5 | PacketSpeakReqUNICODE::onReceive | +0
22:07:DEBUG:>>         15272     |  6 |   CClient::Event_TalkUNICODE | +0
22:07:DEBUG:>>         15272     |  7 |       CClient::Event_Command | +0
22:07:DEBUG:>>         15272     |  8 |                CChar::r_Verb | +0
22:07:DEBUG:>>         15272     |  9 |             CObjBase::r_Verb | +0
22:07:DEBUG:>>         15272     | 10 |           CScriptObj::r_Call | +0
22:07:DEBUG:>>         15272     | 11 |     CScriptObj::OnTriggerRun | +0 <-- exception catch point (below is guessed and could be incorrect!)
22:07:DEBUG:>>         15272     | 12 | CScriptObj::OnTriggerForLoop | +0
22:07:DEBUG:>>         15272     | 13 |                CScript::Seek | +31
22:07:DEBUG:>>         15272     | 14 |    CacheableScriptFile::Seek | +0
22:07:CRITICAL:(k.scp,662)"Access Violation" (0x10c02a), in CScriptObj::TriggerRun() #2 "forinstance"
22:07:DEBUG:key 'ENDFOR' runtype '1' pargs '04B7D568' ret '' [119B6A20]
22:07:54:'admin' commands 'findresources'=1
22:07:DEBUG: thread (15272)  |  # | __ function _ | ticks passed from previous function start __
22:07:DEBUG:>>         15272     |  0 | NetworkManager::processAllInput | +0
22:07:DEBUG:>>         15272     |  1 |   NetworkInput::processInput | +0
22:07:DEBUG:>>         15272     |  2 |    NetworkInput::processData | +0
22:07:DEBUG:>>         15272     |  3 |    NetworkInput::processData | +0
22:07:DEBUG:>>         15272     |  4 | NetworkInput::processGameClientData | +0
22:07:DEBUG:>>         15272     |  5 | PacketSpeakReqUNICODE::onReceive | +0
22:07:DEBUG:>>         15272     |  6 |   CClient::Event_TalkUNICODE | +0
22:07:DEBUG:>>         15272     |  7 |       CClient::Event_Command | +0
22:07:DEBUG:>>         15272     |  8 |                CChar::r_Verb | +0
22:07:DEBUG:>>         15272     |  9 |             CObjBase::r_Verb | +0
22:07:DEBUG:>>         15272     | 10 |           CScriptObj::r_Call | +0
22:07:DEBUG:>>         15272     | 11 |     CScriptObj::OnTriggerRun | +0 <-- exception catch point (below is guessed and could be incorrect!)
22:07:DEBUG:>>         15272     | 12 | CScriptObj::OnTriggerForLoop | +0
22:07:DEBUG:>>         15272     | 13 | CResourceBase::ResourceGetID | +0
22:07:DEBUG:>>         15272     | 14 |          CExpression::GetVal | +0
22:07:DEBUG:>>         15272     | 15 |      CExpression::GetValMath | +0
22:07:CRITICAL:(k.scp,654)"Access Violation" (0x10c02a), in CScriptObj::TriggerRun() #2 "forinstance"
22:07:DEBUG:key 'FORINSTANCES' runtype '1' pargs '04B7D568' ret '' [119B6A20]

I'm on 56b, I have no clue what is happening. ahaha

Thanks!


RE: Item Counter - pointhz - 05-04-2017 06:02 PM

Your first script should work just fine with one short endif. To give that kind of errors you must have changed something