Misc questions

Started by
3 comments, last by Nypyren 11 years, 4 months ago
Hey friends. I was thinking about some stuff as I was going to sleep last night and I was curious how to implement them into a project. So far I have only made some really basic games using C++ and Allegro (like, tic tac toe or whatever). I also want to note that when I press enter in this text box, it does not make a new line. I have no idea why. So this is going to be one giant paragraph, sorry. Anyway Q1: Is using a giant list of if/else if statements or a giant switch ever a bad idea? Like, is there another way to go about checking for many conditions? For example, I was imagining creating a pet game in which you play through days which are like turns. You can do things during the turn in any order, but at the beginning of the day, you will be updated on the pet's status if there is a trouble or curiosity. There may be many troubles like... sick, injured, run away, stressed out, tired, grown up, angry, acting strangely, the pet's birthday, etc etc etc. Is there a better way to check for these other than a giant else if list or something similar? Also, is it very draining on resources to use a giant case list like this, or is it really no big deal and I'm overthinking it? Ok, now for Q2: How would you implement something to tell if the user is "petting" on the pet, for example, you could have pets of many sizes and the "petting" is clicking and holding a click, then moving back and forth at least two times across the pet's head area. The pet could also be moving around so its location is very dynamic. I figured use some kind of collision detection with a cursor to tell if you are clicking in the head area but I don't know really how you would go about recognizing that you are moving left and right without letting up (and not accepting up and down movements, circle movements, etc.). This seems kind of advanced for me. Just a really basic description is fine, no codes or anything needed. It is a curiosity. And lastly, Q3: I am going to be downloading Unity soon as I just got a new computer and would like to try it out. I was wondering, how easy is it to create a project for one platform in mind (for example, windows exe) and then use the same project to make another platform version (like iOS)? Is it as simple as a Save As... or is there a lot more involved? I'm sure this answer is available easily somewhere but I don't really know what to call that so I don't know how to search for it. Again, sorry for giant paragraph... I really wish pressing enter would work (it works everywhere but in this text box...) EDIT: Wanted to add that I was also thinking perhaps for all of the "troubles" they could be set like flags, and then there could just be a "trouble" flag that would go up so if the trouble flag is down, it won't check for all the troubles. But IDK if checking lots of ifs is even a big deal. I don't know a lot about making efficient code yet. I just try to make code that works.
Advertisement
Q1: Is using a giant list of if/else if statements or a giant switch ever a bad idea?
Like, is there another way to go about checking for many conditions?[/quote]
Not "bad" as in, horrible, but there are better ways. I'd suggest looking up state machines and game states.
States often boil down to if() statements or something similar anyway, but they are much easier to maintain and expand, and are easier to manage in a large project.

For example, I was imagining creating a pet game in which you play through days which are like turns. You can do things during the turn in any order, but at the beginning of the day, you will be updated on the pet's status if there is a trouble or curiosity. There may be many troubles like... sick, injured, run away, stressed out, tired, grown up, angry, acting strangely, the pet's birthday, etc etc etc. Is there a better way to check for these other than a giant else if list or something similar?[/quote]
Yeah, sounds like 'states' would be your best bet. The time of day might be a state, and the pet's status would definitely be a state. And the current player interactions might be managed by a "gamestate", which probably has its own logic (taking into account the states of the different objects in the game).

Also, is it very draining on resources to use a giant case list like this, or is it really no big deal and I'm overthinking it?[/quote]
Don't worry about resources or optimization until you actually encounter a slowdown; then you can profile and find out where the slowdown actually is.

Q2: How would you implement something to tell if the user is "petting" on the pet, for example, you could have pets of many sizes and the "petting" is clicking and holding a click, then moving back and forth at least two times across the pet's head area. The pet could also be moving around so its location is very dynamic. I figured use some kind of collision detection with a cursor to tell if you are clicking in the head area but I don't know really how you would go about recognizing that you are moving left and right without letting up (and not accepting up and down movements, circle movements, etc.).[/quote]
Hmm, that would be an interesting challenge for myself as well. Here's one possible way that comes to mind: I'd use a "mousemap" for the creature's sprite. This would be a hidden image that the player can't see, but that the program "sees". It'll be invisibly "overlaid" over the monster's sprite. I'd assign each body part of the creature a specific color, then when the mouse is over the creature, I can find what (invisible) color is under the mouse in the mouse map. For example, the left ear might be red, the right ear might be green, and so on. This way the game knows whether you clicked on the creature's ear to whisper something to it, clicked on its mouth with food to feed it, or clicked on its head to exercise it.

