2019-06-24 07:46:55

I read through the bgt chapters 10 and 11. I understand the inheritance and interfaces. But here is my question. Let's say I have 2 people. So i have 2 objects with different names, amount of money, and popularity status. How would I refer to player 1's money? Or player 2's money? Do I need to add an identity variable and say, if(players[j].id==1)
//code

I initially wrote i, but site gave an error so I changed to j.

What is the i for? Is that where I enter the player? Like for example, players[2].money, will that refer to player 2's money? I have many more questions but this one is the most confusing

Roses are red,
Violets are yellow;
You challenge me to change my signature,
I say oh hell no.

Thumbs up

2019-06-24 08:24:14

Let's make it simple and fast: You code a class named player, with a property named money. Then you create two instances of that class and asign an amount of money to each of them. Now you can use player2.money; to get player2's money which is a number different than player 1's money. Here's the code
class player {
int money;
//constructor
player(int m) {
this.money=m;
}
}

void main()
{
player1=player(30)
player2=player(3400)
alert("information", "player1 is homeless because he got only "+player1.money+" dollars, While player2 soon buys a cor I9 laptop with 64 gigs of RAM and 25 TB SSD, only because he got "+player2.money+" dollars");
}
I hope it was clear. Also player2 lives in heaven. Don't go after him. You fail at finding him around current earth

Add me on skype: kianoosh.shakeri2
Or follow me on twitter @kianoosh shakeri

2019-06-24 08:39:00

lol ok thank you.

Roses are red,
Violets are yellow;
You challenge me to change my signature,
I say oh hell no.

Thumbs up

2019-06-24 11:49:48

Remember, classes are just objects. So, if you want to identify them by some id, you can use arrays or maybe even better dictionaries.

Roel
golfing in the kitchen

Thumbs up

2019-06-24 14:45:32

Using the term better for bgt dictionaries isn't right at all time. BGT dictionaries can be a performance smasher if you go to far with them and set a lot of keys for systems like maps. In other languages however, It is probably right because it's a very neat way of handling/saving/returning the data, and they work nearly perfectly fine in other languages when it comes to map creation with dictionaries

Add me on skype: kianoosh.shakeri2
Or follow me on twitter @kianoosh shakeri

2019-06-28 06:55:04 (edited by Zarvox 2019-06-28 07:15:21)

I'm having a very very difficult time with this. I can make 5 players, but bgt doesn't actually know that there are 5 players. For example, a for loop that makes players p1 through p5. Then if I press space it speaks p5's money. Only problem is, bgt wants to complain because it doesn't actually see p5.

String amount="p"+index;
//index is the for loop counter
player amount;
//that creates a player
We have 5 players now, labeled from p1 to p5. But the code for KEY_SPACE, speak(p5.money) does not work even though p5 exists. Help please

Update: I noticed why it is not working. It is creating the players inside of the for loop and so they are not global. How can I turn them global after the for loop?

Roses are red,
Violets are yellow;
You challenge me to change my signature,
I say oh hell no.

Thumbs up

2019-06-28 09:20:33

Ok. What you're doing isn't right. I would declare an array of type player before the for loop like this:
[email protected][] players;
now in the for loop, I do this:
players.insert_last(player());
This should create 5 players in the players array. Then you can access player5's money like this:
speak(players[5].money);
Oh by the way you can't name a variable what is in a string by just calling the string.
You can't dothis:
string a="hello";
int a=5;
then expecting this to work:
speak(hello);
and run with no errors.

Add me on skype: kianoosh.shakeri2
Or follow me on twitter @kianoosh shakeri

2019-06-28 13:43:08 (edited by Zarvox 2019-06-28 14:19:47)

I have gotten the array to work, and I can create an infinite amount of players and it will cycle back to the first player correctly. Thank you so so much for helping me with this, I think I'm good on using classes now.

Roses are red,
Violets are yellow;
You challenge me to change my signature,
I say oh hell no.

Thumbs up

2019-06-28 14:49:58

very welcome

Add me on skype: kianoosh.shakeri2
Or follow me on twitter @kianoosh shakeri