Checking for object and amounts

So I have a command in my game that needs to check the player's entire inventory and all child inventory within, for a specific object; a "Small Stick".

Then it needs to check how many of these items the player is carrying (via clones) to see if they have enough to let's say build a fire, for example.

Would that be something like...

if  (Got(Small Stick)) {
 if (Small Stick.count >3) {
 }
else {
 }
}

Thanks in advance you guys, I greatly appreciate the help.


if you got a bunch of clones, they need an identifier/flag for them, for example:

<object name="potion">
  <attr name="type_of_in_game_item" type="string">potion</attr>
</object>

<object name="elixer">
  <attr name="type_of_in_game_item" type="string">elixer</attr>
</object>

// when you clone this Object, the clones will have the 'type_of_in_game_item" String Attribute with its Value as 'potion', which you can 'if check' for.

// scripting example:

potion_count = 0
foreach (object_variable, NAME_OF_OBJECT.NAME_OF_OBJECT_LIST_ATTRIBUTE) {
  if (GetString (object_variable, "type_of_in_game_item") = "potion") {
    potion_count = potion_count + 1
  }
}
if (potion_count > 4) {
  msg ("blah blah blah")
}

the problem however is in whether you got container Objects in your 'player' Player Object, that your searching-for Object is within those container Objects.

there's ways of handling it... some more messy than others...


the best method though is to handle the clones up front, so you don't have a bunch of clones (have a single container Object instead of a bunch of clones with an Integer Attribute for the quantity of them) ... basically: Sora's Stackable library, lol.

Pixie can probably help, as I think he implemented this design into his combat library code, and/or he (or maybe it was someone else) also updated Sora's Stackable Library to the current version of quest and its syntax.... at least I thought I saw someone doing this not too long back.


Use GetAllChildObjects to get objects in containers too. scopeInventory probably does that too for open containets which may ne better.

Use FilterByAttribute on the list to get the ones you want. You could filter by alias i would guess.


How might that look in code?

GetScopeInventory()
GetAllChildObjects
if  (Got(Small Stick)) {
 if (Small Stick.count >3) {
 }
else {
 }
}

I appreciate the replies so far!


oops, forgot about 'GetAllChildObjects', thanks Pixie!


count_integer_variable = 0
foreach (object_variable, GetAllChildObjects (player)) {
  if (StartsWith (object_variable.name, "Small Stick")) { // using the 'StartsWith' as this way you don't have to create an Attribute for its identification, instead of the use of the 'GetString' in my previous post's code
    count_integer_variable = count_integer_variable + 1
  }
}
if (count_integer_variable > 3) {
  // scripting
}
// optionally:
else {
  // scripting
}

I'm not sure this would work for my game. I'd have to ask Pixie.


Hello, Anonynn!

Plugging your object's name into HK's example, which utilizes Pixie's suggestion:

stick_count = NewObjectList()
foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "Small Stick")) {
    //msg ("Found a stick!") // for testing
    //msg (o.parent)  // for testing
    list add (stick_count, o)
  }
}
sticks = ListCount(stick_count)
if (sticks > 3) {
  msg ("You have more than 3 sticks. In fact, you have " + sticks + " sticks.") // replace this with your script(s)
}
else {
  msg ("") // replace this with your script(s)
  msg ("Not enough sticks.")  // replace this with your script(s)
}

Example game code:

<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Anonynn's Small Stick">
    <gameid>3b90cdd4-5cfd-4f44-976b-ab5609765cc7</gameid>
    <version>1.0</version>
    <firstpublished>2017</firstpublished>
    <start type="script">
    </start>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <description><![CDATA[This is a test game.<br/><br/>Enter {command:STICKME:STICKME} or {command:STICKCOUNT:STICKCOUNT}<br/><br/><br/>OR:<br/><br/>{command:childinventory:childinventory}]]></description>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
      <object name="bag">
        <inherit name="editor_object" />
        <object name="pouch">
          <inherit name="editor_object" />
          <object name="box">
            <inherit name="editor_object" />
            <object name="Small Stick">
              <inherit name="editor_object" />
            </object>
          </object>
        </object>
      </object>
    </object>
    <turnscript>
      <enabled />
      <script>
        HandleSingleCommand ("look")
      </script>
    </turnscript>
  </object>
  <command>
    <pattern>stickme</pattern>
    <script>
      CloneObjectAndMove (Small Stick, player)
      CloneObjectAndMove (Small Stick, pouch)
      CloneObjectAndMove (Small Stick, box)
      CloneObjectAndMove (Small Stick, bag)
    </script>
  </command>
  <command>
    <pattern>stickcount</pattern>
    <script><![CDATA[
      stick_count = NewObjectList()
      foreach (o, GetAllChildObjects(player)) {
        if (StartsWith(o.name, "Small Stick")) {
          msg ("Found a stick!")
          msg (o.parent)
          list add (stick_count, o)
        }
      }
      sticks = ListCount(stick_count)
      if (sticks > 3) {
        msg ("You have more than 3 sticks. In fact, you have " + sticks + " sticks.")
      }
      else {
        msg ("")
        msg ("Not enough sticks.")
      }
    ]]></script>
  </command>
  <command>
    <script>
      foreach (o, GetAllChildObjects(player)) {
        msg (o.name + ": is in " + o.parent.name)
      }
    </script>
    <pattern>childinventory</pattern>
  </command>