Example mousemap I just slapped together: (Ignore the white flecks of pixels that I missed)
creaturee.png mousemap.png
Creature sprite | Mouse map

([size=2]I'm just using this image as an example. If you actually use Pokemon, Nintendo will sic their lawyers on you faster than you'd think possible)

The same applies to 3D, except you texture your 3D model with solid colors.

You'll need a mousemap like that for every normal movement frame of the creature's animation (you can skip special animation frames, since you probably don't want the user interacting with the creature during those frames anyway).

Now, since you want to find out if the player is "scratching"/"petting" the head, I put the head into three colors: the left, the right, and a thin buffer in the middle. Now all we need to do is:

  1. When the user clicks the mouse, find out if he's over the creature at all.
  2. If so, set "touching creature" to true.
  3. As he moves the mouse back and forth, if he touches the left or right side of the head, set a variable to that side of the head.
    sideOfHeadLastTouched = LeftSideOfHead;
  4. As he moves the mouse, if he crosses over and touches the other side, you do this:
    if(sideOfHeadLastTouched == LeftSideOfHead)
    {
    timesScratched++;
    }
    sideOfHeadLastTouched = RightSideOfHead;

  5. If 'timesScratched' goes above a certain number, say, three, then the user is officially "scratching"/"petting" the creature.
  6. When the user lets go of the mouse, 'timesScratched' and 'sideOfHeadLastTouched' get reset.

This seems kind of advanced for me.[/quote]
At first it seemed advanced to me too. But like all advanced problems, if you think of the individual pieces of what you need to accomplish, it's easier to find a solution.
(Piece A: Find what part of the body the mouse is over. Piece B: Figure out how many times the mouse goes between two points on the body)

And lastly, Q3: I am going to be downloading Unity soon as I just got a new computer and would like to try it out. I was wondering, how easy is it to create a project for one platform in mind (for example, windows exe) and then use the same project to make another platform version (like iOS)? Is it as simple as a Save As... or is there a lot more involved? I'm sure this answer is available easily somewhere but I don't really know what to call that so I don't know how to search for it.[/quote]
It's called "cross platform". I don't use Unity, so I'm not familiar with it. However, even if there was a "Save as iOS" option, it wouldn't be able to figure out in what way mouse controls should change to touch controls (which are wildly different) or in what the GUI should change to fit on the smaller screen. There will still be at least some work you'll have to manually decide.

sorry for giant paragraph... I really wish pressing enter would work (it works everywhere but in this text box...)[/quote]
Odd. You ought to report that here, with your browser version and OS, because enter should work in the text box (it works for me on Google Chrome).
Thanks a lot! The mousemap actually seems very simple to me, now, too. One of the things I love about programming is the more I learn about it, the more I realize it is always pretty simple/easy. Sometimes if you are thinking about a problem the wrong way, you can get stuck or frustrated, but the solution ends up being pretty simple in the end. I tell people I am teaching myself to program and they say "Wow that sounds so hard" but I want to say "It's really easy when you really start trying" but they think I am bragging that I am smart or something. Anyway, your solutions are very helpful and I was planning on learning about states next since it seems to be where everything is pointing anyway (to make bigger games/projects than simple board/number games and stuff). Even though I'm sure I'll learn about it, but is it easy to implement having more than one condition state for the pet? Like it could have its birthday, be stressed out, and have an injury all at the same time (... what a sad birthday...) Well, I'll figure it out. And I will go report that. I am using IE10 (it's like... 10.0.9something something) and Windows 8. In case you were curious O_o;; Thanks again!

I also want to note that when I press enter in this text box, it does not make a new line.

Hold Alt and on the number pad hit 013, then release Alt.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

You can work around the IE10 enter key bug by hitting the 'Toggle editing mode' button on the reply box (it's the the upperleftmost button). The enter key works properly in the raw editing mode.

This topic is closed to new replies.

Advertisement