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 / Declaration error?

Author
Message
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 7th Nov 2011 00:05 Edited at: 7th Nov 2011 00:13
Ok, so whenever I compile this, I get "User function 'GetFreeSprite' must return a value if used in an expression at line 58."
And I really don't know why.
Also I'm not sure if the way I use get image will just get the top 3 in a sprite sheet, each image is 40 x 64 so I assumed that would work but I'm not sure.



I also attached my sprite sheet.

P.S. like meh new signature?

Oh yeah, and if you're going to ask why I'm even doing this, It's because I want to learn how to do sidescrollers (Mainly learn how to detect which piece of terrain the player is currently on, and detect its y + its height (Which would be the very top part of the terrain (The part the player walks on)) and then find out the players y, and put them together * a certain amount of pixels I want it to be and then execute that through a function. (Seems pretty complicated to me. If you can explain it now it will save time in the future )

-------------------------------------------------------------

Attachments

Login to view attachments
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 7th Nov 2011 02:06 Edited at: 7th Nov 2011 02:06
Quote: "And I really don't know why."

Functions perform processes but can also return values.

Take this for example:

This calls a function but the function doesn't return a value.

Now look at this:

Because 'a' is stated right after the endfunction keyword, the function will return that value.

So what you need to do with your functions is to end them with the variable you want to return stated directly after the 'endfunction' keyword.

Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 7th Nov 2011 03:07 Edited at: 7th Nov 2011 03:33
But It's a global, shouldn't it automatically return?

Also, again, will this work for the image I provided?



-------------------------------------------------------------
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 7th Nov 2011 03:19
Quote: "But It's a global, shouldn't it automatically return?"

Nope. The function hasn't been told what to return so it won't return anything but because it's a global, you can access the global where ever you need to just by name but it won't be done by the function unless you make it the variable that, that function returns.

I hope that makes sense.

Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 7th Nov 2011 03:33
Yeah it does, also I edited my other post :/

-------------------------------------------------------------
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Nov 2011 05:06
It's probably thinking you want to return a value because you have a 4 after ENDFUNCTION.



Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 7th Nov 2011 05:09
No that was an accident, I deleted that and it still didn't work, should've mentioned the spelling error in the post lol.

-------------------------------------------------------------
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Nov 2011 05:27
Oh you do want to return something... ok.



Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 7th Nov 2011 06:27
Hmm, I just realized I don't have to do all that calculating. (For the sidescrolling part on post) I can just use a timer() and set an amount of time the player stays airbourne before gravity grabs hold. Then I should have them slowly come to a stop and fall down with increasing speeds.

-------------------------------------------------------------
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 7th Nov 2011 17:23 Edited at: 7th Nov 2011 17:24
I have a question, how do I set up a variable and increase its name, for example:



that only saves all 3 as "shadow" if I want it to be saved as "shadow1", "shadow2", "shadow3" ect would I just do shadow=shadow+"b" or do I have to do something fancy?

also, I've asked this next question multiple times already, but will that work anyways, im grabbing 3 images and their each 40x64 and all on the same row.

-------------------------------------------------------------
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 7th Nov 2011 18:02 Edited at: 7th Nov 2011 20:09
Quote: "how do I set up a variable and increase its name"


You add the variable to the filename by converting it to a string with the STR$() command. The following code snip uses a FOR/NEXT loop that loads Shadow1.png, Shadow2.png, and Shadow3.png. The variable "t" in the FOR/NEXT loop is added to the string after the Shadow and ".png" is added after that.



Quote: "also, I've asked this next question multiple times already, but will that work anyways, im grabbing 3 images and their each 40x64 and all on the same row."


Actually all that'll do is error out because your grabbing image number 0 (shadow=0) and even if you used b instead of shadow you won't see anything because you didn't use PASTE IMAGE. And it's not grabbing what you think it'll grab. To check what GET IMAGE would grab you can use the BOX command instead to get a visual representation of what the code is doing.



To grab multiple frames from one image it's best to use LOAD BITMAP to get the image on the screen without using LOAD IMAGE to load it and PASTE IMAGE to put it on the screen.



Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 8th Nov 2011 01:58 Edited at: 8th Nov 2011 02:00
That helps, but I want it to have a name, using 1-3 isn't the best way to do it, I can easily forget numbers like that. Though I do get the point. I'll just peice it together.

EDIT: Ok, would this work?



-------------------------------------------------------------
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 8th Nov 2011 04:04
GAH! I'm completely confusing myself, I obviously need alot of practice with animations, the most confusing part is storing them all, I have to store 3 images, and 3 sprites, and its all confusing.

I feel like someone needs to explain my code to me. So I'm going to ask just that



P.S. I have the shadows.png up at the top, and the BG.bmp is attached here.

-------------------------------------------------------------

Attachments

Login to view attachments
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 8th Nov 2011 08:18 Edited at: 8th Nov 2011 08:33
If you are using multiple frames for a shadow, why not:



Note: You can also use a regular single sprite for a shadow and resize it when the player jumps but that will require some maths.

Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 8th Nov 2011 08:21 Edited at: 8th Nov 2011 08:28
I guess I could just have an array for the image numbers and swap out, I thought it might be better to use the types, but I guess this will work out too.

EDIT: This should work, but on PlayerInput() im getting "Function declaration 'PlayerInput' must be outside any program nests at line 129."



-------------------------------------------------------------
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 8th Nov 2011 08:36 Edited at: 8th Nov 2011 08:38
Quote: " This should work, but on PlayerInput() im getting "Function declaration 'PlayerInput' must be outside any program nests at line 129." "