</asl>

Example game:

http://textadventures.co.uk/games/view/93e62fqi80kssodjqo9hhg/anonynns-small-stick


Okay. I think I might get it. The game has to check for two different item types and amounts so that the player can proceed to construct what they want to construct. The two items are: large_sticks and small_sticks

Would this work?

small_stick_count = NewObjectList()
large_stick_count = NewObjectList()
foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick;large_stick")) {
    list add (small_stick_count, o)
    list add (large_stick_count, o)
  }
}
small_stick_count = ListCount(stick_count)
if (small_sticks > 3) {
   if (large_sticks > 3) {
   msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
 }
  else {
  msg ("Unfortunately, you don't have enough large sticks for this project.") 
 }
else {
  msg ("Not enough small sticks.")
}

And thank you so much for all your help so far guys, I appreciate this.


almost...

(there's no reason to use Lists, unless you need to for whatever reason, such as get/move/remove/use them for something)
(and if you do need to use Lists, then you'd want to use List Attributes, so you can preserve and use them elsewhere, and not as List Variables, which are destroyed upon the scripting completing)

// for tallying the counts of each item type (these are local Variable VARIABLES, they're destroyed upon the completion of the scripting, if you need to preserve their amounts, then you need to store their values into Attribute VARIABLES, or just use Attribute VARIABLES to begin within instead of these Variable VARIABLES):

small_stick_count = 0
large_stick_count = 0

// iterate/search through your inventory (and any sub containers in your inventory), looking for any Object that has either the "small_stick" or "large_stiack" in the front of its name (the clones and original of these will indeed have this at the front of their names), and when found, add one to that item types' local count Variable:

foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick")) {
    small_stick_count = small_stick_count + 1
  } else if (StartsWith(o.name, "large_stick")) {
    large_stick_count = large_stick_count + 1
  }
}

// checking if you have enough of each item type to do your fire/shelter/etc scripting or not:

if (small_stick_count > 3) {
  if (large_stick_count > 3) {
    msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    // scripting for your construction of the fire/shelter/trap
    // if you want something somewhere else to do the needed scripting, then you need to store the Values into Attributes:
    // game.small_stick_count = small_stick_count
    // game.large_stick_count = large_stick_count
  } else {
    msg ("Unfortunately, you don't have enough large sticks for this project.") 
  }
} else {
  msg ("Not enough small sticks.")
}

@HK

That's pretty slick!

(Apparently I've been list-happy ever since I learned how to use them a little! I just went back through my recent scripts, and there are quite a few unnecessary lists I can remove. Awesome!)


@Anonynn

HK's script is spot on!

The difference between his and yours:

foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick;large_stick")) {
    list add (small_stick_count, o)
    list add (large_stick_count, o)
  }
}

This would only find strings beginning with small_stick;large_stick, rather than searching for either.

Even if it did run the script when finding either size stick, it would add an object onto both lists each time it found either the small stick OR the large stick.


Let's switch it over to HK's way (removing my unnecessary lists and plugging in the codes to increase the counts by 1) and examine it:

foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick;large_stick")) {
   small_stick_count = small_stick_count + 1
   large_stick_count = large_stick_count + 1
  }
}

Now, picture that written a little differently, but performing the same task:

foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick")) {
    if (StartsWith(o.name, "large_stick")) {
       small_stick_count = small_stick_count + 1
       large_stick_count = large_stick_count + 1
    }
  }
}

This would increase the count of both sticks any time either stick was found.


HK separated them, so each size stick's count is increased with a separate IF... statement:

foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick")) {
    small_stick_count = small_stick_count + 1
  } 
  else if (StartsWith(o.name, "large_stick")) {
    large_stick_count = large_stick_count + 1
  }
}

So, here's HK's solution again (minus the notes; formatted in the style you'll see in Quest's Code View):

small_stick_count = 0
large_stick_count = 0
foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick")) {
    small_stick_count = small_stick_count + 1
  }
  else if (StartsWith(o.name, "large_stick")) {
    large_stick_count = large_stick_count + 1
  }
}
if (small_stick_count > 3) {
  if (large_stick_count > 3) {
    msg ("SUCCESS!!!")
    msg ("")
    msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
  }
  else {
    msg ("Unfortunately, you don't have enough large sticks for this project.")
  }
}
else {
  msg ("Not enough small sticks.")
}

... if you do need to use Lists, then you'd want to use List Attributes, so you can preserve and use them elsewhere, and not as List Variables, which are destroyed upon the scripting completing.

List (deleted once script completes) :
stick_list = NewObjectList()

List as an Attribute (this is not deleted, and can be referenced throughout game-play):
game.stick_list = NewObjectList() or player.stick_list = NewObjectList()

