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.

Geek Culture / hate numbers in my code

Author
Message
DARKBASIC PRO 3d WORLD EDITOR
6
Years of Service
User Offline
Joined: 2nd May 2012
Location:
Posted: 26th May 2015 04:22
One of my buddies said this was unacceptable...
I dont know why but I hate a lot of unnecessary numbers... What do you think?

freeObj=FindFree("object",1,100000)
load object "bridge.dbo",freeObj
#constant Bridge freeObj
freeObj=FindFree("object",1,100000)
load object "building.dbo",freeObj
#constant Building freeObj


position object Bridge,10,10,10






end

function FindFree(item$,r1,r2)
select item$
case "object","image","sprite","sound","music","effect"
limit=100000
endcase
case default
limit=32
endcase
endselect
if r2>limit then r2=limit
if r1>limit then r1=limit:r2=limit


n=r1-1
do
inc n: if n>r2 then exit
if item$="object"
if object exist(n)=0 then value=n:exit
endif
if item$="image"
if image exist(n)=0 then value=n:exit
endif
loop

endfunction value





is this acceptable? I haven't tested it yet but this was a solution...

- Infinity is Simplicity -
Dark Java Dude 64
Community Leader
8
Years of Service
User Offline
Joined: 21st Sep 2010
Location: Neither here nor there nor anywhere
Posted: 26th May 2015 04:55
As for your code working, I have no idea. But as for the looks of code, I do prefer things to not have numbers (I think that's what you're getting at here). replacing things with constants looks a lot better IMO. I also don't really like numbers in variable names too much, although I do use them quite often. I am a rather "OCD" person, so my own code never looks good to me.
Seppuku Arts
Moderator
14
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 26th May 2015 09:27 Edited at: 26th May 2015 09:30
Been a while since I''ve used DB, but it is kinda structured to take advantage of object numbers, but using the free object function was a good idea.

However, I generally try to avoid using numbers in the main part of my code and instead declare them in variables, say if you wanted to change a value, you can do it where the variables are stored.

I would say structuring your code is beneficial, in fact there are some structuring principles out there. Given I am into web development these days, Model View Controller is the one I am cornered into. This basically means you're breaking your code down into three sections so they're independent of each other to make it more manageable. So with MVC, I have my model, which is my data, this might be where you put your variables and code that handles/interprets data. Then your controller, which handles in put, perhaps taking some information from the model if a request is made. Then the View is everything the user will see, can be where your media is too. This generally takes information from the controller.

This in theory could work in DBP, but never tried it as I wasn't't brilliantly structured when I used it, but may serve as inspiration if you're looking to structure your code.

Van B
Moderator
16
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 26th May 2015 09:57
I usually just store my object numbers in a typed variable, I mean if your gonna use a constant to refer to an object - why not a typed global, then you can have all the extra information you might need for each object as well.

Type _object
ID
collision
state
animation
Endtype

