Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Creating units in a RTS game

Author
Message
Nameless
20
Years of Service
User Offline
Joined: 11th Apr 2004
Location: U.S.A.
Posted: 15th May 2006 02:45
I'm making a rather basic RTS game. It has been going fine but I have a problem with deploying units.

Here is the code I'm using for it.



b is the barracks, where you recruit units, and a is the soldiers you recruit there. Barracksunitbuild# is the variable for when you have selected to build a unit. Barracksproduce# is the how long it takes for your unit to actually be built, when it reaches 100 the unit is built. Unitexist# show if the unit has been built yet. For testing purposes I just decided to have a unit change color, symbolizing that it has been created. The problem is the first time you buy a unit it works. However, the next time 3 or 4 units are built instead of one. This happens every time I try it after the first. I hope I explained it well enough, I'm hoping someone can point out any problems in my code or offer a suggestion as to a better way to build units. Thanks in advance.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th May 2006 05:26 Edited at: 15th May 2006 07:43
Well first of all, your code cant compile anyway, you have an ENDIF outside of the FOR statements where the originating IF statement was created. tsk tsk.

I'm going to rewrite the whole thing from scratch for you and hopefully you'll see how I structured the code is easier to work with.

[rant]
Getting tired of constants referencing the memory address of whatever is assigned to it rather than the value.
[/rant]


Ok, take a look at this code. I even made a demo to show the features.
All you really have to do at this point is call:

addToBuildQueue(id)

where 'id' refers to the building. Everything else from this point on is handled automatically now. The last function in the code handles what happens once the building has finished processing (recruiting) a unit. Perhaps you load an object or something. At the very least, you'll have to add the new unit to an array, like soldiers(). My RTS tutorial covers basically everything on handling an array of units or playable characters.





Mr X
19
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 15th May 2006 16:22
If you are trying to make an RTS, there is an tutorial in the Code Snippets. Dont know if youve read it, but here is the link. http://forum.thegamecreators.com/?m=forum_view&t=44248&b=6. Goodluck.
Nameless
20
Years of Service
User Offline
Joined: 11th Apr 2004
Location: U.S.A.
Posted: 15th May 2006 23:01
Thnbaks for the replies.

Phaelax:
Thanks for the code but it's a litle more complicated then I expected. My being a newb I was wondering if you could answer some questions I have about certain aspects of it.


I don't really see what this code does.


Same with this.


I don't really understand how this creates the buildings. I thought they alreasy were created.

If you or anyone else could help me understand this code I would truly appreciate it.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 16th May 2006 09:07
Question 1
a constant is a variable that never changes. An in IF statement, any value greater than 0 is treated as true, 0 is false. Most languages, like Java which I use mostly, you actually type true and false. It makes things easier to read.

if (thing = 1) then do something

Can thing only be 0 or 1? Is it a boolean? Or can thing equal any number? If it was written like this:

if (thing = TRUE) then do something

Then its easier to see what its value means.


Question 2
Using that function, it creates a new UDT (user-defined type) of type "BuildingStatsObject" based on the parameters you enter for the function. It adds the UDT to the array and returns the array index of where that UDT was added at. That index is then assigned to a global (accessible to all functions) variable. So whenever we need to reference that specific type of UDT, we don't need to remember the array index number, we can simply type the variable which we assigned that value to.

It's first assigned to a temp variable because it was giving me problems when I tried to assign it directly to a global.

When you want to look up the stats of the barracks building, is it easier to type "1" and remember that "1" means barracks, or is it easier to just type "BARRACKS_ID"?


Question 3
Another function, basically does the same thing as the other one I just described. Pass in the parameters, create a UDT (only instead its a Building tpye this time) then add that UDT to the array.
The buildings weren't created yet, only the stats. I'll explain the difference.

The UDT called "BuildingStatsObject" stores values that are the same for all Buildings.

If you have 3 barracks buildings in your base, all 3 share some of the same values no matter what. First of all, they're all called "barracks" so the name is shared. Second, they all produce units in the same amount of time. And thirdly, all barracks buildings have the same amount of maximum hitpoints.

Now, the UDT called "Building" is what stores the individual properties of each of the 3 barracks. Each building has its own hitpoints, which is different from maximum hitpoint.

Barracks 1 hitpoints - 53/100
Barracks 2 hitpoints - 89/100

100 = maximum hitpoints allowed for that building. Lets say you upgrade the armor around a building, you only have to change one value. The 53 and 89 are the buildings' individual hitpoints.

Other individual properties include knowing if the building is currently creating a new unit, and if so then it also keeps track of how far along the build process is.

side note
For the unit builds, I based all the times on the timer(). This way, a soldier will be created in 2seconds on any machine. Using your method shown in your code is dependant on the user's processor speed. Let's say 2 people are playing against each other, one person has a 7GHz machine while the other person has a 1GHz machine. The person with the faster machine would be able to create 10 soldiers in the time it takes the second person to create 1. By using the timer() we prevent this from happening.

@Mr X,
That's actually my tutorial.


Mr X
19
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 16th May 2006 16:33
I didnt say otherwise (but I didnt think of it either). Its a nice tutorial, thats why I set up the link.
Nameless
20
Years of Service
User Offline
Joined: 11th Apr 2004
Location: U.S.A.
Posted: 17th May 2006 00:08
@Phaelax:
Thanks I wasn't expecting an answer so in-depth. That reaslly helped me understand it a bit more. However if you would be kind enough will you suffer through several more newb questions I have?



