Any good examples of structures and lists
-
- New User
- Posts: 9
- Joined: Fri Nov 27, 2015 4:53 pm
Any good examples of structures and lists
I am looking for any good examples of using structures, storing them in lists, adding and removing from the list.
I think I have structures down but not lists and not using them together.
Thank you
I think I have structures down but not lists and not using them together.
Thank you
Re: Any good examples of structures and lists
Mby it helps:
Code: Select all
EnableExplicit
Structure DUMMY_STRUCT
name.s
EndStructure
Global NewList dummy.DUMMY_STRUCT();<- list with a struct
Global *entry
AddElement(dummy());<- add entry
*entry = dummy();<- store the ptr of the created struct (for quick access)
dummy()\name = "Hello! 1"
AddElement(dummy());<- add another entry
dummy()\name = "Hello! 2"
ForEach dummy();<- show what has been stored in the list
Debug dummy()\name
Next
;we can only delete the currently active element!
;to delete the 1st entry we can use the ptr stored before (*entry)
ChangeCurrentElement(dummy(),*entry);<- select entry 1
DeleteElement(dummy());<- delete entry
ForEach dummy()
Debug dummy()\name
Next
;to get rid of all entries
ClearList(dummy())
;to free the allocated memory
FreeList(dummy())
End
Re: Any good examples of structures and lists
You could also assign the structure to your pointer:
You can also use FirstElement() (and LastElement()) instead of ChangeCurrentElement().
Code: Select all
Global *entry.DUMMY_STRUCTURE
[...]
Debug *entry\name
-
- New User
- Posts: 9
- Joined: Fri Nov 27, 2015 4:53 pm
Re: Any good examples of structures and lists
";we can only delete the currently active element!"
assuming i'm moving images and when they move off the screen I can do it like,
cycle thru a list with foreach and checking say boundries of the screen and if the current object is out of boundry and I want to get rid of it I can just DeleteElement(dummy()) because it will be the current element in the list?
assuming i'm moving images and when they move off the screen I can do it like,
cycle thru a list with foreach and checking say boundries of the screen and if the current object is out of boundry and I want to get rid of it I can just DeleteElement(dummy()) because it will be the current element in the list?
Re: Any good examples of structures and lists
Yes.jchase1970 wrote:";we can only delete the currently active element!"
assuming i'm moving images and when they move off the screen I can do it like,
cycle thru a list with foreach and checking say boundries of the screen and if the current object is out of boundry and I want to get rid of it I can just DeleteElement(dummy()) because it will be the current element in the list?
- Andre
- PureBasic Team
- Posts: 2056
- Joined: Fri Apr 25, 2003 6:14 pm
- Location: Germany (Saxony, Deutscheinsiedel)
- Contact:
Re: Any good examples of structures and lists
Maybe this chapter from the "UserGuide" (part of the PB refererence manual) gives also some ideas:
https://www.purebasic.com/documentation ... _data.html
https://www.purebasic.com/documentation ... _data.html
-
- New User
- Posts: 9
- Joined: Fri Nov 27, 2015 4:53 pm
Re: Any good examples of structures and lists
Thank you for your help I was able to get it to work and understand a little more. I made a little snow falling effect to try it out
I do have 2 questions that I came across as I did this and I am coming from using Blitzmax so
question 1 is there a random float function? I used random(100)/100 to get a random float because I could not find another random function.
question 2 is there a simple syntax to preform operation to variable self? In Blitzmax if I want to do a=a+5 for example I can write a:+5
Code: Select all
If InitSprite() = 0 Or InitKeyboard() = 0
MessageRequester("Error", "Can't open the sprite system", 0)
End
EndIf
Global screenwidth=1280
Global screenheight=1024
Global gravity.f=0.5
Global wind.f
Structure snow
x.f
y.f
weight.f
EndStructure
Global NewList snowflakes.snow()
Procedure CreateSnow()
AddElement(snowflakes())
snowflakes()\x = Random(screenwidth)
snowflakes()\y = 0
snowflakes()\weight = Random(100)/100
EndProcedure
Procedure updatesnow()
ForEach snowflakes()
snowflakes()\x=snowflakes()\x+wind*snowflakes()\weight
If snowflakes()\x<0
snowflakes()\x=screenwidth
ElseIf snowflakes()\x>ScreenWidth()
snowflakes()\x=0
EndIf
snowflakes()\y=snowflakes()\y+gravity+snowflakes()\weight
If snowflakes()\y<screenheight
Circle(snowflakes()\x,snowflakes()\y,snowflakes()\weight*3,RGB(255,255,255))
ElseIf snowflakes()\y>=screenheight
DeleteElement(snowflakes())
EndIf
Next
EndProcedure
If OpenScreen(screenwidth, screenheight, 32, "My Screen")
Repeat
wind=wind+0.5-(Random(100)/100)
If wind<-4
wind=wind+0.5
ElseIf wind>4
wind=wind-0.5
EndIf
ClearScreen(RGB(0,0,0))
createsnow()
StartDrawing(ScreenOutput())
updatesnow()
StopDrawing()
FlipBuffers()
ExamineKeyboard()
Until Quit Or KeyboardPushed(#PB_Key_Escape)
Else
MessageRequester("Error", "Can't open windowed screen!", 0)
EndIf
question 1 is there a random float function? I used random(100)/100 to get a random float because I could not find another random function.
question 2 is there a simple syntax to preform operation to variable self? In Blitzmax if I want to do a=a+5 for example I can write a:+5
Last edited by jchase1970 on Wed Aug 28, 2019 3:44 am, edited 1 time in total.
Re: Any good examples of structures and lists
It's even simpler than in BlitzMax. You just use the operators on the variable:jchase1970 wrote:question 2 is there a simple syntax to preform operation to variable self? In Blitzmax if I want to do a=a+5 for example I can write a:+5/
Code: Select all
Define a = 10
a + 10
a - 5
a * 10
a / 5
Re: Any good examples of structures and lists
And no, there's no native RandomFloat() function.
Even better
Code: Select all
Random(2147483647)/2147483647
Re: Any good examples of structures and lists
Update
Code: Select all
Macro RandomFloat(Steps)
(1.0 * Random(Steps) / Steps#.0)
EndMacro
For i = 1 To 100
Debug StrF(RandomFloat(1000), 3)
Next
Debug "**********"
For i = 1 To 100
Debug StrF(RandomFloat(5), 3)
Next
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Any good examples of structures and lists
Nice Macro @mk-soft thx