creating an instance of an object

BlitzMax Forums/BlitzMax Programming/creating an instance of an object

Cruis.In(Posted 2012) [#1]
Do you still use the 'new' method to create a new instance of an object?

I am having a problem with old code where a function in another type, is not finding an object to modify one of its attributes.

in this case the object is the player entity, and the attribute i want the other function to modify is speed. Used to work AS IS before. i changed nothing now it can't find the entity.


Zethrax(Posted 2012) [#2]
Yes you still use the New command to create a new object instance. You'll need to post some code if you want more detailed advice.

What error message are you getting?


Cruis.In(Posted 2012) [#3]
not sure, it seemed to work itself out. maybe the program automatically updated syntax which was outdated.

I have another question: What commands should i investigate for implementing a timed event?

I want to execute a block of code a certain amount of seconds after a button press.

Example

If tab is pressed
wait three seconds
deploy parachute, play a sound

so you press tab, a parachute comes out three seconds after you press tab, and also plays a sound at the same time.


Zethrax(Posted 2012) [#4]
' Setup the number of millisecond to delay for.
' This code could go in your Type declaration, in some sort of Create function, etc.

delay_amount = 3000

---

' Set the delay timeout when whatever trigger event that marks the start of the delay period occurs (a button press or whatever).

delay_timeout = MilliSecs() + delay_amount

---

' Check if the delay timeout has reached its timeout point and take action if it has.
' This should go in your update routine for whatever object, etc, it is that the timeout is used for.
' Note that this code doesn't care if MilliSecs() has wrapped around from a positive value to a negative one, or from negative to positive. It's a relative check. As long as the delay_amount you've set is less than about 24 days (or the milliseconds equivalent) then you're not going to have any problems at all with the MilliSecs() value wrapping around.

If ( MilliSecs() - delay_timeout ) > 0 Then DoAction()


Cruis.In(Posted 2012) [#5]
thanks, that example helps.

I have one more question.

What would I have to look into for zooming in towards the centre of the screen where my entity is?

its a top down 2d view, of a space ship at the centre.

I've experimented with opengl and translatef etc. Can't seem to get it to work as I have no understanding of the function parameters, and their effects.

I want to tie the zoom level to mousez()

so mouseZ() will be the variable I zoom at.


Zethrax(Posted 2012) [#6]
For a game using 3D rendering (3D or 2 1/2 D) you'd move the camera closer to the point you want to zoom in on. Assuming the camera is aligned to face into the axis direction that represents 'forward' then you would move the camera in that 'forward' direction. If the camera is aligned at an arbitrary direction then you would move it forward in its local space. You'd need to be aware of the min camera clipping distance and avoid getting close enough for it to begin clipping the geometry.

I haven't used Max2D but I'd assume the way to do it there would be to have a function that applies a multiplier to the scale of all the 2D objects (including the distances between them, speed attributes, etc). This would be a fairly involved process.

For OpenGL I have no clue. You'd probably be better off making your game in Max2D or MiniB3D (possibly using 2D in 3D techniques).

For getting mouse input I'd use MouseZSpeed() with an accumulator variable (so you can clip the values involved and apply a multiplier).

' Declare the 'zoom' accumulator variable.
Global zoom:Float

' Declare the min/max zoom constants.
' You could also use variables for this if the values need to be able to be changed (to suit user preferences, etc).
' The values used are for example purposes only. Use whatever values work best for your game.
Const MIN_ZOOM:Float = 0.0
Const MAX_ZOOM:Float = 10.0

' Declare the 'zoom' multiplier variable. You could also use a constant for this if it doesn't need to be able to be changed (to suit user preferences, etc).
' The value used will cause each MouseZSpeed() unit to be worth one-tenth of a world unit. Use whatever value works best for your game.
Global zoom_multiplier:Float = 0.1

---

' Accumulate the mouse z speed and apply a multiplier to control the value added.
zoom :+ ( MouseZSpeed() * zoom_multiplier )

' Need to call MouseZSpeed() here to reset it.
MouseZSpeed()

' Clip the zoom to be within the range of MIN_ZOOM and MAX_ZOOM.
If zoom < MIN_ZOOM
zoom = MIN_ZOOM
Else
If zoom > MAX_ZOOM Then zoom = MAX_ZOOM
EndIf


' Code to apply the zoom value goes here.

Last edited 2012


Cruis.In(Posted 2012) [#7]
i did this six years ago.

https://www.dropbox.com/sh/12ikjlfz8iurt48/GcIY6bAdHW/zoom.rar

if you download that, it contains, a 500kb exe file, graphics folder with two ship graphics.

The file is compiled so I cannot see what/how I did it. I suspect I was using opengl translatef

I am re-learning programming/blitzmax. I don't expect to pick it up again so fast after 6 years but that's not the point. As long as I stick with it, I can't be any worse than the best I got to six years ago, cause I was able to get that far.

As for the zoom. I wrote a function for that in the code base.

Scaling everything yeh is out of the question. As you'd have to apply the scale to everything that is dawn. And the distance would appear to change on screen once scales get bigger. Which isn't a true ZOOM in but rather making images larger.


Cruis.In(Posted 2012) [#8]
getting an issue on setting the delay time out.

works fine on the execute part when testing.


' Set the delay timeout when whatever trigger event that marks the start of the delay period occurs (a button press or whatever).

got this variable set on a key press.

delay_timeout = MilliSecs() + delay_amount

but the event im waiting on activates immediately


Cruis.In(Posted 2012) [#9]
field: delay_amount = 5000 (5 seconds)


if keyhit tab
delay_timeout = MilliSecs() + delay_amount
end if

function()
deploy_parachute()
deploy your parachute
end function


In the main loop:

'after 5 seconds this should deploy the parachute
if MilliSecs() - delay_timeout ) > 0
deploy_parachute()
end if


Cruis.In(Posted 2012) [#10]
if you don't declare the values for "delay_amount" and "delay_timeout" up front then the condition check on if MilliSecs() - delay_timeout ) > 0 is always true and the code u want delayed executes immediately.

if you declare an amount, the code executes after that amount of time has passed. Great right? However, that means it executes on the passage of time alone, and no input. I tried to default the delay amount to be 0, and let the key press set it at 3000, so the code doesn't execute until the countdown.

but as said above, if it doesn't have a value it executes right away because the condition is always true for if MilliSecs() - delay_timeout ) > 0


Cruis.In(Posted 2012) [#11]
Ok in order to initialize the variables at 0, but stop it from executing automatically, I had to put another condition in the if statement using and

if MilliSecs() - delay_timeout ) > 0 and XXX is true

so it didn't execute on initialization, but only after the key press, which makes XXX true.