Jump to content


Photo

variables and now arrays? *Headache*


  • Please log in to reply
8 replies to this topic

#1 Invero

Invero

    GMC Member

  • GMC Member
  • 199 posts

Posted 07 December 2011 - 09:26 AM

I'm using Gamemaker 8.1 Reg.

I was just wondering if it's possible to add more variables in your game while the game is in play?
I am trying to make a smart NPC were it can memorize what you have typed earlier during the game so you may have a history of your trading past.

You type "Hi" to the NPC "Sam" now "Sam" will add the variable "Player=Hi" to itself. If the Player "You" type another word it would add another variable "Player1=Buy" if you typed buy to "Sam". I would like to know how it can detect the last variable name it added and add 1, 2, 3, 4 etc... to it then following with the = and the keyboard_string.

Example:
Player=Hi
Player1=buy
Player2=sword
Player3=no
Player4=wand
Player5=yes

Note: I know how to add more variables during game play to the NPC "Sam" I just don't know how to change the name of the variable if it's already in use by adding 1, 2, 3, 4 etc... to it before the equal sign.

If you don't understand plase reply and I will retype this thank you in advance!.

Edited by Invero, 07 December 2011 - 09:43 PM.

  • 0

#2 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2440 posts
  • Version:GM:Studio

Posted 07 December 2011 - 10:20 AM

Your attempt is a little wrong, but the thought behind it is good - and is actually supported in gm in the shape of arrays.
You can store your variables in arrays like this:
player[0]="Hi"
player[1]="buy"
player[2]="sword"
last=2
and find the last string like this:
laststring=player[last]

Edited by TheSnidr, 07 December 2011 - 10:21 AM.

  • 0

#3 Invero

Invero

    GMC Member

  • GMC Member
  • 199 posts

Posted 07 December 2011 - 08:19 PM

Your attempt is a little wrong, but the thought behind it is good - and is actually supported in gm in the shape of arrays.
You can store your variables in arrays like this:

player[0]="Hi"
player[1]="buy"
player[2]="sword"
last=2
and find the last string like this:
laststring=player[last]


Thank you TheSnidr I do appreciate the time you toke to reply to my topic. I have changed all my previous variables into arrays unfortunately I'm still in the same situation. I am Sad to say since you posted I was still unable to find the solution regarding my problem. Call it what you want I guarantee I left something out of place. I'm probably doing this wrong I'm just not sure as to what it is that I'm doing wrong.

Create Event:
Adding=0
Dat[1]=0
Dat[2]=0
Dat[3]=0
Dat[4]=0
Dat[5]=0

Enter Event:
Adding+=1
if Dat[Adding]=0
then
{
Dat[Adding]=1
}

The variable "Adding" works perfectly by adding a value of 1 once the enter key is released but it seems to be discarded when it's applied in the square brackets. What would I have to change in order for the variable "Adding" to remain active and add it's value within the square brackets of the array "Dat"?

PS: Thank you in advance once again to all who post on this topic regarding my noobish question.
  • 0

#4 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2440 posts
  • Version:GM:Studio

Posted 07 December 2011 - 08:43 PM

What's supposed to happen?
From what I can see, when the code is executed the first time (assuming Adding is at 0 to begin with), you set Dat[1] to 1 (as it is set to 0 to begin with). Next time it is executed, you set Dat[2] to 1. Next time Dat[3] to 1, and so on.
  • 0

#5 RubixCube

RubixCube

    GMC Member

  • New Member
  • 47 posts

Posted 07 December 2011 - 08:48 PM

It looks like you're miss understanding arrays. Think of the inside of the brackets as coordinates

dat[2+1] is actually just the variable dat[3]. dat[3] is just a variable. The math going on inside of the brackets is to find that variable.
So when you do Adding+=1 and put it in the brackets like this dat[adding], you're just changing the variable that you're looking at. You're not adding to the value stored in that variable.

Edited by RubixCube, 07 December 2011 - 08:51 PM.

  • 0

#6 Invero

Invero

    GMC Member

  • GMC Member
  • 199 posts

Posted 07 December 2011 - 09:34 PM

TheSnidr is correct as to what I am trying to accomplish with the code I provided above. So far I have failed to do what I wish to be done... I believe RubixCube is correct when he or she says "It looks like you're miss understanding arrays." I will definitely look into arrays they should be easy to learn.
RubixCube just a question or to who ever reads this. When you "RubixCube" explained it as "dat[2+1] is actually just the variable dat[3]" would this mean if I wish to accomplish what TheSnidr has explained on his previous post would I need to place a mathematical equation within the square brackets? If this is not true how would I proceed in adding to the value stored in that variable not once but in a pattern of 1 everytime the enter key is released for example?