(I don't know if it would be better to set the attribute to the game or to the player... Would it matter either way, HK?)


@HK

You're the man!!!

I CAN TOO POST THIS HERE!!! JUST WATCH!!! This is invisible.


Hi. I played around a bit with this.

I made a small game and a command for this.

This is what the start script looks like:

player.small_stick_count = NewObjectList()
player.large_stick_count = NewObjectList()
player.small_stick_count = 0
player.large_stick_count = 0

And this is what the code for the command looks like:
foreach (o, GetAllChildObjects(player)) {
if (StartsWith(o.name, "small_stick")) {
player.small_stick_count = player.small_stick_count + 1
}
else if (StartsWith(o.name, "large_stick")) {
large_stick_count = player.large_stick_count + 1
}
}
if (player.small_stick_count > 3) {
if (player.large_stick_count > 3) {
msg ("SUCCESS!!!")
msg ("")
msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
}
else {
msg ("Unfortunately, you don't have enough large sticks for this project.")
}
}
else {
msg ("Not enough small sticks.")
}

I named the command Stick count and it has a pattern of "stick #object#".

Then I ended up making a "small_stick". (1) It comes up with the message "Not enough small sticks."

I got to say, I'm not used to code like this! The code seems to work fine, though.


@jmnevil54

I had the list in there because I was printing out testing messages throughout the script, one of which printed each stick's name and it's parent object.


This is what the start script looks like:

player.small_stick_count = NewObjectList()
player.large_stick_count = NewObjectList()
player.small_stick_count = 0
player.large_stick_count = 0


The following two lines set up these two attributes/variables as object lists. (I was using this method initially, but it's no longer being used anywhere.)

player.small_stick_count = NewObjectList()
player.large_stick_count = NewObjectList()

Then this next bit turns those same attributes/variables into integers. (It does this last in your script, and integers is what you want, so it still works.)

player.small_stick_count = 0
player.large_stick_count = 0

So, to ease unnecessary load on Quest, change your start script to this:

Start Script:

player.small_stick_count = 0
player.large_stick_count = 0

small_stick_count = 0
large_stick_count = 0
foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick")) {
    small_stick_count = small_stick_count + 1
  }
  else if (StartsWith(o.name, "large_stick")) {
    large_stick_count = large_stick_count + 1
  }
}
if (small_stick_count > 3) {
  if (large_stick_count > 3) {
    msg ("SUCCESS!!!")
    msg ("")
    msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
  }
  else {
    msg ("Unfortunately, you don't have enough large sticks for this project.")
  }
}
else {
  msg ("Not enough small sticks.")
}

I tried this but no message is showing up. I'm wondering if it's because it isn't accounting for the sticks that the player might already have in their inventory. Either that or it's not recognizing the items inside of the containers within the player's inventory. OR which is more likely the case --- it might not be recognizing that all the items it is looking for are clones of the originals. Maybe finding the object alias' would be better?


Hi,

What's the name of the small stick in the game?

Is it Small Stick, small_stick or small_sticks? (I just noticed that we have it written all three ways in different places.)


Easy... (non object method)
.>get stick
player.stick=player.stick+1
In the UI, do not have "add stick to inventory"
Hide, or delete the stick.
msg("You now have " + player.stick + " sticks.")
You could add a check to see if there is a large stick or a small stick.


Hello again!


You can test this out online.

It's the same stickcount script you posted last, but I included an extra testing command to check and see where all the sticks are.

NOTE: All 4 commands are displayed as links when you 'LOOK'.

http://play2.textadventures.co.uk/Play.aspx?id=93e62fqi80kssodjqo9hhg

