Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 2 [3]

Author Topic: Trying to make a game.  (Read 5686 times)

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Trying to make a game.
« Reply #30 on: July 10, 2009, 04:58:28 pm »

....
Ugh. I lost my post.

Summary of lost data:
Switch/case statements are best for state varables, not number comparison.

Actual code, from a win32 program, poorly written:
Spoiler (click to show/hide)
Logged
Eh?
Eh!

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: Trying to make a game.
« Reply #31 on: July 10, 2009, 05:00:12 pm »

lol, qwertyuiopas...

I'm studying the win32 api and failed to mention that it's used to retrieve messages... . :P

Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

termitehead

  • Bay Watcher
    • View Profile
Re: Trying to make a game.
« Reply #32 on: July 24, 2009, 02:28:29 pm »

Just my opinion, but I never use switch-case blocks. Seems like unnecessarily compicated syntax to do basicly the same thing as a string of if-else blocks. Plus if-else blocks let you use any boolean expression instead of having to use a number. I'd probably end up making a bunch if-else blocks anyway to come up with the number to feed to the switch block anyway.

Java-using seventeen-year-old here, BTW.

It's probably a good idea to do that if you only have 2-4 different branches to deal with.  Once you get to 10+ it gets silly to try to do a series of if statements in my opinion.  Especially when it is really mundane stuff where all you need is different blocks of code for different values of a variable

SolarShado

  • Bay Watcher
  • Psi-Blade => Your Back
    • View Profile
Re: Trying to make a game.
« Reply #33 on: July 24, 2009, 04:48:32 pm »

Oddly, I've reciently started using switch blocks. I'm working on a new project and using a lot more numerical variables than ever before. I also suddenly have no trouble remembering the exact syntax, which, I must admit, saves typing. But I still think it looks ugly.
Logged
Avid (rabid?) Linux user. Preferred flavor: Arch

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Trying to make a game.
« Reply #34 on: July 24, 2009, 06:17:29 pm »

It's ugly, in many cases, but it can look better than an equal amount of ifs.
Not that there is much diffrence in functionality when simply comparing the state of a variable...

Certainly, it ensures to readers that in amongst the else if(var ==???) there isn't else if(othervar != ???), and it can be clearer by isolating just the number/number-symbol.

Also, it can be used for clever hacks.
Did you know that switch-case has no problem if one case is in a nested if or loop?

I read it being used in that way for some clever coroutine thing, to jump straight into the loop after the first time. It's more acceptable than a goto, I guess.
Logged
Eh?
Eh!

codezero

  • Bay Watcher
    • View Profile
Re: Trying to make a game.
« Reply #35 on: July 26, 2009, 09:36:44 pm »

