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 / storing a variable for later use

Author
Message
smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 11th Jul 2010 19:08 Edited at: 11th Jul 2010 19:23
hi using dbpro super noob. got a simple problem im sure holding up the progression of my program





I sure hope someon can maek heads or tail out of this.
basically this is part of a menu system ab# , bb# and cb# will increment 15 each time it loops. so if ab# and bb# meet there if it tells the sprite when to move down in my menue system.

the problem is i need to be able to store the number it was on when the mouse was clicked for later use

simple example not sure if the above is to clear


do
if mouseclick()=1
save varible "apple"!!
endif
for apples= 1 to 8
inc apples,1
next apple
loop
other wise when i try to call it and the for next loop is no longer running the variable has been reset to zero or null

i think i might need to use the dim command? have no idea how to do so is that correct??

A child's dream never dies.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 11th Jul 2010 22:01
Quote: "I sure hope someon can maek heads or tail out of this."

It would help if your code was indented. It increases the readability significantly. Like this:


Secondly, you are inside an if/endif where mouseclick() = 1 must be true, but you test for it again within it. There is no need for that second test. I removed the extra check to see if mousclick() = 1.

Next, a minor point, but I think is worth mentioning. You have a # after your variables, yet you are only using integers. Normally # is used after a variable to denote that it is a float. (I didn't change this).

I am trying to understand your question. You are using the variable primobj# to store some information about what the user has clicked on. Why can't you just use the value of primobj# for the info you need?


Kira Vakaan
15
Years of Service
User Offline
Joined: 1st Dec 2008
Location: MI, United States
Posted: 12th Jul 2010 06:41
I really recommend using floats only when floats are necessary. Due to issues with floating point precision, the integer you try to store in a float could be converted to a float that's some tiny fraction off, and then it wouldn't equal the integer anymore.

I'm trying to understand your question as well. What if you just tested the position of the mouse against some zones on the screen every time the user clicks? If the user clicks inside one of the zones, you store it in primobj.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 12th Jul 2010 07:24
I'm puzzled too as to the real question. If you're wanting to use that variable in a function it needs to be GLOBAL for it to retain it's value inside functions. DIM statements always make any array automatically GLOBAL too.

Run this to see it work then rem off "global test" to see the difference GLOBAL makes:


The same with a DIM statement instead:


smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 12th Jul 2010 18:15 Edited at: 12th Jul 2010 18:28
not sure i was very clear last night. apparently not.

gonna try better this time

this is not part of a menu system but imagine it is. same concept except that im going to need to know mabey 10 minutes down the line what item on my menue whas selected


the ab bb an cb are starting position on screen where i want my sprite to start at for highlighting selecting
as i move my mouse down the menue if its above of below 15 pixels from its revious point the sprite will move there
ab#=146
bb#=161
cb#=146
for cib =1 to 7

if mousey() >ab# and mousey() < bb#
`sprite will move there!! sprite 46,1143,[b]cb#[/b],33

all this is fine and good
now i got a for next loop here activated buy mouseclick
so when the mouse is clicked it will run the loop and cb# will
pick the current mouse position mousse starting pos =ab# or 146+ 15 x how many places it has moved so i can assign primobj# a value bases off the current cb# number.

all this works fine and i can print the variable cb# to the screen or even have it type 1-7 or select an item 1-7 ect or have it do something
however the problem is that if i want to call the variable cb# somewhere else in my program for use unless the mouseclick=1 and is over my menue ect the variable doesnt seem to retain its previous value only while its in the loop

want to be able to call primobj# with = cb# value

but it returns a 0 if its not inside the funstion im using

hope this is explanation enough sorry best i can do im very new to coding and how it all works

i did get it working buy throwing a function into my function so techincally its still in the loop but my question remains other than opening a file to write data to is there a way to store data and keep it from changing even if the value of the variable witch the data came from was changed


cant do it global i dont think grob grog it doesnt have a definite value its changing several times a loop
did try it though through primobj as a global didnt work oustide the function

and ty LBFN for the indent info ill use it form now on will meak it easier for my if statments i got thousands of them get lots alot as far as the float goes ive never had an issue using it but have had not using it so i just stick with it for now for what i do most it suites me

A child's dream never dies.
LBFN
17
Years of Service
User Offline
Joined: 7th Apr 2007
Location: USA
Posted: 12th Jul 2010 19:03
Quote: "however the problem is that if i want to call the variable cb# somewhere else in my program for use unless the mouseclick=1 and is over my menue ect the variable doesnt seem to retain its previous value only while its in the loop"


Grog Grueslayer told you the solution (with one minor edit):
Quote: "If you're wanting to use that variable in a function it needs to be GLOBAL for it to retain it's value inside functions."

The edit being that it will retain it's value OUTSIDE of the function. So, make whatever variable you want to use into a global variable, and no matter where it is in the program, it will retain it's value.


smerf
19
Years of Service
User Offline
Joined: 24th Feb 2005
Location: nm usa
Posted: 12th Jul 2010 21:22 Edited at: 12th Jul 2010 21:22
heck yeah thanks guys! sorry for before stuck the global in there dint work cause i didn't realize i hadn't saved my previous project and had the print/text set up wrong
gonna open a whole new world for me.




A child's dream never dies.

Login to post a reply

Server time is: 2024-09-28 20:19:30
Your offset time is: 2024-09-28 20:19:30