Global bridge as _object
bridge.ID=FindFree("object",1,100000
bridge.collision = 1
bridge.state=0
bridge.animation = 0

And of course the same type can be used to declare an array as well.

Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 27th May 2015 04:57
I tend to do it similar to VanB, though I tend to use typed arrays so that when it comes time to process updates, I can just iterate through the array and not worry too much about what specific object is being processed. when I need to get hold of a specific object I will use a get function to find it in the array

Matty H
10
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 27th May 2015 12:52 Edited at: 27th May 2015 12:53
I think id numbers are just a bad idea.

You can(and should) come up with systems to make them safer and reduce potential bugs but at the end of the day an id number is still just a number. Any part of your program can accidently access your resources and mess things up and it can be very difficult to track down why it's happening.

I don't think that was what the original question was about but I felt like saying it

Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 27th May 2015 17:37
the idea of an ID is handy for locating things, but using something that is likely to change when things are added or deleted such as object numbers, image numbers, array indexes can be problematic to try to keep all the ID in sync.

using some unique and independent identifier though such as an incremental record number, a time stamp, unique name, or some concatenation of thereof can make finding what your looking for easier.

Van B
Moderator
16
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 27th May 2015 18:13
Yeah, that method requires a bit more housekeeping, like clearing out or resetting object type arrays. It depends on how complicated things get, I mean take a particle engine for example... a particle engine could just be a whack of plains cloned from a source object which are created and never deleted, just recycled. That tends to be the way I work, some things get created and recycled like particles and bullets, but things like in-game object, enemies, those tend to be cleared out and re-populated (each level for example).

I tend to never ever mess with array indices, like adding them or deleting them or even using them to track the next free index - I always use a global counter for these sorts of typed array, and the array is always created at a size that I've specified that doesn't change. For me I need to stay in control of proceedings, otherwise I might as well play a game - I prefer to weigh up the implications of extending array sizes, maybe I'm just worried that things will go rogue due to a bug I don't notice. I never let my arrays off their leash

Kevin Picone
16
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Australia
Posted: 27th May 2015 20:01
Anything that removes the media allocation burden from the user can be useful. For me depends on the project, sometimes i'll use a raw index in a demo, but generally treat the media index as child of parent type that's been dynamically allocated. Just makes life easier when the program has to load external data sets, like loading a level in a game.

For external data, what i do i find useful is referencing everything via string name. So the resource is loaded into it's container and when something else whats to know where said resource is, we search it via name. String compares are bit slow, but storing hashes of the names gets rid of the major brute overhead.

Ortu
DBPro Master
11
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 28th May 2015 07:17 Edited at: 28th May 2015 07:19
For say images, (simpler than objects, no need to worry about clones/instances) here is how I handle them:



Seppuku Arts
Moderator
14
Years of Service
User Offline
Joined: 18th Aug 2004
Location: Cambridgeshire, England
Posted: 29th May 2015 21:33 Edited at: 29th May 2015 21:35
I thought for a bit of fun, I would open DBPro for the first time in years and thought I'd see how I might structure it as MVC.

Main.dba



Models:

CharacterModel.dba


GameModel.dba


Controllers


CharacterController.dba


GameController.dba



Views

GameView.dba



Of course, this is just a little experiment. I theory, one could have the "Models" use something like DarkData for all the info, then LUA scripting for the Views, which will be all the levels and stuff.

The above could be improved, just been so long since I've used DBPro.

But the principle here would be: all gameplay logic is handled by the controller, how it's presented is handled by the view, the data used it handled by the model, there's a separate area for "helpers" to help your coding and you have the "main" code for initiating everything. The idea is for the model and controller to chat to each other, and also, the view and controller to chat.

Seems long winded for just 2 cubes and rudimentary camera control, but of course, the idea is for a structure that can be scaled.

DARKBASIC PRO 3d WORLD EDITOR
6
Years of Service
User Offline
Joined: 2nd May 2012
Location:
Posted: 20th Jun 2015 21:30
This thread actually helped me a lot more than I thought.. wow


- Infinity is Simplicity -
Clonkex
Forum Vice President
8
Years of Service
User Offline
Joined: 20th May 2010
Location: Northern Tablelands, NSW, Australia
Posted: 21st Jun 2015 08:45
When I first started using DBPro my code was full of hard-coded numbers and variables ending in numbers because I didn't know about arrays.

These days, I generally, I have a class/type like the following (in DBPro-code):



That way if I delete an element (in this case, a room) from the array, I don't change the indices of the remaining elements since all I do is change "exist" to false. The disadvantage of this method is that it can require more memory (i.e. if lots of elements get marked as "exist=false" because they're then not being used but still take up memory) and that it requires more processing time to add new elements because you have to (potentially) scan over the entire array to find the first empty ("exist==false") slot. For me, though, the advantages outway the disadvantages many times over simply because you can use the array indices to directly access other elements of the array without fear of the indices changing when an element is removed or added.

Login to post a reply

Server time is: 2019-03-27 02:48:53
Your offset time is: 2019-03-27 02:48:53