Post new topic    
Page «  1, 2, 3  »
Liquid Metal Slime
Send private message
 
 PostSat Nov 22, 2014 4:03 am
Send private message Reply with quote
No problem. I didn't become aware of scancode.hsi until I asked about doing something with keyboard input too. This idea of yours may actually turn out to help me later in my game. I planned on having a cauldron that the player can mix ingredients in to make potions! Please keep this thread updated on your progress. I'd like to see the final working script once it's done if you don't mind...
⊕ P E R S O N A L M U S I C: https://open.spotify.com/album/6fEo3fCm5C3XhtFRflfANr
⍠ C O L L A B M U S I C: https://dustpuppets.bandcamp.com/releases
Slime Knight
Send private message
 
 PostSat Nov 22, 2014 4:46 am
Send private message Reply with quote
I'll definitely try to update this thread with any information on progress, or snags/setbacks... honestly it will most likely be a lot of the latter. If this turns out to be something that works, I'll gladly share the code for everyone to use, tweak, improve, etc.

Likewise, I'd love to hear any suggestions, tips, or ideas from anyone interested in giving them. I really don't quite know what I'm doing, but I love solving problems! I already owe all the progress I've made to the helpful men and women of the Slime Salad forums V

Also, that potion cauldron idea sounds pretty rad!
Metal King Slime
Send private message
 
 PostSat Nov 22, 2014 5:50 am
Send private message Reply with quote
Remove the waitforscancode lines. That just pauses the script until that key is pressed; not what you want. You could move all the "if(key is pressed(..))" stuff into an on-keypress script, or alternatively use a while loop and continually check the keyboard input. I suggest the later.
"variable (npid:1) ... create npc (npid:1, ...)" isn't right. Just like the explanation Giz gave, this creates a variable with initial value 0, and creates a copy of NPC ID 0. Write "create npc (1,...)" instead.

To create multiple NPCs, just call 'create npc' multiple times.

You script might look like:

Code:

# Press enter to complete the recipe
while (key is pressed(key:enter) == false) do (
    if(key is pressed(key:1)) then(create npc (0, hero x, hero y))
    if(key is pressed(key:2)) then(create npc (1, hero x, hero y))
    if(key is pressed(key:3)) then(create npc (2, hero x, hero y))
    wait(1)   # finished all processing for this game tick (frame), so pause the script until the next tick
)
# Now check for a valid recipe using npcatspot


Note: rather than long strings of npcatspot calls, I recommend creating a new script which tests for a particular pattern, returning true or false. Something like (assuming the topleft of the grid is at x=4, y=4):

Code:

script, check recipe, x0y0, x1y0, x2y0, x0y1, x1y1, x2y1, x0y2, x1y2, x2y2, begin
  if (npc at spot(4, 4) == x0y0 &&
   npc at spot(5, 4) == x1y0 &&
   .....  # etc
  )
  then (return (true)) else (return (false))
end


Then you can use it like this (-1 indicates no item (npc) there):

Code:


# 3 copies of item 0 on a diagonal
if (check recipe(
   0, -1, -1,
   -1, 0, -1,
   -1, -1, 0
)) then (
   # success
) else if (check recipe(
   # next recipe....
) else (
   # bad recipe
)
Slime Knight
Send private message
 
 PostSat Nov 22, 2014 2:18 pm
Send private message Reply with quote
Excellent! Thank you very much, I knew my code had lots of unnecessary crud in it, and now I can see what to remove.

I was hoping to find the way to do this using if/thens and npc checks, so I'm glad you posted what you did Smile I imagine I could also add an option for removing a placed npc if there was an input mistake. If you place an npc in the wrong spot and want to get rid of it, could I implement a keypress code to check the tile for an existing npc, and if that returns true, use a destroy npc command?

I will also most likely try to add a script to the keypress to check items in the hero inventory. Something so that when you place an npc representing an Iron ore, the inventory will be checked to make sure there is an iron ore available, and if so, remove it from the inventory. In addition, to replace the iron ore, if the player inputs an incorrect recipe, or incorrectly places an iron or, and wants to "delete" it.

Again, thank you or the help! Your post was most enlightening. I appreciate you taking the time to help someone so unfamiliar with scripting to add an unnecessary system to their silly game.
Metal King Slime
Send private message
 
 PostSun Nov 23, 2014 12:34 am
Send private message Reply with quote
Opps, I noticed a bug in my "check recipe" script. "npc at spot" returns an npc reference, while you want to check the npc id number. And it returns 0, not -1, for no npc. We can fix that with another helper script:

Code:
script, npc id at spot, x, y, begin
  if (npc at spot(x, y) == 0) then (
    return (-1) # no NPC
  ) else (
    return (get npc id(npc at spot(x, y)))
  )
end

Then just modifiy "check recipe" to call "npc id at spot" instead of "npc at spot"

Deleting an npc is very easy; just write "if (npc at spot(hero x, hero y)) then (delete npc (npc at spot(hero x, hero y)))". Doing the if test is necessary because npcatspot returns 0 if there's no NPC there, which will be misinterpreted as wanting to delete the first copy of NPC ID 0, if any.

Also, "key is pressed" is going to continue to return true for every game tick (frame) that a key is down, so if you press a key for more than a fraction of a second it will place multiple npcs. I think the best way to fix that is to delete any npc already at the spot before placing the new one.
Slime Knight
Send private message
 
 PostSun Nov 23, 2014 1:29 am
Send private message Reply with quote
thanks for all the helpful info! As things stand right now, I've got it so that the hero transforms into an empty block, goes to the crafting table, maneuvers around the table ( at the moment, it is 4x4. I adjusted the code you posted to reflect that in addition to punching in my x,y coordinates.)

The empty block moves within the table, pressing 1,2, and three create NOCs representing Iron, Gold, and Diamond respectively. Hitting enter disables further NOC placements, but as of yet hitting enter does not seem to call the check recipe function. I also added a Keypress of "tab" to teleport back out of the crafting table



So there is definitely progress! I was also playing around with some additional codes to implement as far as item cost is concerned. I have to research how to make it so the player must have, for example, an Iron Ore in his inventory in order to place one on the crafting table, and doing so removes 1 iron ore from the player's inventory.

Below is a code I was working on to link to a keypress that would remove an NPC and replace to the inventory the item cost of the NPC.
Code:
plotscript, deletion, begin
if (npc at spot(hero x, hero y)) == 0
  then ((destroy npc (0) (get item (27))
      else if (npc at spot(hero x, hero y)) == 1
  then ((destroy npc (1) (get item (31))
     else if (npc at spot(hero x, hero y)) == 2
  then ((destroy npc (2) (get item (26)))
end)


So as I said, there have been some... events so far. I will cautiously call these events "progress"


*update* Hitting Enter seems to cause all other keypress functions to cease

Thanks again for all the code help!


*edit* just posting a potential code here for personal reference
Code:
end
plotscript, iron1, begin
if (inventory (27) == true) then (create npc (0, hero x, hero y)
delete item (27))
end


Update... again....
I have a code now that will remove one of each item used on the crafting table, ut I haven't been able to set it up so that when you run out of the item, you are no longer able to place that item... I'm too tired to do any more than that for tonight, Things are progressing...
Metal King Slime
Send private message
 
 PostSun Nov 23, 2014 7:22 am
Send private message Reply with quote
OK, lots of problems here...

Code:
if (npc at spot(hero x, hero y)) == 0
  then ((destroy npc (0) (get item (27))
      else if (npc at spot(hero x, hero y)) == 1

Aside from missing the bracket before the 'then', this checks whether there isn't an npc on the same tile as the hero. Use the 'npc id at spot' script I gave you instead. Also, the other brackets are randomly and incorrectly placed. You can use commas to seperate multiple commands on one line. So the above should be written:
Code:
if (npc at spot(hero x, hero y) == 0)
then (destroy npc (0), get item (27)
else if (npc at spot(hero x, hero y) == 1)


The "inventory" command doesn't return true/false, it returns the number of items in the inventory. So write "inventory (27) > 0" instead of "inventory (27) == true". Secondly, I strongly recommend referring to things by name instead of ID number. The .hsi file that can be exported from the Script Management menu has constants like "item:iron" which you can use instead of "27".
Slime Knight
Send private message
 
 PostMon Nov 24, 2014 12:56 am
Send private message Reply with quote
Thanks for the tips! I feel so dumb while I'm working on this stuff, I seem to have the most trouble with the most elementary of scripting issues; brackets, openings and closings... bleh

I wasn't sure about the inventory script, the dictionary led me to believe that it could be done either with a true/false, or a >, <. I hadn't tried to implement this code at the time I posted it, so I wasn't sure if it would work.

I used the code you posted and it seems to be working Smile I attempted to build upon it, and I am once again doing something stupid wrong. When I try to add more, I get an error while importing leading me to believe that I am not closing the script correctly. It seems like it is trying to import the opening of the net script in my hss file. So I'm trying to figure that out

Code:

plotscript, deletion, begin
if (npc id at spot(hero x, hero y) == 0)
then (destroy npc (0) ( get item (item:iron ore) )
    else if (npc id at spot(hero x, hero y) == 1 )
then (destroy npc (1) ( get item (item: gold ore) )
    else if (npc id at spot(hero x, hero y) == 2 )
then (destroy npc (2) ( get item (item: diamond) )
end )


Thanks again for all the help you've given me. I really do try to do this independently, and figure things out on my own before running to the Slime Salad forums with my ridiculous and inane questions.

I will continue to try to study the tutorials and scripting dictionary.
Metal Slime
Send private message
 
 PostMon Nov 24, 2014 1:22 am
Send private message Reply with quote
Yep. Something you need to watch out for with plotscripting is that "begin" and "end" tags are interpreted the same way as parentheses. When you get an error that says you might have an extra begin or end in the script, it could also mean that you have parentheses that aren't closed properly. Something like the extra parenthesis ")" you have at the end of your code would be interpreted as an extra "end".

I also recommend indenting your scripts every time you make a new begin block, to make it easier to remember how many "begins" and "ends" you have. Something like this might look more readable.

Code:
plotscript, deletion, begin
  if (npc id at spot(hero x, hero y) == 0)
  then (destroy npc (0) ( get item (item:iron ore) )
  else if (npc id at spot(hero x, hero y) == 1 )
  then (destroy npc (1) ( get item (item: gold ore) )
  else if (npc id at spot(hero x, hero y) == 2 )
  then (destroy npc (2) ( get item (item: diamond) )
end )



And, lastly. This:
Code:
destroy npc (0) ( get item (item:iron ore) )


I'm not sure this is something that would work. "destroynpc" and "getitem" are two separate commands. If you want to use two commands on the same line, you should separate them with a comma. Like this:
Code:
destroy npc (0), get item (item:iron ore)
Slime Knight
Send private message
 
 PostTue Nov 25, 2014 1:03 am
Send private message Reply with quote
Awesome! Thanks for the help. In addition to everything else, I was having a little trouble figuring out the workings of commas in scripts. This should help Smile

*edit*
Update: Major Breakthrough!

I made a Sword happen!
.....
Okay, so maybe it's not that major, but I was pretty pleased with it Grin

I now have it so that the player can place an item on the crafting table, and 1 of that item is removed from their inventory. Placing the items correctly and striking Enter brings up a text box congratulating them on crafting the item. I'll probably set the text box to use the door to the crafting table after it closes to give the impression of resetting everything.

It seems like right now when the player presses a key to place an item, it is only putting one down, which is great. I am however unsure. each keystroke is deleting 1 item, but I'm not sure if multiple NPCs are being created on the same spot. This may be related to another problem...

The deletion command I have so far is faulty. It takes several keystrokes, or holding down the key to delete the placed NPCs. In turn, though the item NPCs eventually delete, the player gains another of that item for each time the keystroke is read (does this equate to ticks?)

This is a code that is currently not quite doing what I'd like. I was trying to figure out how to make it so the player could not place an item not in their inventory, and attempting to do so would play a discouraging buzz sound. What I currently have doesn't stop the player from placing item NPCs when they no longer have any of that item in their inventory, and also plays the buzz every time the key is pressed, regardless of inventory.
[code]
plotscript, placeiron, begin
if (inventory (item:iron ore) >0) then (create npc (0, hero x, hero y) delete item (item:iron ore))
else if (inventory (item:iron ore) <1>0, and ==zero all with the same resullts if that makes any difference.
I'm also attempting to implement a check system, like TMC mentioned allowing only one of any item NPC to be placed on any single tile.
Metal King Slime
Send private message
 
 PostTue Nov 25, 2014 3:04 pm
Send private message Reply with quote
The way to avoid a keydown event from repeating every tick is to write "if(keyval(key:X) > 1)" instead of "if(keyispressed(key:X))" (I've been meaning to add a more obvious command for that for years). However the former still starts repeating every tick after a delay, so you still need to handle it anyway.

The problem with deleting NPCs that you're having is very probably that there are several copies on the same tile, and you don't notice anything until removing the last one.

Also, make sure that you wrote something like "delete npc(npc at spot(hero x, hero y))" instead of "delete npc(npc id at spot(hero x, hero y))"'; the later is wrong.

I can't read the code you posted because the forum software gets confused when it sees < and > characters is a script. Tick "Disable HTML in this post" to prevent that from happening.

I suggest creating a script "delete item here" which check whether there's already an npc on the hero's tile, and if so deletes it and returns the item to the inventory. Then you can just always call that script before placing any item. Then if the key gets held down it doesn't matter, because there will only ever be a single copy.
Slime Knight
Send private message
 
 PostTue Nov 25, 2014 11:42 pm
Send private message Reply with quote
Holy Cannoli , It's working!

I've amended my scripts, following the tips suggested, and implementing a couple other things I thought of, and after some tweaking my scripts (tweaking is code for; adding and removing parentheses, and commas, over and over, and over again, until I was able to figure out where the heck they actually belonged) I have tested ym crafting table system, and it seems to be working exactly as I initially hope it would!

I will do my best to clean up my hss file, add annotations, and post the code here pretty soon. I am also considering compiling the crafting element of this game into its own, independent "game" and including the script file for folks who want to try it out, customize it to their liking, and improve it. Though I suppose anyone can do that on their own as ong as they have the scripts.

I want to once again thank RMSephy, Sheamkennedy, Gizmog, and most of all TMC for all of your help, and for putting up with my relative ineptitude when it comes to coding, and plotscripting. I truly, and deeply appreciate all the assistance and patience I've been granted in this endeavor.

I will at this point attempt to get back to scripting elements that are not beyond my personal, shallow depth.

Thank you all once again Grin
Metal King Slime
Send private message
 
 PostWed Nov 26, 2014 2:23 am
Send private message Reply with quote
Urkelbot666 wrote:

I will at this point attempt to get back to scripting elements that are not beyond my personal, shallow depth.


Forget that noise, man! Stretch out, grow! Good for you!

Urkelbot666 wrote:

Thank you all once again Grin


You're quite welcome, always glad to help!
Metal King Slime
Send private message
 
 PostWed Nov 26, 2014 4:28 am
Send private message Reply with quote
Cool. You seemed like someone who's capable of working things out for yourself and improving, but just lacked experience with HamsterSpeak and programming. It'll be nice to have your scripts available to point other people to.

As for commas, there's a simple rule of thumb: you only need them to separate two things that otherwise would appear to be a single item when you remove all the whitespace. For example, if putting two statements on one line like "wait, fadescreenout" you need the comma because otherwise it gets interpreted as a command called "waitfadescreenout". And you don't need any commas in "if(...) then(...) else if(...) then(...)" because the "if" and "then" are separated from everything else by parentheses. "elseif" is actually a separate keyword, it's not an else next to an if, so no comma in between. Actually, the way commas work in HS is really weird. "destroy npc (0) ( get item (item:iron ore) )" is actually equivalent to "destroy npc (0), get item (item:iron ore)" because the redundant parentheses are silently thrown away. This causes lots of people to add extra brackets around things. Of course, you shouldn't do that; it's not easy to read.

I very strongly second Sephy's suggestion to use indentation to keep track of where and how many parentheses you need. That's how you avoid that sort of problem.

If you do have a misplaced parenthesis, you can use this tool to indent the script according that the begin/ends and parentheses that are present:
http://tmc.castleparadox.com/ohr/script-format.php
Misplaced or missing parentheses can then be spotted because they cause the indentation to look wrong. Note that this script is very old and can't handle all HS features, like strings.
Slime Knight
Send private message
 
 PostWed Nov 26, 2014 8:32 pm
Send private message Reply with quote
When I was testing the crafting system, I realized that I hadn't included any way to restart the crafting script following an incorrect input.

At the end of my check recipe script, I put a final else command to play a sound, and use a door returning the player to the crafting table, which would have been refreshed by the use of the door. No problem!

However, I noted that this would not return any of the placed items to the player's inventory, thus making the entry of an incorrect recipe a potentially major issue. If you wanted to craft diamond armor, but misplaced one item, you could be out 5 or 6 diamonds.

Simply adding an else command to run the crafting script upon incorrect entry gives me a scripting error.

I have been looking at the dictionary. I am attempting to find a way to check the NPC references on the crafting table, and add one of each item they represent to the players inventory before reloading the map. I realize I could do this with another check recipe style script, but that would require inputting a ton of incorrect recipes into the script.

So I am about to try to find a way to make a script that will call all the NPCs still on the table, return their IDs and number of copies, and add that number of appropriate items back to the player's inventory.

That is, unless I can find a way to restart the script after the enter key is pressed, which would allow a player to return to the crafting table, and manually delete items to try again.

Failing either of those attempts, I'll just add a big warning message telling players to save before beginning any crafting, or simply DON'T MESS UP! XD


**Update**

Ran into a bit of a snag while testing out ways to return placed items to player's inventory after an incorrect recipe. I worked up a script to get an npc count, and return that number of items to the inventory. When I would use it I kept getting really inflated numbers.

After some checking, I realized that in the bottom corner of the map used for the crafting table were copies of all the NPCs I had placed when I started this project thinking that instead of using a create NPC command, I would use a put npc command, and just keep all the available npcs offscreen.

Out of curiosity, I moved these surplus npcs to a spot on the map that I could see them while testing the game. When I place an Item (this is scripted with the create NPC command) that corresponding npc is placed on the table like i wanted, however, I can now see that when i place it, one of the surplus npcs disappears... seemingly as if I were using a command to move an npc, instead of create a brand new one.

The destroy NPC command I am using still permanently destroys the NPC, which is what I wanted. Now, that means that the map could conceivably run out of NPCs if the player was really goofing off placing and deleting items, which is unlikely.

So... this isn't a huge setback, as I don't mind having a surplus of off screen NPCs to call in use of the crafting table. However, this means I probably can't get a simple NPC count call to return items to the player's inventory after an incorrect recipe.

I just find it strange... below is the item placing code I am using. It does use Create NPC commands, not Put NPC commands... Like I mentioned, not a total dealbreaker, just... curious..


Code:
plotscript, placeiron, begin
      if (inventory (item:iron ore) >0) then  (destroy npc (npc at spot

(hero x, hero y)), create npc (0, hero  x, hero y),    delete item

(item:iron ore) )
       else (play sound (55,false,false))
Display posts from previous:
Page «  1, 2, 3  »