[QUOTE=Ehmmett;41065506]but the size is variable so i don't want to make the array manually, it could be any size.
also i said table, but i meant array, my bad.[/QUOTE]
Tables in Lua automatically resize to their contents, but you don't have to make the table manually. If you were to switch around your for loops a bit, you can easily create the table for the row in the outer loop, then index into it in the inner loop. You don't really want to do that though because it's unnecessarily wasteful, all those extra tables don't come for free.
[lua]
local size = 10
local field = {}
for x = 1, size do
field[x] = {}
for y = 1, size do
field[x][y] = cats
end
end
[/lua]
Doing it with the maths is the "nicest" solution though.
I said array, but I meant table. There are no arrays - only tables in Lua. The difference is how you use them.
[editline]17th June 2013[/editline]
[QUOTE=Ehmmett;41065506]but the size is variable so i don't want to make the array manually, it could be any size.
also i said table, but i meant array, my bad.
[editline]16th June 2013[/editline]
you see there that res is 10, so i want a 10 by 10 array, but i also want to be able to change that easily[/QUOTE]
If you replace 10 with res in my earlier examples it will still work for any size. I was just too lazy to write the extra line.
[QUOTE=lavacano;41062271]guys, bash script makes my head spin
why is this while loop wrong
[code]i=0
test=""
while [ $i -lt $TGTCOL ]; do
test+="a"
i+=1
done[/code]
$TGTCOL is 229, but it's only appending four "a"s to the string[/QUOTE]
i+=1 treats i as a string and appends "1".
You want ((i+=1)), the double paranthesis signals arithmetic evaluation in bash.
[QUOTE=ZeekyHBomb;41058046]You have some sort of logic tick, right? Just do it there.
And the syntax I provided should work. If it does not, I'm going to need more information about the Block type (declaration) and the Collision function (signature).[/QUOTE]
Here's the class:
[code]
class Block
{
public:
// Methods
Block(sf::Vector2f dimension, sf::Vector2f position, sf::Color color);
~Block();
template <class Obj>
bool Collision(Obj object);
// Atributes
sf::RectangleShape blockShape;
sf::Vector2f blockPosition;
sf::SoundBuffer hitBuffer;
sf::Sound hitSound;
float blockLeft, blockRight, blockTop, blockBottom;
};
[/code]
And the method:
[code]
template <class Obj> bool Block::Collision(Obj object)
{
// Collision variable
bool getCollision = false;
// Object borders
float objLeft = object.getPosition().x - object.getRadius() / 2;
float objRight = object.getPosition().x + object.getRadius() / 2;
float objTop = object.getPosition().y - object.getRadius() / 2;
float objBottom = object.getPosition().y + object.getRadius() / 2;
if (objLeft <= blockRight)
getCollision = true;
else if (objRight >= blockLeft)
getCollision = true;
else if (objTop <= blockBottom)
getCollision = true;
else if (objBottom >= blockTop)
getCollision = true;
// Destroy the object in case of collision
if (getCollision)
this->~Block();
}
[/code]
The information is enough?
Ps.: I think I don't need this bool return.
[cpp]#include <thread>
class Block
{
public:
template<class Obj>
bool Collision(const Obj&){}
};
int main()
{
Block b;
int i;
std::thread t(&Block::Collision<int>, b, i);
}[/cpp]
This uses std::thread, but sf::Thread should be about the same.
The syntax works for me this way.
I have determined the hardest part about programming is coming up with ideas of WHAT to program.
Anyone got any suggestions for a project that'll teach me a lot?
I'm pretty good with C++, I'd say advanced-intermediate :P
[QUOTE=Jellyman;41069952]I have determined the hardest part about programming is coming up with ideas of WHAT to program.
Anyone got any suggestions for a project that'll teach me a lot?
I'm pretty good with C++, I'd say advanced-intermediate :P[/QUOTE]
Try asking someone who doesn't know programming if there's a repetitive task that they would benefit from automating, or whether there's a (simple) program they wish they had.
If it's feasible and doesn't exist yet it's likely either really tedious or will teach you about at least three different APIs/algorithms/patterns/whatever.
I have networking working in my game, but I'm mostly flying by the seat of my pants so I'm probably doing a bunch of stuff wrong.
I'm using UDP, even though it's not a high-traffic real-time game. I'm talking about a few hundred packets per game, total. Should I be using TCP instead?
Even though I'm using UDP, I've been assuming that packets aren't being lost and are arriving in order. So far this has worked fine, but I've only tested on my local network. Is packet loss a serious problem over the internet?
[QUOTE=Larikang;41072472]I have networking working in my game, but I'm mostly flying by the seat of my pants so I'm probably doing a bunch of stuff wrong.
I'm using UDP, even though it's not a high-traffic real-time game. I'm talking about a few hundred packets per game, total. Should I be using TCP instead?
Even though I'm using UDP, I've been assuming that packets aren't being lost and are arriving in order. So far this has worked fine, but I've only tested on my local network. Is packet loss a serious problem over the internet?[/QUOTE]
Not really unless there's a problem with the sender or receivers connection. If you want to be really sure you could always implement a simple ACK response system.
If you don't know what that is:
Client 1 sends an update packet to Client 2.
Client 2 gets the update and responds with a tiny packet, usually 1-2 bytes, to Acknowledge it got the update.
Client 1 waits for that Acknowledgment packet and if it doesn't receive it in a certain time (a second or so, depending on how often you send packets) it will resend the update and repeat the procedure.
I'd suggest having some limit on the amount of times it tries to resend though. If a response from Client 2 hasn't been given after say 5 retries then you could safely assume the connection has been lost between them.
(TCP uses this method to validate delivery)
[QUOTE=Lord Fear;41072783]Not really unless there's a problem with the sender or receivers connection.[/QUOTE]
Yes it is really a problem. The question is whether your game can benefit from handling packet-loss differently from TCP.
[url]http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/[/url]
If latency doesn't matter for your game (i.e if it's turn based, or a card game, etc) then there's no reason to bother with UDP.
[QUOTE=Larikang;41072472]I have networking working in my game, but I'm mostly flying by the seat of my pants so I'm probably doing a bunch of stuff wrong.
I'm using UDP, even though it's not a high-traffic real-time game. I'm talking about a few hundred packets per game, total. Should I be using TCP instead?
Even though I'm using UDP, I've been assuming that packets aren't being lost and are arriving in order. So far this has worked fine, but I've only tested on my local network. Is packet loss a serious problem over the internet?[/QUOTE]Packet loss occurs only to a very small percentage of packets (~1%) if there are no problems with the connection. To be safe, it is best to assume that it will happen to any packet, at any time. Packet loss can get very very bad if there are any issues with the connection.
As for the order of packets, it is VERY likely that quite a few of your packets WILL arrive out of order when sent over the internet. Make sure that you are not relying on them being in the correct order to avoid any problems.
If you're implementing a scripting language in a big project, should that be something that is done early or later in the project's development?
Any good Monogame tutorials out there? (Preferably for Visual Studio)
[QUOTE=ZeekyHBomb;41069206][cpp]#include <thread>
class Block
{
public:
template<class Obj>
bool Collision(const Obj&){}
};
int main()
{
Block b;
int i;
std::thread t(&Block::Collision<int>, b, i);
}[/cpp]
This uses std::thread, but sf::Thread should be about the same.
The syntax works for me this way.[/QUOTE]
thread is not a member of std. Still trying to use sf::Thread. I just changed the bool return to void and did this way:
[code]
sf::Thread blockThread(&Block::Collision<sf::CircleShape>ball, &blocks);
blockThread.launch();
[/code]
There's a ',' missing before ball.
[editline]18th June 2013[/editline]
Also, are you sure you didn't get the order of arguments confused?
[QUOTE=ZeekyHBomb;41080396]There's a ',' missing before ball.
[editline]18th June 2013[/editline]
Also, are you sure you didn't get the order of arguments confused?[/QUOTE]
I'm following the documentation ([url]http://www.sfml-dev.org/documentation/2.0/classsf_1_1Thread.php[/url]) not sure what I'm doing I just thought that use thread is the best way to get the collisions. Moreover, I don't program C++ a while ago. :-/
sf::Thread is too limited for your use-case.
If you don't want to use C++11 std::thread consider using boost::thread.
If it is a relatively simplistic game and you can probably get away without giving any thought to multi-threading though and just do the check in your logic tick.
Hey, I'm making a game in C# and I'm having trouble figuring out how to split up my files. Right now in my main I create a GameLogic class that contains all the variables and methods for game logic, then create a GameForm class and give it the GameLogic. The GameForm does all the drawing and sends the user input to GameLogic. Then I create a GameThread and pass both the logic and form to that and the thread just has an infinite loop of update then draw. This is causing annoying problems like having to grab all the variables used for drawing from the logic class every time I need to use them in form. Also I'm holding variables that are pretty much only used in the form in the logic because I need them there for certain calculations. Is there a common practice of how to separate these parts in a game?
[QUOTE=Lord Fear;41072783]Not really unless there's a problem with the sender or receivers connection. If you want to be really sure you could always implement a simple ACK response system.
If you don't know what that is:
Client 1 sends an update packet to Client 2.
Client 2 gets the update and responds with a tiny packet, usually 1-2 bytes, to Acknowledge it got the update.
Client 1 waits for that Acknowledgment packet and if it doesn't receive it in a certain time (a second or so, depending on how often you send packets) it will resend the update and repeat the procedure.
I'd suggest having some limit on the amount of times it tries to resend though. If a response from Client 2 hasn't been given after say 5 retries then you could safely assume the connection has been lost between them.
(TCP uses this method to validate delivery)[/QUOTE]
I have some ACK set up for really important packets, but I've been wondering if it would be better to use it for every packet. But then I figured if I did that maybe I might as well use TCP. Sounds like I can maybe put it off though.
[editline]18th June 2013[/editline]
[QUOTE=Zwolf11;41085829]Hey, I'm making a game in C# and I'm having trouble figuring out how to split up my files.[/QUOTE]
Don't separate things that don't need to be separate. I start by putting everything together in one big, ugly main function, and slowly separate out pieces that I find are independent. On my current game, it took me about 2 months before I saw a nice way to separate out the game logic.
[QUOTE=Larikang;41087372]Don't separate things that don't need to be separate. I start by putting everything together in one big, ugly main function, and slowly separate out pieces that I find are independent. On my current game, it took me about 2 months before I saw a nice way to separate out the game logic.[/QUOTE]
On a similar note, is it cool to have public field variables? I learned that all field variables should be private with public get and set methods. If I showed my source code to someone looking for coding practices, would they think that's unprofessional?
Public variables have their place, though that place is not often required. Do you want to be able to read and write the variable without any restriction? Bam, public.
Most of the time you don't want that though. You want some kind of validation when writing to the variable, or you want to prevent side-effects when reading the variable. Hence private with getters/setters.
[QUOTE=Zwolf11;41089571]On a similar note, is it cool to have public field variables? I learned that all field variables should be private with public get and set methods. If I showed my source code to someone looking for coding practices, would they think that's unprofessional?[/QUOTE]
So there's this idea that [url=http://stackoverflow.com/questions/441309/why-are-mutable-structs-evil]structs should be immutable in C#[/url], but in a lot of game-related libraries, structs have public fields and mostly static methods that use ref/out to pass the structs around. They do this because:
1) Blittable structs allocated inside of a method are stack-allocated, reducing how much garbage is generated to nothing.
2) Static methods are faster that instance methods in general. Structs can't be null, so the null-check on a callvirt instruction is pointless and takes unnecessary time.
3) By default, structs are passed by value, meaning a copy is made every time you return or pass one in as a method parameter. The use of ref and out passes the structs by reference, preventing a copy from being made, which takes time.
4) These libraries assume you have some idea of what you're doing. Yes, it would be safer to make your Vector3 class immutable and it may prevent a few bugs here or there, but the performance cost in tight math loops is way too high to be worth it. Most bugs caused by thinking you're modifying the original when you're modifying a copy will be very noticeable in a game and it won't take too long to point out.
5) If you're using /unsafe, which some game libraries do, an array of structs are sequential in memory, This makes it nice and easy to use with pointers. Just "fixed(Vector3* ptr = vecArray)" and you have a Vector3* that you can manipulate or pass to a native library directly, without making a copy or anything.
Generally this stuff won't fly outside of games though. For the ~5% of a game's code that's called on every frame, performance is key. Safety/correctness are less important. Most other libraries/applications would choose less efficient but harder to mess up code.
[editline]18th June 2013[/editline]
And all of that optimization stuff is specific to C# and probably a few other CLI-based languages. From your other posts, I'm assuming you're a beginner writing games in C#, don't feel intimidated if any of it is over your head, I've been doing this for a while now and have gotten a pretty good idea of what's going on inside the CLR when some C# code is run, I understood none of that stuff when I was first starting.
[QUOTE=Zwolf11;41089571]On a similar note, is it cool to have public field variables? I learned that all field variables should be private with public get and set methods. If I showed my source code to someone looking for coding practices, would they think that's unprofessional?[/QUOTE]
If you use [code]public Type Name {get; set;}[/code] instead you have all of the benefits from a public field in terms of speed (because the accessors should be inlined at the calling site) but the code won't break if you add validation and an explicit backing field later.
[editline]19th June 2013[/editline]
[QUOTE=robmaister12;41089948]1) Blittable structs allocated inside of a method are stack-allocated, reducing how much garbage is generated to nothing.[/quote]
This is mostly important for embedded/XBox 360 games, the garbage collector on a desktop platform is threaded and non-blocking, so there will usually be no noticeable performance impact from that. On XBox 360 there can be stuttering because the GC pauses execution.
[QUOTE]2) Static methods are faster that instance methods in general. Structs can't be null, so the null-check on a callvirt instruction is pointless and takes unnecessary time.[/quote]
Structs never have virtual calls, the instance calls act exactly like a static call with a reference to the struct as first parameter.
The static methods are just a coding practice to separate mutating from constant operations.
[QUOTE]
5) If you're using /unsafe, which some game libraries do, an array of structs are sequential in memory, This makes it nice and easy to use with pointers. Just "fixed(Vector3* ptr = vecArray)" and you have a Vector3* that you can manipulate or pass to a native library directly, without making a copy or anything.[/quote]
Looks like a wording mistake, (struct) arrays are always sequential. /unsafe only means you can use fixed and so on.
To guarantee compatibility with a native library you have to use [StructLayout] to set the memory layout to a defined format. (works on classes, too)
MySQL question ahoy
So basically, I have 2 tables. Table 1 holds the identity and name of a "server group" (sg_id & sg_name, where sg_id is the primary key).
Table 2 holds the modules each server group should load (sg_id & sg_modules). Basically, in Table 2, both columns come together to form a composite primary key, but since sg_id also references table A's sg_id I also want it to be a foreign key, and I'm not entirely sure how to do it.
The queries should look something like this (they're auto-generated from my database system):
Table 1:
[code]
CREATE TABLE sg_groups(
sg_id VARCHAR(32) NOT NULL,
sg_module VARCHAR(32) NOT NULL,
PRIMARY KEY ( sg_id )
)
[/code]
Table 2:
[code]
CREATE TABLE sg_modules(
sg_id VARCHAR(32) NOT NULL,
sg_modules VARCHAR(32) NOT NULL,
PRIMARY KEY ( sg_id, sg_module )
)
[/code]
So, is it valid to use sg_id as a foreign key while it is also part of a composite primary key, and how would the query look?
I'm not entirely sure how you managed to create two tables with the same name.
[editline]19th June 2013[/editline]
You edited that didn't you :v:
[editline]19th June 2013[/editline]
FOREIGN KEY (`sg_id`) REFERENCES `sg_groups` (`sg_id`)
should work
I did edit that :v:, thank you
[QUOTE=Tamschi;41091682]If you use [code]public Type Name {get; set;}[/code] instead you have all of the benefits from a public field in terms of speed (because the accessors should be inlined at the calling site) but the code won't break if you add validation and an explicit backing field later.
[editline]19th June 2013[/editline]
This is mostly important for embedded/XBox 360 games, the garbage collector on a desktop platform is threaded and non-blocking, so there will usually be no noticeable performance impact from that. On XBox 360 there can be stuttering because the GC pauses execution.
Structs never have virtual calls, the instance calls act exactly like a static call with a reference to the struct as first parameter.
The static methods are just a coding practice to separate mutating from constant operations.
Looks like a wording mistake, (struct) arrays are always sequential. /unsafe only means you can use fixed and so on.
To guarantee compatibility with a native library you have to use [StructLayout] to set the memory layout to a defined format. (works on classes, too)[/QUOTE]
Oh yeah, I wasn't being too clear on point 5, I kinda mixed 3 points into one.
And the virtual call thing makes perfect sense, seeing as structs can't be null.
[QUOTE=robmaister12;41098773]And the virtual call thing makes perfect sense, seeing as structs can't be null.[/QUOTE]
The reason is a bit different, they can't have virtual calls because structs don't have inheritance or type information/a call table.
If they are used with an interface they are first boxed and the virtual call is performed on that box object.
Another side effect is that changes from interface calls are discarded for structs, because they act on the boxed copy.
The null check is a different matter, it's imposed by the runtime where needed and afaik can be turned off in Mono with certain command line parameters.
I'm trying to implement a Finite State Machine in C++. What would be the best way to do this?
Trying to build my test OS, but it's failing:
[code]
bin/boot.o: In function `_start':
src/boot.asm:(.text+0x6): undefined reference to `kernel_main'
[/code]
Copypasta'd from the osdev tutorials.
Boot.asm calls
[code]
extern kernel_main
call kernel_main
[/code]
and kernel.c has
[code]
void kernel_main()
{
terminal_initialize();
/* Since there is no support for newlines in terminal_putchar yet, \n will
produce some VGA specific character instead. This is normal. */
terminal_writestring("Hello, kernel World!\n");
}
[/code]
I made a makefile:
[code]
bin/muagetdua.bin:
nasm -felf src/boot.asm -o bin/boot.o
gcc -c src/kernel.c -o bin/kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
gcc -T link.ld -o muagetdua.bin -ffreestanding -O2 -nostdlib bin/boot.o bin/kernel.o -lgcc
[/code]
[QUOTE=Armandur;41099400]I'm trying to implement a Finite State Machine in C++. What would be the best way to do this?[/QUOTE]
Have an enumeration listing all the states, and then have a struct that stores a token->state mapping (the semantics of which is if you are in the current state and [i]token[/i] is read, then you transition to the mapped state.
Then you can create a state-array of size max-state + 1, keep the current state index or a pointer to the current state somewhere and when transitioning you can simply update that variable.
Well, I'm not sure if it's the best way, but it certainly is the most straight forward.
[editline]19th June 2013[/editline]
[QUOTE=Map in a box;41100288]Trying to build my test OS, but it's failing:
//stuff[/QUOTE]
Looks right.
What's the output of objdump -t bin/kernel.o | grep kernel_main?
Sorry, you need to Log In to post a reply to this thread.