Why is the ranfe of the array 0? If there are going to be multible buildings shouldn't it be more?



What is the significance of this code?



Not sure whta this line of code does either.

That's it for now but I really haven't gotten to look at some of the functions and considering my lack of experience with them I will probably have some questions there aswell. Thanks, again.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th May 2006 01:00
I just love rts games, even though most ppl could probably kick my butt at them. I used to play against my brother in red alert, and lost every time.

Question 1
ranfe? I'm guessing you mean size?
Using the array commands in DBP, we can grow or shrink the array size as needed. If you look in the functions that add a UDT to the arrays, you'll see the commands.

array insert at bottom array()

if array() was defined with 0 elements, that command would at 1 to it. It like now saying array(1). This command adds a new blank item to the array at the end of it. So if I started with 10 elements, the newly added element would be at position 11.

array insert at top array()

Same thing, only this adds the new element at the beginning and so it is accessed at array(1). All other elements have been shifted up 1. So the element that was previously at 4, is now located at 5.

I've used these commands to create a queue(pronounced "Q"). A queue is like a line of people. First person in the line is the first person out, and so on. So whenever you click a building to start producing another unit, its added to the queue which maintains the order of what to build next. When all items in this queue have completed, the array size is back at 0 because it is empty.


Question 2
That code just draws those boxes at the top of the screen to indicate the building progress. Has nothing to do with the functionality of the code. But you can look in the function to see how I determine the current build progress.

The first time I call those functions, 3 times, its just display the 3 dark colored boxes, or background, of the progress meters.

Question 3
Parameters:
X = b*150+50
Y = 10
time at which this building started the current build process
the current time
how many milliseconds it takes to build the unit, might be easier to see broken down like this:


index = building(b).stats
index will now equal either "1" for BARRACKS_ID or "2" for FACTORY_ID

buildingStats(index).buildTime
get the corresponding stats UDT and grab its "buildTime" variable


Tinkergirl
21
Years of Service
User Offline
Joined: 1st Jul 2003
Location: United Kingdom
Posted: 17th May 2006 16:05
@Phaelax: You're being super generous/kind/helpful here, as always - but I wondered if you're aware that creating an array with size (0) actually makes it with one entry in it? It's bitten me in the past, and the (horribly nasty nasty bodge-tastic) way of creating an empty one is to create it like so:



Often, it doesn't need to be 'really' empty (especially if you're definitely adding entries later) but just in case you didn't know. It's evil.
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 17th May 2006 23:44
Yea its given me issues before, but as long as I dont try to write or read at the o indice it should be fine. Though I never knew you could use -1 in the declaration.


Nameless
20
Years of Service
User Offline
Joined: 11th Apr 2004
Location: U.S.A.
Posted: 19th May 2006 02:13
Thanks for all you help Phaelex. I think I'm going to start my game over and try to incoroporate some of the stuff you explained to me in it.

P.S. Nice tutorial.
Renegade Andy
20
Years of Service
User Offline
Joined: 16th Oct 2004
Location: Scotland
Posted: 20th May 2006 21:16
how is this project going? I wouldnt mind discussing it with you in MSN, add - renegadeandy@hotmail.com

I would like to help out here.

Thanks

Andy

NO KEYBOARD DETECED - PRESS F11 TO CONTINUE - FOOKED BIOS
Nameless
20
Years of Service
User Offline
Joined: 11th Apr 2004
Location: U.S.A.
Posted: 21st May 2006 00:28
Added you to MSN.
Renegade Andy
20
Years of Service
User Offline
Joined: 16th Oct 2004
Location: Scotland
Posted: 21st May 2006 02:00
excellent thanks mate -

NO KEYBOARD DETECED - PRESS F11 TO CONTINUE - FOOKED BIOS
Phaelax
DBPro Master
21
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 22nd May 2006 10:31
Though I'd let you know, due to this thread and the sudden responses to my tutorial, I'll be writing another chapter in it over the next week. I'll also be doing a few bug fixes and optimizations, most recently in the pathfinding which should increase overall performance.

"Using Unix is the computing equivalent of listening only to music by David Cassidy" - Rob Pike
Renegade Andy
20
Years of Service
User Offline
Joined: 16th Oct 2004
Location: Scotland
Posted: 22nd May 2006 13:03
Yes! Good man! Im very definitiely looking forward to this.

TY for the e-mail also.

NO KEYBOARD DETECED - PRESS F11 TO CONTINUE - FOOKED BIOS
Mr X
19
Years of Service
User Offline
Joined: 25th Sep 2005
Location: Universe, milkyway, sol-system, Earth...
Posted: 22nd May 2006 17:19 Edited at: 22nd May 2006 17:19
Another chapter? Cool . Im looking forward for it.
Renegade Andy
20
Years of Service
User Offline
Joined: 16th Oct 2004
Location: Scotland
Posted: 24th May 2006 17:03
Does anyone know if its been written yet!

NO KEYBOARD DETECED - PRESS F11 TO CONTINUE - FOOKED BIOS

Login to post a reply

Server time is: 2024-11-26 19:58:50
Your offset time is: 2024-11-26 19:58:50