The following warnings occurred:
Warning [2] Use of undefined constant SAPI_NAME - assumed 'SAPI_NAME' (this will throw an Error in a future version of PHP) - Line: 3388 - File: inc/functions.php PHP 7.4.33-nmm7 (Linux)
File Line Function
/inc/functions.php 3388 errorHandler->error
/showthread.php 116 build_archive_link
Warning [2] Use of undefined constant IN_ARCHIVE - assumed 'IN_ARCHIVE' (this will throw an Error in a future version of PHP) - Line: 3331 - File: inc/functions.php PHP 7.4.33-nmm7 (Linux)
File Line Function
/inc/functions.php 3331 errorHandler->error
/inc/functions.php 3324 build_forum_breadcrumb
/showthread.php 195 build_forum_breadcrumb
Warning [2] Use of undefined constant IN_ARCHIVE - assumed 'IN_ARCHIVE' (this will throw an Error in a future version of PHP) - Line: 3331 - File: inc/functions.php PHP 7.4.33-nmm7 (Linux)
File Line Function
/inc/functions.php 3331 errorHandler->error
/showthread.php 195 build_forum_breadcrumb






Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Item Counter
Author Message
Chukwuemeka
Apprentice
*

Posts: 12
Likes Given: 0
Likes Received: 0 in 0 posts
Joined: Nov 2016
Reputation: 0



Post: #1
Item Counter
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.
05-03-2017 11:54 AM
Find all posts by this user Like Post Quote this message in a reply
Coruja
Sphere Developer
*****

Posts: 987
Likes Given: 5
Likes Received: 226 in 187 posts
Joined: Jul 2012
Reputation: 7

Dimension Shard

Post: #2
RE: Item Counter
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.
05-03-2017 02:56 PM
Find all posts by this user Like Post Quote this message in a reply
Chukwuemeka
Apprentice
*

Posts: 12
Likes Given: 0
Likes Received: 0 in 0 posts
Joined: Nov 2016
Reputation: 0



Post: #3
RE: Item Counter
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!
05-04-2017 09:28 AM
Find all posts by this user Like Post Quote this message in a reply
pointhz
Journeyman
*

Posts: 148
Likes Given: 1
Likes Received: 55 in 28 posts
Joined: Oct 2013
Reputation: 1



Post: #4
RE: Item Counter
Your first script should work just fine with one short endif. To give that kind of errors you must have changed something
05-04-2017 06:02 PM
Find all posts by this user Like Post Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 2 Guest(s)