Coding Confusion- Simpler way to code?


#1

So I’m working on a game with many variables, and I have one stat that is the average of seven other stats. To hopefully make the whole situation easier to understand, I’ll go ahead and explain what they are.

The one “big stat” is Party Hunger that tracks if your party as a whole is starving. The others are the individual character hunger meters. I don’t just want to display the hunger meters separately because there will be one party member who is actually betraying the player and trying to kill the rest of them. It would simply be too easy to tell who the saboteur is if one character is fully fed and the rest are all starving because he ate all the food in secret. Since the deaths of the characters will be randomized throughout the game, or none of them could die at all, I was wondering if there is a simple way of coding how to calculate the average for the Party Hunger since once a character is dead, their hunger meter no longer needs to affect the average. The only way that I can think to calculate it is to list out every possibility of living and dead characters, and with seven characters, that is a LOT of combinations!

If you need me to try to explain this better before anyone can help, I can TRY, but don’t count on it. I’m bad at explaining things since it makes sense in my head and I thus assume that it makes just as much sense to everyone else.

Thanks for all the help in advance!


#2

@P_Tigras So to use a boolean variable, you do not to say *if Dead true? It just knows that Dead is true? Then how do you do something if the boolean is false?


#3

*if not (dead)

Or

*if dead=false


#4

@FairyGodfeather Thanks!

This thread may now be closed.


#5

What I would do is create a stat that determines if a character is alive or not. If they are alive, then their hunger is added to Party Hunger and a seperate counter is increased by one. If they are dead, Teh you go on to check the next character. There should be seven *if statements in total, one for each character. After all of the characters have been checked to see if they are living or not, you then divide Party Hunger by the counter you’ve been increasing by each alive character to determine the average.

I hope that makes sense to you.


#6

I think I see what you’re saying. I’ll try it sometime tomorrow and see if I can get it to only add in the correct characters’ hungers, and I’ll get back to you on that when I have finished it.

Although before I do that, I have another question. Would it work if I had this coded within the stat chart file, or would it be better to go ahead and do a check at each point in the story where a character can die and recalculate at each of those points?


#7

If you’re just displaying the average and it doesn’t actually affect gameplay, it would be simpler for you to code it within the stat screen. That way it runs and updates the script anytime a player wants to check it.


#8

Exactly. And it sounds like your gameplay could run fine on individual hunger rather than party hunger.

But if you did want it to affect gameplay, yes, you’d need to recalculate it (if anyone’s individual hunger/life has changed since the last time it affected something) before each bit of the game you want it to affect.

The shortest way of doing that would probably be to insert a subroutine, e.g.
*gosub hunger_calculation
as a one-liner everywhere you wanted to recalculate – which would require each chapter to have a bit of code like this somewhere (say at the bottom of the page, after “finish”):


*label hunger_calculation
* if char1living
  *set totalhunger +char1hunger
[etc. through all 7 characters...]
*set partyhunger totalhunger/survivors
*return

(The variables are what I might use to implement youwish12’s suggestion)


#9

@Havenstone Thanks! That’s precisely the sort of thing that I was thinking, but I was having a bit of trouble figuring out the exact coding/wording of it!


#10

I wouldn’t bother with all the *if charliving’s. There’s actually an even simpler way that takes cares of it all in one big formula on one line that can be used over and over.


@label hunger_calculation

*set partyhunger ((char1hunger*char1living)+(char2hunger*char2living)+ ....)/survivors

charNliving, where N is the character number, is a boolean value that is either 1 if the character is alive or 0 if the character is dead. If it’s 0 then the result of the multiplication is 0 which will have no effect when added to the total. Just keep the total number of the survivors accurate and the results of the equation will also always be accurate.


#11

It works! Thanks to everyone who gave me advice!

I sort of wish I’d eaten lunch and THEN done the coding because that way I could have seen P_Tigras’s equation before I did the coding with all the *if’s… Anyway, I have never coded anything outside of CS before, so I am still a complete noob at this. Here it goes… What’s a boolean? And how do they work? I’ve read about them, but I can never get them to work properly. I get errors and give up on them and use strings instead whenever a boolean seems as though it might work better. To me, it seems as though a boolean is basically a string with the words “true” and “false” substituted in for the two numbers 1 and 0, but I’m not sure.


#12

@Galador, a boolean is a variable whose value is always either TRUE or FALSE. In Choicescript, TRUE equates to 1, and FALSE to 0. The most obvious way to use them is in conditionals such as:


if Dead

  you died!

else

  you're alive!

If Dead is TRUE (1), then “you died!” will be printed. On the other hand if Dead is FALSE (0), “you’re alive!” will be printed.

There are also all sorts of nifty tricks you can do with booleans based on set theory and boolean algebra to simplify your code. The example I gave above is one such.