Code View
<!--Saved by Quest 5.7.6404.15496-->
<asl version="550">
  <include ref="English.aslx" />
  <include ref="Core.aslx" />
  <game name="Anonynn's sticks">
    <gameid>3b90cdd4-5cfd-4f44-976b-ab5609765cc7</gameid>
    <version>0.1</version>
    <firstpublished>2017</firstpublished>
    <start type="script">
    </start>
    <roomenter type="script">
    </roomenter>
  </game>
  <object name="room">
    <inherit name="editor_room" />
    <description><![CDATA[{command:smallstickme:smallstickme}<br/>{command:largestickme:largestickme}<br/>{command:stickcount:stickcount}<br/>{command:childinventory:childinventory}<br/>]]></description>
    <object name="player">
      <inherit name="editor_object" />
      <inherit name="editor_player" />
      <inherit name="namedmale" />
      <object name="pouch">
        <inherit name="editor_object" />
        <object name="bag">
          <inherit name="editor_object" />
          <object name="box">
            <inherit name="editor_object" />
          </object>
        </object>
      </object>
    </object>
  </object>
  <command>
    <pattern>smallstickme</pattern>
    <script>
      CloneObjectAndMove (small_stick, player)
      CloneObjectAndMove (small_stick, pouch)
      CloneObjectAndMove (small_stick, box)
      CloneObjectAndMove (small_stick, bag)
    </script>
  </command>
  <command>
    <pattern>stickcount</pattern>
    <script><![CDATA[
      small_stick_count = 0
      large_stick_count = 0
      foreach (o, GetAllChildObjects(player)) {
        if (StartsWith(o.name, "small_stick")) {
          small_stick_count = small_stick_count + 1
        }
        else if (StartsWith(o.name, "large_stick")) {
          large_stick_count = large_stick_count + 1
        }
      }
      if (small_stick_count > 3) {
        if (large_stick_count > 3) {
          msg ("SUCCESS!!!")
          msg ("")
          msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
        }
        else {
          msg ("Unfortunately, you don't have enough large sticks for this project.")
        }
      }
      else {
        msg ("Not enough small_sticks.")
      }
    ]]></script>
  </command>
  <command>
    <pattern>childinventory</pattern>
    <script>
      foreach (o, GetAllChildObjects(player)) {
        msg (o.name + ": is in " + o.parent.name)
      }
    </script>
  </command>
  <object name="small_stick">
    <inherit name="editor_object" />
  </object>
  <command>
    <script>
      CloneObjectAndMove (large_stick, player)
      CloneObjectAndMove (large_stick, pouch)
      CloneObjectAndMove (large_stick, box)
      CloneObjectAndMove (large_stick, bag)
    </script>
    <pattern>largestickme</pattern>
  </command>
  <object name="large_stick">
    <inherit name="editor_object" />
    <take />
    <ontake type="script">
      CloneObjectAndMove (large_stick, room)
    </ontake>
  </object>
</asl>

I clone a object then check to see if im carrying it in inventory, if not I add stick to inventory, remove [this](for cloned object) and +1 sticks.volume . Volume is a built in feature that works with the inventory as well so meaning you can carry so many types of objects and a certain volume in your inventory. Then i check if sticks.volume >= 3 then {Make Club} and so on, then subtract 3 from volume and check if volume is = 0 move stick to a storage room. I make those into functions so it can be called when ever, this is from my survival game i have been working on and this is just what works well for me so far but i also only do gui editor coding so theirs always better ways.

This is on the "Take" Object on inventory tab

if (Got(sticks)) {
  IncreaseObjectCounter (sticks, "volume")
  RemoveObject (this)
}
else if (not Got(sticks)) {
    RemoveObject (this)
    AddToInventory (sticks)
  }
    otherwise {
      RemoveObject (this)
      AddToInventory (sticks)
      IncreaseObjectCounter (sticks, "volume")
    }
  }
}

Now on the on "Drop"

if (Got(sticks)) {
  if (GetInt(sticks, "volume") >= 2) {
    DecreaseObjectCounter (sticks, "volume")
    CloneObjectAndMove (sticks, player.parent)
  }
  else if (GetInt(sticks, "volume") = 1) {
    DecreaseObjectCounter (sticks, "volume")
    MoveObject (sticks, StorageRoom)
    CloneObjectAndMove (sticks, player.parent)
  }
}

Then if I want to craft a small campfire here is the function for that

msg ("Type in the number you wish to craft. EX: 1, 3, or 5")
get input {
  s = ToInt(result)
  for (i, 1, s) {
    if (Got(sticks)) {
      if (GetInt(sticks, "volume") >= 6) {
        sticks.volume = sticks.volume - 6
        CloneObjectAndMove (campfire, player)
        CheckResourceValue
      }
      else {
        msg ("You need to be carrying more sticks before making a small campfire.")
      }
    }
    else {
      msg ("You need to be carrying sticks before making a small campfire.")
    }
  }
}

I start by asking for a number and make it a variable, then run a loop foreach one I can make while checking how many resources I have each time.

The "CheckResourceValue" is a function that does exactly that

if (GetInt(sticks, "volume") <= 0) {
  MoveObject (sticks, StorageRoom)
  sticks.volume = 0  <-----------//here im resetting the volume to ensure it stays correct//
}

And in that function above you can add all your resources as I did mine just didn't want a even bigger post but that is how I do mine hope it helps.


@ onimike

That is brilliant!


@ Anonynn:

what's the error message you get during game play? Or, are you NOT able to open into the GUI/Editor and/or play your game? What's the error message that given which prevent you from opening into the GUI/Editor and/or when trying to play your game?


the most likely cause is that the naming/labeling isn't being matched up, make sure that you're using the same names as you're using in your game for your Objects and their clones, and for their Attributes.

or, if you're doing the this coding directly in code, then it's common to forget to add the 'cdata' tags for your use of the 'greater than/lesser than/greater than or equal to/lesser than or equal to' symbols in your scripting.


this:

if (StartsWith(o.name, "small_stick;large_stick")) {}

is likely a syntax error. I don't think you can do this: "small_stick;large_stick", as it's looking literally for this in your name: small_stick;large_stick, or it's a flat-out syntax error: it doesn't work at all.

you can't combine them, they have to be separate:

if (StartsWith (o.name, "small_stick")) {} // this looks/checks for an Object that starts with "small_stick" in its name, which would be the original 'small_stick' Object and its clones: 'small_stick1', 'small_stick2', 'small_stick3', etc etc etc