I started with qbasic, who's modern equivalent is freeBASIC. magellans' roguelike 'Prospector'(check it out, http://www.bay12games.com/forum/index.php?topic=34943.0) is written in freebasic. But if you've already committed to C there's not much point changing, they're fairly similar languages(even speed-wise), and freebasic probably isn't even any easier. Some good points of FB though are built in graphics functions and it's open source, with a friendly community. A bad(?) point is limited oop support in it's current state.

Example code:
Code: [Select]
select case n
case kick_door
if roll_to_kick() then
do_some_skill_up_stuff()
break_door()
end if
case door_broken
break_door()
end select
Select case auto breaks after each case, so there's some extra code there, but I wouldn't have set n up like that in the first place using fb.
« Last Edit: July 26, 2009, 09:41:16 pm by codezero »
Logged

Alexhans

  • Bay Watcher
  • This is toodamn shortto write something meaningful
    • View Profile
    • Osteopatia y Neurotonia
Re: Trying to make a game.
« Reply #36 on: July 26, 2009, 10:59:00 pm »

It's a matter of opinion if you feel is tidier or not...  BUT, switch IS faster.
Logged
“Eight years was awesome and I was famous and I was powerful" - George W. Bush.

sonerohi

  • Bay Watcher
    • View Profile
Re: Trying to make a game.
« Reply #37 on: July 29, 2009, 04:36:41 pm »

Alright, I had to take a break from learning for a while there (was away from my home computer with code::blocks and all my notes and such), and I'm hitting roadblocks again, or rather, I've passed the roadblocks because I had my ID on me, but I don't understand why the roadblock was there.

Structures: These are basically to be used for things like databases on a specific thing or person, right? Like, for example, my profile page. All the different int's and float's for my post count and activity % are all just part of the database that is Sonerohi.

Arrays: The only thing I can tell about these are that they are used as side-by-side variables, basically. So, if I wanted a 3-d game that had 100 tiles in width, 100 tiles in length, and 100 tiles in depth, I'd need a three dimensional array, right? The tutorial I'm using says that if the last slot in an array is filled, then there is a chance that the next space the memory is allocated to could be anywhere, which has the potential for bad things. So, I always need 1 extra slot in an array to prevent catastropich phail. The example code is pretty well filled up with comments that I can't make much sense out of how to make a 2d array that actually functions right, let alone a 1d array.
Logged
I picked up the stone and carved my name into the wind.

Vactor

  • Bay Watcher
  • ^^ DF 1.0 ^^
    • View Profile
Re: Trying to make a game.
« Reply #38 on: July 29, 2009, 08:21:19 pm »

i would need to know what you're trying to do with the array, and what you're having trouble with to help, also as far as structs go are you using C# or C++? 

Logged
Wreck of Theseus: My 2D Roguelite Mech Platformer
http://www.bay12forums.com/smf/index.php?topic=141525.0

My AT-ST spore creature http://www.youtube.com/watch?v=0btwvL9CNlA

qwertyuiopas

  • Bay Watcher
  • Photoshop is for elves who cannot use MSPaint.
    • View Profile
    • uristqwerty.ca, my current (barren) site.
Re: Trying to make a game.
« Reply #39 on: July 29, 2009, 09:20:37 pm »

Arrays?

In C and C++, they are 0-based, meaning the first element is 0, and the last is n-1.

There is no such thing as "the last element could be anywhere".

However, especially in C, where the compiler doesn't care if you treat an int as a pointer and then a char(though for the char it will probably convert it automatically), you can give a negative array index, or one greater than the array bounds. The issue is that the computer didn't give all it's RAM to storing your array. There are other numbers in there as well.

Great example: I had an ASCII pong-like thing(a bouncing highlight as an example test), but it would go one over on the right side. This was not a real problem unless it hit the lower right corner.
If it did, it would overflow the array and write the background colour of a nonexistant tile to be something.

This would not be much of a problem, except that that particular memory location was used for something else: The "ball"'s X velocity.

If it was something else, for example, a pointer stored there, it could severely mess up the data, and when the program next tries to access the pointer's target, it would likely edit a random bit of the memory. This is more than likely to cause a segfault if it had a pointer of it's own and it was "relocated" to uninitialized memory, or if the change caused the program to try to write over the portion of memory where itself was stored.

In some of the worst cases, a corrupt pointer corrupts another pointer that corrupts another pointer and so on until one causes a segfault.


Just remember:
For strings, they terminate with an invisible character 0 so a 8 char string needs 9 bytes of storage, but otherwise 8 chars of binary data only needs 8 chars of array. The diffrence is that it is stored in array[0] to array[7] instead of [1] to [8].

Naturally, not all programming languages use 0-based arrays, but those that don't are generally in the category of BASIC and some script languages.



A structure is just a bunch of variables grouped in a single memory location so you can pass a pointer to the first and get access to them all(AFAYNTN(As Far As You Need To Know), just pass a pointer to a struct, or even a whole struct, and everything stays together), but more importantly, structs can be allocated/created all at once and are kept together automatically, and you can have a single array of structs rather than an array of values for each value that would be in the struct.





I'm not a teacher, so if you didn't learn anything, it's the way I explained it.
Logged
Eh?
Eh!

sonerohi

  • Bay Watcher
    • View Profile
Re: Trying to make a game.
« Reply #40 on: July 30, 2009, 11:03:22 am »

Well, you clarified structures, and you explained the catastrophic failures that could occur by overflowing the array. Besides that, I didn't manage to catch much.

I'm using C++, and I'm not specifically trying anything yet with them, but I need to understand all about how they function and how to make them function.
Logged
I picked up the stone and carved my name into the wind.

Vactor

  • Bay Watcher
  • ^^ DF 1.0 ^^
    • View Profile
Re: Trying to make a game.
« Reply #41 on: July 30, 2009, 05:16:39 pm »

so for making your array of a 100x100x100 gridspace you would want to declare an array A[100][100][100].  You might want to leave yourself an extra line along the edges so that if anything is leaving the gridspace it can move into that line, and you can have code to bump it back in before it can move again, so you would go with A[102][102][102]  this gives you three dimensions in the array from 0 - 101.

I don't know C++ but maybe i'll help you figure out using an array
maybe something like this:

Code: [Select]
myArray = new int[102][102][102];
for (int x = 0; x < 102; x++)
     for (int y = 0; y < 102; y++)
          for (int z = 0; z < 102; z++)
myArray[x,y,z] = random.Next();

this (in C#) would cycle through the entire array, assigning a random int to each point in the array.

You could also manually fiddle with a particular part of the array by using its grid coords:

Code: [Select]
myArray[23,84,35] = 5;
as far as keeping something in the array, lets say you have something moving around, at the end of the cycle have a piece of code that checks to see if the moving object is in the edge row/column/page, and if it is have it move it back away from the edge at least one space.

It isn't necessary to use a 3d array to deal with 3 dimensions, you could, for example, have a series of objects that record their own 3d location, which are then rendered using their locations as a transform matrix.
Logged
Wreck of Theseus: My 2D Roguelite Mech Platformer
http://www.bay12forums.com/smf/index.php?topic=141525.0

My AT-ST spore creature http://www.youtube.com/watch?v=0btwvL9CNlA
Pages: 1 2 [3]