You didn't close your for..next loop a few lines above.

EDIT: 30 lines to be precise.

Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 9th Nov 2011 02:09 Edited at: 9th Nov 2011 06:02
Ugh *Face palms* should have noticed that. Well now its saying 'variable 'player' doesnt exist' in my main loop (Its all just for testing ATM)

But I don't see why it says that, I declare everything, all the returns are in place (I tried to check little things like that. I had some other problems, but I fixed those on my own. Anyway just wondering. Also, would my current jumping thing in PlayerInput() work? I haven't tested it yet, I've also never made one, so I'll need help as its new to me.



EDIT: Ok, apparently I just can't declare something as a type in a subroutine, wierd.

EDIT TWO: Ok, for whoever was the one who helped me with the "get image" It doesnt work, it grabs like one pixel of each frame, so all I see is a single pixel following my mouse around.



I think I'm going to try what I originally did.

EDIT THREE: Yeah... still didn't work, can someone visually explain how I'm messing up lol. It would help alot.



-------------------------------------------------------------
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 9th Nov 2011 08:13
Quote: " Yeah... still didn't work, can someone visually explain how I'm messing up lol. It would help alot. "


Here is what you are doing:


You load the image to bitmap 1. You then grab the following images at these coordinates (translated from your code):

shadow(1) = x40, y40 TO x64, y64
shadow(2) = x80, y80 TO x128, y128
shadow(3) = x120, y120 TO x192, y192

In other word, you are moving diagonally across your surface AND overlapping. So you are grabbing something waaaaay out.

Let's assume you have 3 images of 32x32 tiles horizontally in a bitmap. Your code should read:



Now we'll have:
shadow(1) = x0, y0 TO x32, y32
shadow(2) = x32, y0 TO x64, y32
shadow(3) = x64, y0 TO x196, y32

Standby for visual explanation. A warning in advance: I cannot draw to save my life.

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 9th Nov 2011 08:28
Okay, here's the visual for what I just said. I suggest you magnify it in paint and press Ctrl + G to enable the grid view to give you a pixel-ish view. It's not very accurate, I know but it's just a more or less. Apologies for the bad drawing, you were warned.

Attachments

Login to view attachments
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 9th Nov 2011 16:39
Yeah, that helps alot.

But for some reason now its having the background follow my mouse and not the shadow images O.O

Do you think its grabbing from the background image instead or something?



-------------------------------------------------------------
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 9th Nov 2011 18:01 Edited at: 9th Nov 2011 18:03
Quote: "Yeah... still didn't work, can someone visually explain how I'm messing up lol. It would help alot."


I know NonZero showed you later but if you ran the second code snip of my last message you would of seen a visual of what it was grabbing back on the 7th.

Quote: "But for some reason now its having the background follow my mouse and not the shadow images O.O

Do you think its grabbing from the background image instead or something?"


It's because you do this for the background image:


If you didn't catch it... your checking for a free sprite for the background image. It's nice to be able to have an automatic system like this for big projects where it's too many images/sprites to track. But for small games it's best to just create a list of all the image numbers and sprite numbers you use for the game in a REMSTART/REMEND.



Also it's a good idea to have some text to show whats going on with the variables in the program.

Your code with TEXT commands (without other changes):


Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 9th Nov 2011 18:21 Edited at: 9th Nov 2011 18:23
Ok, I just change GetFreeSprite to GetFreeImage and it works, but the code to grab it is apparently extremely messed up, it only grabbed the top half and turned transparency white, and the black transparent, really weird, plus the animation thing doesnt work, also probably because of get image but I'm not sure.

EDIT: Oh wait, never mind about only grabbing the top half, I just put in the wrong coords, but it still is really glitchy.

-------------------------------------------------------------
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 9th Nov 2011 20:07
In Darkbasic the default transparency color is black. You can change the transparency by using SET IMAGE COLORKEY.



Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 10th Nov 2011 01:53
Ohhh! So thats how people have colors that are transparent in-game.

Also the background isn't pasting

(Though that I can figure out myself)

-------------------------------------------------------------
Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 10th Nov 2011 08:27 Edited at: 10th Nov 2011 16:28
That shadow.png file is using a real transparency but because you use LOAD BITMAP and GET IMAGE without turning on the ability to grab transparencies so it doesn't keep the natural transparency.

.pngs allow us to use one or several different transparencies. To show you what it can do I attached an edited shadows.png file (now called shadows2.png) with a shadow that has a 60% opacity level so it's semi-transparent. The following code loads the image so you can see the .pngs natural transparent background and the semi-transparency for the character. It also uses CREATE ANIMATED SPRITE which is a lot easier to work with than what you've been doing with GET IMAGE to grab individual frames. It is good to know how to do it the old way first but CREATE ANIMATED SPRITE does all the work for you and you use PLAY SPRITE to animate at the exact delay you wish (1000 = 1 second).



Attachments

Login to view attachments
Darkzombies
13
Years of Service
User Offline
Joined: 25th Dec 2010
Location: In multiple tabs, most likely youtube.
Posted: 10th Nov 2011 17:50
Well yeah, I know that, It just sort of feels like cheating, having commands that do all the work for you, plus I plan on learning other languages after this, so I need to master the old ways.

-------------------------------------------------------------

Login to post a reply

Server time is: 2024-11-22 12:14:12
Your offset time is: 2024-11-22 12:14:12