else if (StartsWith (o.name, "large_stick")) {} // this looks/checks for an Object that starts with "large_stick" in its name, which would be the original 'large_stick' Object and its clones: 'large_stick1', 'large_stick2', 'large_stick3', etc etc etc

so, what you're doing is this:

is it the 'small_stick' original Object or one of its clones: if yes, add to the 'small_stick_count'

if it's not, then: is it the 'large_stick' original Object or one of its clones: if yes, add to the 'large_stick_count'

if not, then do nothing: move onto the next Object in the inventory


so, also your logic of this (pretending that there wasn't a syntax error with it) is very wrong:

  if (StartsWith(o.name, "small_stick;large_stick")) {
    list add (small_stick_count, o) // or: small_stick_count = small_stick_count + 1
    list add (large_stick_count, o)  // or: large_stick_count = small_stick_count + 1
  }

if this would work as you mis-intended, this is what would happen:

'small_stick2' clone Object in inventory would add to BOTH 'small_stick_count' and 'large_stick_count', which you don't want

'large_stick2' clone Object in inventory would add to BOTH 'small_stick_count' and 'large_stick_count', which you don't want

so, when you're suppose to have this:

small_stick_count = 1
large_stick_count = 1

you have this:

small_stick_count = 2
large_stick_count = 2

even though you only had one 'small_stick' clone Object and one 'large_stick' clone Object in your inventory


What's the name of the small stick in the game?
Is it Small Stick, small_stick or small_sticks? (I just noticed that we have it written all three ways in different places.)

They are:
small_stick with an alias of Small Stick
large_stick with an alias of Large Stick

What's the error message you get during game play? Or, are you NOT able to open into the GUI/Editor and/or play your game? What's the error message that given which prevent you from opening into the GUI/Editor and/or when trying to play your game?

The game is running perfectly fine and no error messages are popping up when trying to use the previous script. It just wasn't showing anything, which I'm sure is because we're looking for the original object instead of the object's clone's alias.

I do like the idea and simplicity of this...

small_stick_count = 0
large_stick_count = 0
foreach (o, GetAllChildObjects(player)) {
  if (StartsWith(o.name, "small_stick")) {
    small_stick_count = small_stick_count + 1
  }
  else if (StartsWith(o.name, "large_stick")) {
    large_stick_count = large_stick_count + 1
  }
}
if (small_stick_count > 3) {
  if (large_stick_count > 3) {
    msg ("SUCCESS!!!")
    msg ("")
    msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
  }
  else {
    msg ("Unfortunately, you don't have enough large sticks for this project.")
  }
}
else {
  msg ("Not enough small sticks.")
}

I just need it to find obj.alias (because of clones) rather than the original object itself in both the small_stick and the large_stick. Is there a way to alter the code above to look for the object alias'?

((Sorry about my late response too I went camping last night for one night)).

Anonynn.


the 'StartsWith' Function/code handles the clones' names, so this will work for you. You don't need to use the 'alias' Attribute if you're using the 'StartsWith' Function/code.


if you do want to use the 'alias' instead:

(there can be other/more issues/problems with using the 'alias' instead, whereas the 'StartsWith' will always work)

small_stick_count = 0
large_stick_count = 0
foreach (o, GetAllChildObjects(player)) {
  if (o.alias = "Small Stick") { // or: if (GetString (o, "alias") = "Small Stick") {
    small_stick_count = small_stick_count + 1
  }
  else if (o.alias = "Large Stick") { // or: else if (GetString (o, "alias") = "Large Stick") {
    large_stick_count = large_stick_count + 1
  }
}
if (small_stick_count > 3) {
  if (large_stick_count > 3) {
    msg ("SUCCESS!!!")
    msg ("")
    msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
  }
  else {
    msg ("Unfortunately, you don't have enough large sticks for this project.")
  }
}
else {
  msg ("Not enough small sticks.")
}

to only get the clones (and NOT the original Object), you need some additional coding:

(all clones have a longer name than the original Object, as contigious numbers are added to each created-clone's name)

<![CDATA[
  small_stick_count = 0
  large_stick_count = 0
  foreach (o, GetAllChildObjects(player)) {
    if (StartsWith(o.name, "small_stick") and LengthOf (o.name) > LengthOf ("small_stick")) {
      small_stick_count = small_stick_count + 1
    }
    else if (StartsWith(o.name, "large_stick") and LengthOf (o.name) > LengthOf ("small_stick")) {
      large_stick_count = large_stick_count + 1
    }
  }
  if (small_stick_count > 3) {
    if (large_stick_count > 3) {
      msg ("SUCCESS!!!")
      msg ("")
      msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    }
    else {
      msg ("Unfortunately, you don't have enough large sticks for this project.")
    }
  }
  else {
    msg ("Not enough small sticks.")
  }
]]>

if you want to use the 'alias', here:

<![CDATA[
  small_stick_count = 0
  large_stick_count = 0
  foreach (o, GetAllChildObjects(player)) {
    if (o.alias = "Small Stick"  and LengthOf (o.name) > LengthOf ("small_stick")) { // or: if (GetString (o, "alias") = "Small Stick"  and LengthOf (o.name) > LengthOf ("small_stick")) {
      small_stick_count = small_stick_count + 1
    }
    else if (o.alias = "Large Stick"  and LengthOf (o.name) > LengthOf ("large_stick")) { // or: else if (GetString (o, "alias") = "Large Stick" and LengthOf (o.name) > LengthOf ("large_stick")) {
      large_stick_count = large_stick_count + 1
    }
  }
  if (small_stick_count > 3) {
    if (large_stick_count > 3) {
      msg ("SUCCESS!!!")
      msg ("")
      msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    }
    else {
      msg ("Unfortunately, you don't have enough large sticks for this project.")
    }
  }
  else {
    msg ("Not enough small sticks.")
  }
]]>

Is the player technically carrying the parent object of small_stick and large_stick?

  • player
    • container
      • small_stick
      • large_stick

or

  • player
    • container
      • container
        • container
          • small_stick
          • large_stick

E.g., If the sticks are in a wheelbarrow, and the player is not wheelbarrow.parent (or if the player is not the parent object of whatever contains the sticks), this wouldn't work.


The 'GetAllChildObjects' is suppose to work (it gets ALL Objects within the inputted Object, regardless of their nested layer).


The 'GetAllChildObjects' is suppose to work (it gets ALL Objects within the inputted Object, regardless of their nested layer).

Yes, sir. It definitely works, I'm using the very same code Anonynn posted last in my example game, and it works.

...but, in our minds, whatever the sticks are in eventually leads up to the player in the parent chain, so we have this: foreach (o, GetAllChildObjects(player)) {

BUT the sticks may not be child objects of anything concerning the player.

Like if there were a wheelbarrow the player moved from room to room, or if there were an elaborate inventory system, which I believe Anonynn may be using.

It was just one of those thoughts that hit me out of nowhere...

@Anonynn

Is there a special take script for the sticks by any chance?


Is there a special take script for the sticks by any chance?

Yup, Pixie and I have completely converted the game's objects (not rooms) to finding alias'. Here is the take (listed below). And yes, the sticks go into the players inventory in a submenu, still nested on the player.

if (this.alias = null) {
  this.alias = this.name
}
container = this.stackparent
this.parent = container
SetStack (container)
if (this.takemsg = null) {
  msg (DynamicTemplate("TakeSuccessful", object))
}
else {
  msg (this.takemsg)
}

So I tried this (the code below) and nothing showed up again when I clicked the option that had the code in it. No messages and no errors.

  small_stick_count = 0
  large_stick_count = 0
  foreach (o, GetAllChildObjects(player)) {
    if (o.alias = "Small Stick"  and LengthOf (o.name) > LengthOf ("small_stick")) { // or: if (GetString (o, "alias") = "Small Stick"  and LengthOf (o.name) > LengthOf ("small_stick")) {
      small_stick_count = small_stick_count + 1
    }
    else if (o.alias = "Large Stick"  and LengthOf (o.name) > LengthOf ("large_stick")) { // or: else if (GetString (o, "alias") = "Large Stick" and LengthOf (o.name) > LengthOf ("large_stick")) {
      large_stick_count = large_stick_count + 1
    }
  }
  if (small_stick_count > 3) {
    if (large_stick_count > 3) {
      msg ("SUCCESS!!!")
      msg ("")
      msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    }
    else {
      msg ("Unfortunately, you don't have enough large sticks for this project.")
    }
  }
  else {
    msg ("Not enough small sticks.")
  }

BUT this code (the one listed below) showed the "Not enough small sticks" --- but when I tested it again with the correct amount and absolutely no sticks of either kind, it didn't print any message or errors. So this one might just be tracking one small_stick but not any of the others.

  small_stick_count = 0
  large_stick_count = 0
  foreach (o, GetAllChildObjects(player)) {
    if (StartsWith(o.name, "small_stick") and LengthOf (o.name) > LengthOf ("small_stick")) {
      small_stick_count = small_stick_count + 1
    }
    else if (StartsWith(o.name, "large_stick") and LengthOf (o.name) > LengthOf ("small_stick")) {
      large_stick_count = large_stick_count + 1
    }
  }
  if (small_stick_count > 3) {
    if (large_stick_count > 3) {
      msg ("SUCCESS!!!")
      msg ("")
      msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    }
    else {
      msg ("Unfortunately, you don't have enough large sticks for this project.")
    }
  }
  else {
    msg ("Not enough small sticks.")
  }

if you're using a different Object as the (root) storage Object (instead of the Player Object), then you'd just input that into the 'GetAllchildObjects' Function:

  small_stick_count = 0
  large_stick_count = 0
  foreach (o, GetAllChildObjects(NAME_OF_ROOT_STORAGE_OBJECT)) {
    if (StartsWith(o.name, "small_stick") and LengthOf (o.name) > LengthOf ("small_stick")) {
      small_stick_count = small_stick_count + 1
    }
    else if (StartsWith(o.name, "large_stick") and LengthOf (o.name) > LengthOf ("small_stick")) {
      large_stick_count = large_stick_count + 1
    }
  }
  if (small_stick_count > 3) {
    if (large_stick_count > 3) {
      msg ("SUCCESS!!!")
      msg ("")
      msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    }
    else {
      msg ("Unfortunately, you don't have enough large sticks for this project.")
    }
  }
  else {
    msg ("Not enough small sticks.")
  }

for example:

<object name="storage_object">
  <object name="item_storage_object">
    <object name="consumable_item_storage_object">
      <object name="life_potion_object">
      </object>
    </object>
    <object name="battle_item_storage_object">
      <object name="motolov_coctail_object">
      </object>
    </object>
  </object>
  <object name="equipment_storage_object">
    <object name="weapon_storage_object">
      <object name="sword_storage_object">
        <object name="katana_sword_object">
          <attr name="type_of_object_string_attribute" type="string">sword</attr>
        </object>
        <object name="claymore_sword_object">
          <attr name="type_of_object_string_attribute" type="string">sword</attr>
        </object>
      </object>
      <object name="axe_storage_object">
        <object name="tomahawk_axe_object">
          <attr name="type_of_object_string_attribute" type="string">axe</attr>
        </object>
        <object name="hatchet_axe_object">
          <attr name="type_of_object_string_attribute" type="string">axe</attr>
        </object>
      </object>
    </object>
    <object name="armor_storage_object">
      <object name="torso_storage_object">
        <object name="chain_mail_Object">
        </object>
      </object>
      <object name="hand_storage_object">
        <object name="gauntlet_Object">
        </object>
      </object>
    </object>
    <object name="clothing_storage_object">
      <object name="torso_storage_object">
        <object name="shirt_Object">
        </object>
      </object>
      <object name="hand_storage_object">
        <object name="glove_Object">
        </object>
      </object>
    </object>
  </object>
  <object name="spell_storage_object">
    <object name="fire_storage_object">
      <object name="fireball_object">
      </object>
    </object>
    <object name="earth_storage_object">
      <object name="earthquake_object">
      </object>
    </object>
  </object>
</object>

// just an example of/for seeing the Function Call/Use:

<game name="example_game">
  <attr name="start" type="script">
    example_function (storage_object)
  </attr>
</game>

// adjusted for my Objects above:

<function name="example_function" parameters="root_object_parameter">
  <![CDATA[
    sword_count_integer_variable = 0
    axe_count_integer_variable = 0
    foreach (object_variable, GetAllChildObjects (root_object_parameter)) {
      if (GetString (object_variable, "type_of_object_string_attribute") = "sword") {
        sword_count_integer_variable = sword_count_integer_variable + 1
      } else if (GetString (object_variable, "type_of_object_string_attribute") = "axe") {
        axe_count_integer_variable = axe_count_integer_variable + 1
      }
    }
    if (sword_count_integer_variable > 3) {
      if (axe_count_integer_variable > 3) {
        // blah scripting
      } else {
        msg ("Unfortunately, you don't have enough axes for this project.")
      }
    } else {
      msg ("Not enough swords.")
    }
  ]]>
</function>

if you don't have a (single) root storage Object, you should (as it simplifies and make the coding more efficient: less operations as it doesn't have to search and handle multiple root Objects). Have all of your individual storage Objects be nested or via 'parent'-ed into a single (root) storage Object.


if this is correct:

"BUT this code (the one listed below) showed the "Not enough small sticks" --- but when I tested it again with the correct amount and absolutely no sticks of either kind, it didn't print any message or errors. So this one might just be tracking one small_stick but not any of the others. (Anonynn)"

then make sure that everything in code, matches up with this (or whatever you have it has --- check to make sure that indeed your original Object's name is 'large_stick' and its 'alias' is "Large Stick"):

(especially check for accidental more/less spaces than intended... as these are really nasty typos/coping-pasting mistakes...)

large_stick.alias = "Large Stick"

<object name="large_stick">
  <attr name="alias" type="string">Large Stick</attr>
</object>

(filler for getting this edited post, updated/posted)


wait... what do you mean by this:

"BUT this code (the one listed below) showed the "Not enough small sticks" --- but when I tested it again with the correct amount and absolutely no sticks of either kind, it didn't print any message or errors. So this one might just be tracking one small_stick but not any of the others. (Anonynn)"

???


if your game code wasn't so massive... it'd be very easy for you to just post/pm your entire game code, so we can see what you're doing... well... I guess you could do that... it'd just take us more time to scour through your massive game code, seeing what you did and fixing it up so it works and explaining it to you (why it didn't work and why it now works with the/our fixes to it).


actually... you should just post or pm us your entire game code, as that'd probably actually be faster than indirectly via: you and us trying to communicate via these postingss we're doing now and figure out what's the issues with your code, as seeing your entire game code, we can directly scour through it and fix up the mistakes in it for you, it'd just take a bit of time based on how large your game code is, lol

though, we'd need to see Pixie's library code too... as we need to know the Objects/Attributes/Functions/ETC that his library is using, for us to make sure your code and/or our fixes to it, match up and thus work with Pixie's library code, which you're using for your game


if you're using a different Object as the (root) storage Object (instead of the Player Object), then you'd just input that into the 'GetAllchildObjects' Function

I have the Player Inventory, then within the inventory once a specific object gets taken it creates a specialized sub-container within the Player's Inventory called "Survival Inv". All of the survival items like the small_stick and the large_stick are stored there. I figured Get ScopeInventory(GetAllChildObjects) would work, but I guess not, huh?

BUT this code (the one listed below) showed the "Not enough small sticks" --- but when I tested it again with the correct amount and absolutely no sticks of either kind, it didn't print any message or errors. So this one might just be tracking one small_stick but not any of the others. (Anonynn)

What I meant by that was, I tried that code with one small_stick in my inventory and it showed the "not enough small_sticks" message. Afterward, I tried it with the correct amount of sticks needed (over 3) and nothing happened when I tried to run the script. Then I tried the code with absolutely 0 small_sticks in the inventory and nothing happened when I tried to run the script.

actually... you should just post or pm us your entire game code, as that'd probably actually be faster than indirectly via: you and us trying to communicate via these postingss we're doing now and figure out what's the issues with your code, as seeing your entire game code, we can directly scour through it and fix up the mistakes in it for you, it'd just take a bit of time based on how large your game code is, lol

I can't PM the entire code. There is a limit to how much you can post in PMs, unfortunately. I tried posting the details of the Command (not the code but the notes for it) in a PM and couldn't even fit all of that. But yeah, my game is like 42-45,000 loc now (I haven't checked in a couple of weeks).


Last time, I just e-mailed the entire game file to The Pixie.
Other alternatives include putting the code on another website like deviantart or something.
But yeah, just PM the Pix and he should give you his e-mail or at least offer to help.


Last time, I just e-mailed the entire game file to The Pixie.
Other alternatives include putting the code on another website like deviantart or something.
But yeah, just PM the Pix and he should give you his e-mail or at least offer to help.

Pixie's actually on vacation at the moment which is good cause he's been busting his/her butt! Also, there's no way in hell I would post my game's code on a public website xD But I do trust a couple of individuals on here to take a look at it. HK for example, Pertex, Pixie, K.V, onimike, Jay, Xan to name a few.


ya, forgot about the pm character limit, indeed, have to use some other means...

and ya, you'd want to pm those you trust, as you don't want to make your game code public (via posting it)... I was just making a generalized comment of 'pm or post', but for you specifically, it'd jsut be 'pm', lol. --- But, I forgot the pm character limit, so, no pm ether, lol.

I don't want to give out my email... there's other sites that you can post onto it, and it uses a random unique url, so that works pretty well for keeping it private, as you'd just pm that url to the person who'll look at your code, and thus no one else would know the url and non-censentually see your game code. But that's still kind of a hassle.

Personally, if you can wait on getting this fixed up for your game, I'd just wait until Pixie comes back and get pixie's help and email Pixie your game code, as he's already been helping you, and thus is familiar a bit with your game code already, as well as obviously being familiar with his own library that you're using for your game.


ah, okay, so your sub-root storage Object in within your (root storage of the) Player Object. The 'GetAllChildObjects' and the rest of the code, aka the code, should work...


"What I meant by that was, I tried that code with one small_stick in my inventory and it showed the "not enough small_sticks" message. Afterward, I tried it with the correct amount of sticks needed (over 3) and nothing happened when I tried to run the script. Then I tried the code with absolutely 0 small_sticks in the inventory and nothing happened when I tried to run the script. (Anonynn)"

  if (small_stick_count > 3) {
    if (large_stick_count > 3) {
      msg ("SUCCESS!!!")
      msg ("")
      msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    }
    else {
      msg ("Unfortunately, you don't have enough large sticks for this project.")
    }
  }
  else {
    msg ("Not enough small sticks.")
  }

this (the above code) should be giving you a message no matter what:

if small sticks less than 4: msg ("Not enough small sticks.")

if small sticks greater than 3, but large sticks less than 4: msg ("Unfortunately, you don't have enough large sticks for this project.")

if small sticks greater than 3 and large sticks greater than 3:
msg ("SUCCESS!!!")
msg ("")
msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")


maybe you got the nesting/indenting of the 'if' blocks wrong... make sure you got it correct... (post this part of your code for us, and we'll check it for you)


you could also just try this code and let us know if it works or not:

(as this will narrow down the issue/problem/error, you're getting, to the handling of the Objects involved, which is the part of code removed from the below code for you to try)

  small_stick_count = 0 // test it by changing its value
  large_stick_count = 0 // test it by changing its value
  if (small_stick_count > 3) {
    if (large_stick_count > 3) {
      msg ("SUCCESS!!!")
      msg ("")
      msg ("You have enough to construct this fire/shelter/trap! And proceed to do so.")
    }
    else {
      msg ("Unfortunately, you don't have enough large sticks for this project.")
    }
  }
  else {
    msg ("Not enough small sticks.")
  }

This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums