Topic:   Help with code   (Read 18487 times)


0 Members and 3 Guests are viewing this topic.

Xiphos


  • GMG-er

  • **


  • Posts: 676
Help with code
« on: December 03, 2009, 07:34:15 PM »
Quote
FOR x = 1 to 16
IF invspace+str$(x) = "" THEN
LET invspace+str$(x) = "fathersword"
LET x = 16
END IF
NEXT

I need to make x become part of the code so it's like saying if x was 1 then
Quote
IF invspace1 = "" THEN
LET invspace1 = "fathersword"
LET x = 16
END IF
The let x = 16 ending the code.
What am I doing wrong. (sorry this is probably one of the only times I've done a for next code in SC) And will x = 16 end the code, because I don't want every single inventory space written as the same item.
« Last Edit: December 03, 2009, 07:39:55 PM by Xiphos »

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Help with code
« Reply #1 on: December 03, 2009, 07:49:15 PM »
So this code is supposed to find a empty space in the inventory and then put the sword into just that space?

Xiphos


  • GMG-er

  • **


  • Posts: 676
Re: Help with code
« Reply #2 on: December 03, 2009, 07:50:17 PM »
Yeah. That's a good way to put it in simple words... Something I'm not too good at.


But it doesn't work because the inventory+str$(x) isn't proper syntax. I don't know what the correct syntax is. I also have a few other questions about the code somewhere in there.
« Last Edit: December 03, 2009, 07:52:55 PM by Xiphos »

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Help with code
« Reply #3 on: December 03, 2009, 07:56:32 PM »
You want an array for the inventory variable. You can't create a variable with a custom name like that.


-Gandolf

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Help with code
« Reply #4 on: December 03, 2009, 07:57:11 PM »
I would make a fake string array of the inventory spaces (just a string that looks like "blank,blank,sword,blank" or something) and then use the MID$ or NHFIELD$ trick to cycle through each inventory field in a for loop. SC doesn't support real string arrays yet so you have to use this trick.
« Last Edit: December 03, 2009, 07:58:02 PM by WarHampster »

Xiphos


  • GMG-er

  • **


  • Posts: 676
Re: Help with code
« Reply #5 on: December 03, 2009, 08:00:26 PM »
Okay I'll do that thanks. But if you replace once blank with something would it be limited to 5 letters?

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: Help with code
« Reply #6 on: December 03, 2009, 08:03:31 PM »
Sc does...
Code: [Select]
DIM myArray$(4)
LET myArray$(1) = "YO!"
LET myArray$(2) = "HEY!"
LET myArray$(3) = "SUP!"
LET myArray$(4) = "HI!"
FOR i = 1 to COUNTARRAY(myArray$)
 Â  PRINT myArray$(i)
NEXT
Stick that in open card event and watch what happens.


-Gandolf
EDIT: Here's an example of the inventory system as an array.
Open Card/Game
Code: [Select]
DIM inventory$(16)
Elsewhere
Code: [Select]
FOR x = 1 to COUNTARRAY(inventory$)
 Â  IF inventory$(x) = "" THEN
 Â        LET inventory$(x) = "fathersword"
 Â        LET x = COUNTARRAY(inventory$)
 Â  END IF
NEXT

EDIT2: For some reason it appears that the help syntax in Sc for arrays is out of date. I'll have to warn Mike. Says there aren't any string arrays yet.
« Last Edit: December 03, 2009, 08:09:01 PM by Gandolf »

WarHampster


  • GMG Extraordinaire

  • ***


  • Posts: 1501

  • The People's Moderator
    • Arcade of the Absurd
Re: Help with code
« Reply #7 on: December 03, 2009, 08:04:50 PM »
I just thought about this and realized that its even easier than that. Just use the REPLACE$ command, that replaces the first instance of oldstring in sourcestring with newstring. So in your case it will automatically replace the first blank spot with the new item. As for your question, I would assume that the new string is not limited to the same length as the oldstring.

EDIT - forget all that, I thought string arrays weren't implemented yet.
« Last Edit: December 03, 2009, 08:07:00 PM by WarHampster »

Xiphos


  • GMG-er

  • **


  • Posts: 676
Re: Help with code
« Reply #8 on: December 03, 2009, 08:25:41 PM »
Thanks for everything guys. Inventory added to game!