~ Thank you once again.

Edited by Invero, 07 December 2011 - 09:36 PM.

  • 0

#7 loverock125

loverock125

    GMC Member

  • GMC Member
  • 1612 posts
  • Version:GM8

Posted 07 December 2011 - 09:46 PM

You can put any mathematical equation within the brackets, or even variables.

Arrays are the same thing as variables. The only difference is that when using variables (not arrays) you are using the name to determine which variable you want to access while in an array you use an integer inside the brackets to determine which variable you want to access.

For example:
player0 = 'Hi';
player1 = 'buy';
player2 = 'sword';

To get access to one of these variables you would normally need to type the name. Sometimes you may know the name 'player' but not the number after the name (0,1 or 2), or you may want to initialize a lot of variables at once without the need to type each one of them individually. That's when arrays come handy.

player[0] = 'Hi';
player[1] = 'buy';
player[2] = 'sword';

//And to access those variables:
player[1] = 'sell';

//To initialize an array of 20 indexes at once you need to use a loop.
for(i=0;i<20;i+=1)
{
//this sets a variable i to 0, and while i is below 20 it keeps executing the following code and adds 1 to i after each execution.  
//This is called a loop, and this particular loop basically loops 20 times.

player[i] = ""; 

//This sets all values to an empty string. Remember, the variable i will go from 0 to 20.  This way you initialize 20 
//variables (array) at once.
}


I hope this was helpful,

Good luck!

Edited by loverock125, 07 December 2011 - 09:47 PM.

  • 0

#8 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9260 posts
  • Version:Unknown

Posted 07 December 2011 - 11:45 PM

TheSnidr is correct as to what I am trying to accomplish with the code I provided above. So far I have failed to do what I wish to be done... I believe RubixCube is correct when he or she says "It looks like you're miss understanding arrays." I will definitely look into arrays they should be easy to learn.
RubixCube just a question or to who ever reads this. When you "RubixCube" explained it as "dat[2+1] is actually just the variable dat[3]" would this mean if I wish to accomplish what TheSnidr has explained on his previous post would I need to place a mathematical equation within the square brackets? If this is not true how would I proceed in adding to the value stored in that variable not once but in a pattern of 1 everytime the enter key is released for example?

~ Thank you once again.

If what TheSnidr said is, in fact, what you're trying to do...then you've done it already :lol: . He was just explaining what the code you posted already does.

So if you think it's not working, then your problem is elsewhere. What makes you think it's not working? As TheSnidr said, your code would change the next array index from 0 to 1 every time the ENTER key is pressed. So the first time I press ENTER, it will set the first array entry to 1. The next time I press ENTER, it will set the second array entry to 1. And so on.

Why do you suspect that's not happening? What other code are you using to check that?

-IMP
  • 0

#9 Invero

Invero

    GMC Member

  • GMC Member
  • 199 posts

Posted 08 December 2011 - 01:30 AM


TheSnidr is correct as to what I am trying to accomplish with the code I provided above. So far I have failed to do what I wish to be done... I believe RubixCube is correct when he or she says "It looks like you're miss understanding arrays." I will definitely look into arrays they should be easy to learn.
RubixCube just a question or to who ever reads this. When you "RubixCube" explained it as "dat[2+1] is actually just the variable dat[3]" would this mean if I wish to accomplish what TheSnidr has explained on his previous post would I need to place a mathematical equation within the square brackets? If this is not true how would I proceed in adding to the value stored in that variable not once but in a pattern of 1 everytime the enter key is released for example?

~ Thank you once again.

If what TheSnidr said is, in fact, what you're trying to do...then you've done it already :lol: . He was just explaining what the code you posted already does.

So if you think it's not working, then your problem is elsewhere. What makes you think it's not working? As TheSnidr said, your code would change the next array index from 0 to 1 every time the ENTER key is pressed. So the first time I press ENTER, it will set the first array entry to 1. The next time I press ENTER, it will set the second array entry to 1. And so on.

Why do you suspect that's not happening? What other code are you using to check that?

-IMP


I actaully just relized that I solved my own problem without even acknowledging it lol. I feel so noobish at this time but gave me a good laugh lol :P. Thanks for all the help to everyone that has posted here and helping me understand arrays. Thanks TheSnidr for explaining it even though I did not notice you were lol. Thank you also IceMetalPunk for pointing it out if not for you I would have still been confused wondering what I'm doing wrong lol.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users