Page 1 of 1

[2DGame] AddMapElement not works, crached!

Posted: Thu Jun 28, 2018 9:23 am
by JamieVanCadsand
Hey Programmers and Game Developers...

I get an simple game programmed, called to add an element (an map) for create an text with the drawing 2d functions.
Just i try to write an script to add an map ellement [NewMap Text.s(), ...]. I try to do an test to choote bullet, called
to do an simple test with draw test you must press the 'space' key pushed to create them, just if the text is > 480 pixels
under the screen (YPos + 1), than the text must be deleted [DeleteElement(Text(), "B")] and if you press 'space' again,
my means is to create the element map again yet...

So this is my code, writted in pure basic 5.x yet:

Code: Select all

Global XPos.i = 320
Global YPos.i = 0

NewMap Text.s()
Text("1") = "B"

If InitSprite() = 0 Or InitKeyboard() = 0 Or OpenScreen(640, 480, 32, "Add Element Text Test") = 0
  MessageRequester("Fatal Error", "Failed to initialize", #PB_MessageRequester_Error)
  End
EndIf

Repeat
  
  If Speed & 1
    YPos + 1
  EndIf
  
  StartDrawing(ScreenOutput())
  Repeat
    DrawText(XPos, YPos, Text("1"), RGB(255, 255, 255), DrawingMode(#PB_2DDrawing_Transparent))
    Speed + 1
  ForEver
  StopDrawing()
  
  FlipBuffers()
  ClearScreen(0)
  
  If YPos > 480
    DeleteMapElement(Text(), "B")
  EndIf
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space)
    AddMapElement(Text(), "B")
  EndIf
  
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  
ForEver
If i run this script, the program crashed if you press 'space'... also the text is unviseble yet...
So it doesn't works. Just can anyone correct my code, just i can expiriment with the NewMap ...
Function ?..., thanks for help, Jamie.

Re: [2DGame] AddMapElement not works, crached!

Posted: Thu Jun 28, 2018 10:30 am
by Mijikai
JamieVanCadsand wrote:...
So this is my code, writted in pure basic 5.x yet:

Code: Select all

Repeat
    DrawText(XPos, YPos, Text("1"), RGB(255, 255, 255), DrawingMode(#PB_2DDrawing_Transparent))
    Speed + 1
  ForEver
 
After a quick look i noticed that your code
never gets out of the drawing loop.

Re: [2DGame] AddMapElement not works, crached!

Posted: Sun Jul 01, 2018 2:49 pm
by JamieVanCadsand
Now i get this code, but if i run this script, i get an error:

Code: Select all

;Check of 'Sprite' wilt initializeren, zo niet... geeft hij een melding
If InitSprite() = 0
  MessageRequester("Fout!", "Sprites wilt niet initializeren", #PB_MessageRequester_Info)
  End
EndIf

;Check of 'Keyboard' wilt initializeren, zo niet... geeft hij een melding
If InitKeyboard() = 0
  MessageRequester("Fout!", "Keyboard wilt niet initializeren", #PB_MessageRequester_Info)
  End
EndIf

;Check of 'Scherm' wilt initializeren, zo niet... geeft hij een melding
If OpenScreen(640, 480, 32, "Add/Delete Text") = 0
  MessageRequester("Fout!", "Scherm wilt niet initializeren", #PB_MessageRequester_Info)
  End
EndIf




;Maak Structuur Text aan
Structure Texten
  IsNaam.i
  Position.i
EndStructure


Global NewList Namen.Texten.s()
AddElement(Texten())
Namen(0) = "Jan"
Namen(1) = "Kees"
Namen(2) = "Piar"


Procedure MakeText()
  ForEach Namen()
    If Namen()\Position > 480
      DeleteElement(Namen())
      Namen()\Position = -32
    EndIf
  Next Namen()
EndProcedure
    

Repeat
  
  Texts()\Position = -32
  StartDrawing(ScreenOutput())
  DrawText(320, Namen()\Position, Namen(Random(2, 0))\Text, RGB(255, 100, 200), DrawingMode(#PB_2DDrawing_Transparent))
  StopDrawing()
  
  FlipBuffers()
  ClearScreen(0)
  
  MakeText()
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space)
    AddElement(Namen())
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  
ForEver


And this is my error i get:
[15:12:32] [COMPILER] Line 28: Syntax error.
[15:13:45] [COMPILER] Line 37: Comparisons (=, <, >, =< and >=) are only supported with keywords like If, While, Until or within Bool().
[15:14:22] [COMPILER] Line 35: This list doesn't have a 'Structure'.
[15:14:28] [COMPILER] Line 35: This list doesn't have a 'Structure'.
[15:14:43] [COMPILER] Line 23: Garbage at the end of the line.
[15:15:03] [COMPILER] Line 24: Garbage at the end of the line.
[15:15:33] [COMPILER] Line 23: Garbage at the end of the line.
[15:15:59] [COMPILER] Line 23: Garbage at the end of the line.
[15:16:12] [COMPILER] Line 35: This list doesn't have a 'Structure'.
[15:16:39] [COMPILER] Line 23: Garbage at the end of the line.
[15:19:18] [COMPILER] Line 34: Texts_S() is not a function, array, list, map or macro.
[15:20:40] [COMPILER] Line 47: Bad parameter type, number expected instead of string.
[15:21:03] Waiting for executable to start...
[15:21:03] Executable type: Windows - x64 (64bit, Unicode)
[15:21:03] Executable started.
[15:21:12] The debugged executable quit unexpectedly.
[15:21:50] Waiting for executable to start...
[15:21:50] Executable type: Windows - x64 (64bit, Unicode)
[15:21:50] Executable started.
[15:21:59] The debugged executable quit unexpectedly.
[15:29:45] [COMPILER] Line 30: Texts_S() is not a function, array, list, map or macro.
[15:29:51] [COMPILER] Line 31: '()' is expected after a list.
[15:34:05] [COMPILER] Line 28: 'NewList' syntax is: NewList Name().
[15:38:41] [COMPILER] Line 29: 'NewList' syntax is: NewList Name().
[15:38:50] [COMPILER] Line 29: 'NewList' syntax is: NewList Name().
[15:46:43] [COMPILER] Line 29: 'NewList' syntax is: NewList Name().

Can anyone fix my code pleace..., Thanks for help,
Jamie.

Re: [2DGame] AddMapElement not works, crached!

Posted: Sun Jul 01, 2018 3:35 pm
by #NULL

Code: Select all

Global NewList Namen.Texten.s()
Name is the name of the list. Texten is the Type of the list's elements. And .s is nonsense.

Code: Select all

AddElement(Texten())
the name of the list is Namen and not Texten

Code: Select all

Namen(0) = "Jan"
Namen(1) = "Kees"
Namen(2) = "Piar"
It's a list, not an Array so don't use indices. And the elements are of a structure type so you would need to add a string field in the structure and assign a string to that field like Namen()\name = "jan". You would also call AddElement() once for each of the 3 elements.

Code: Select all

Namen(Random(2, 0))\Text
No inidces. Use SelectElement(list()) and/or the lists current element via just 'list()\..'

Code: Select all

  If KeyboardPushed(#PB_Key_Space)
    AddElement(Namen())
there is an EndIf missing, or the next If must be an ElseIf

see how that goes :)

Re: [2DGame] AddMapElement not works, crached!

Posted: Mon Jul 02, 2018 8:49 am
by JamieVanCadsand
Now, i understand the Structure AdElement an little bit, but now i get the following problem...
My ForLoop not works in the
Texten()\L = I
Code...

Code: Select all

Global Dim TextNumber.s(4)

Structure TextenStruct
  IsText.i
  L.i
EndStructure

Global NewList Texten.TextenStruct()

TextNumber(0) = "Jamie van Cadsand"
TextNumber(1) = "Vinnie Orgers"
TextNumber(2) = "Dylan van Eenenaam"
TextNumber(3) = "Liesbeth v.d Jagt"
TextNumber(4) = "Ellister van Hoeven"


;*******************************************************************************************************************
;* CHECK INITIALIZES: **********************************************************************************************
;*******************************************************************************************************************

If InitSprite() = 0
  MessageRequester("Fatal Error", "Failed to Init Sprite", #PB_MessageRequester_Error)
  End
EndIf

If InitKeyboard() = 0
  MessageRequester("Fatal Error", "Failed to Init Keyboard", #PB_MessageRequester_Error)
  End
EndIf

If OpenScreen(800, 600, 32, "") = 0
  MessageRequester("Fatal Error", "Failed to Open Screen", #PB_MessageRequester_Error)
  End
EndIf
;*******************************************************************************************************************
;*******************************************************************************************************************


Repeat
  
  StartDrawing(ScreenOutput())
  ForEach Texten()
    DrawText(320, Texten()\L, TextNumber(Texten()\IsText), RGB(255, 100, 0), DrawingMode(#PB_2DDrawing_Transparent))
  Next Texten()
  StopDrawing()
  
  FlipBuffers()
  ClearScreen(0)
  
  For I = 0 To 480 Step 120
    AddElement(Texten())
    Texten()\IsText = I
    Texten()\L = I
  Next
  
  
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  
ForEver
Can you fix my problem pleace..., my goal is to loop an text underly with an for next and an structure element...
Can anyone correct my code to fix this problem, please?...

Thanks for help, Jamie.

Re: [2DGame] AddMapElement not works, crached!

Posted: Mon Jul 02, 2018 9:08 am
by JamieVanCadsand
I am trying to create an script, called to choote text when you press the 'Space' Key... But if i run this script,
my program don't works...

So this is my script:

Code: Select all

Global Dim TextNumber.s(4)
Global YPos.i = 24

Structure TextenStruct
  IsText.i
EndStructure

Global NewList Texten.TextenStruct()

TextNumber(0) = "Jamie van Cadsand"
TextNumber(1) = "Vinnie Orgers"
TextNumber(2) = "Dylan van Eenenaam"
TextNumber(3) = "Liesbeth v.d Jagt"
TextNumber(4) = "Ellister van Hoeven"


;*******************************************************************************************************************
;* CHECK INITIALIZES: **********************************************************************************************
;*******************************************************************************************************************

If InitSprite() = 0
  MessageRequester("Fatal Error", "Failed to Init Sprite", #PB_MessageRequester_Error)
  End
EndIf

If InitKeyboard() = 0
  MessageRequester("Fatal Error", "Failed to Init Keyboard", #PB_MessageRequester_Error)
  End
EndIf

If OpenScreen(800, 600, 32, "") = 0
  MessageRequester("Fatal Error", "Failed to Open Screen", #PB_MessageRequester_Error)
  End
EndIf
;*******************************************************************************************************************
;*******************************************************************************************************************


Repeat
  
  StartDrawing(ScreenOutput())
  ForEach Texten()
    DrawText(320, YPos, TextNumber(Texten()\IsText), RGB(255, 100, 0), DrawingMode(#PB_2DDrawing_Transparent))
  Next Texten()
  StopDrawing()
  
  FlipBuffers()
  ClearScreen(0)
  
  Texten()\IsText = 0
  
  If YPos > 400
    DeleteElement(Texten())
  EndIf
  
  YPos + 4
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Space)
    AddElement(Texten())
  EndIf
  
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  
ForEver
Can anyone correct my code to give my the just example pleace?.... this works fustrated for me!...
Thanks for code correcting to give my the just code from the code i writted here...

Jamie.

Re: [2DGame] AddMapElement not works, crached!

Posted: Mon Jul 02, 2018 12:44 pm
by infratec
Hi,

your code is also frustrating :wink:

I even don't understand what you exactly want to do with it.

When you add an element, you have also to set the values of this element, else all variables have their default values.
Read the help about AddElement()

Then you try to delete an element, but which one ???
There is none selected since your foreach loop points behind the last element after it is finished.
Read the help about ForEach

Maybe you meant something like this:

Code: Select all

Structure TextenStructure
  YPos.i
  Index.i
EndStructure

Dim TextNumber.s(4)
Define YPos.i
Define Index.i
Define NewList Texten.TextenStructure()

YPos = 24

TextNumber(0) = "Jamie van Cadsand"
TextNumber(1) = "Vinnie Orgers"
TextNumber(2) = "Dylan van Eenenaam"
TextNumber(3) = "Liesbeth v.d Jagt"
TextNumber(4) = "Ellister van Hoeven"


;*******************************************************************************************************************
;* CHECK INITIALIZES: **********************************************************************************************
;*******************************************************************************************************************

If InitSprite() = 0
  MessageRequester("Fatal Error", "Failed to Init Sprite", #PB_MessageRequester_Error)
  End
EndIf

If InitKeyboard() = 0
  MessageRequester("Fatal Error", "Failed to Init Keyboard", #PB_MessageRequester_Error)
  End
EndIf

If OpenScreen(800, 600, 32, "") = 0
  MessageRequester("Fatal Error", "Failed to Open Screen", #PB_MessageRequester_Error)
  End
EndIf
;*******************************************************************************************************************
;*******************************************************************************************************************


Repeat
  
  ClearScreen(0)
  
  If StartDrawing(ScreenOutput())
    
    ForEach Texten()
      DrawText(320, Texten()\YPos, TextNumber(Texten()\Index), RGB(255, 100, 0), DrawingMode(#PB_2DDrawing_Transparent))
    Next Texten()
    
    StopDrawing()
  EndIf
  
  FlipBuffers()
  
  
  ExamineKeyboard()
  If YPos < 400
    If KeyboardReleased(#PB_Key_Space)
      AddElement(Texten())
      Texten()\YPos = YPos
      YPos + 15
      
      Texten()\Index = Index
      Index + 1
      If Index > ArraySize(TextNumber())
        Index = 0
      EndIf
    EndIf
  EndIf
  
  If KeyboardPushed(#PB_Key_Escape)
    End
  EndIf
  
ForEver
But the code itself is not the best solution to achieve something like that.
And if you wanted something like that, then your code was not reflecting this.

Bernd

Re: [2DGame] AddMapElement not works, crached!

Posted: Mon Jul 09, 2018 7:50 pm
by bfernhout
Infratec:
The solution you give is good. I do understand what the good guy try to do. He Is good in programming but some times his thinking is going the wrong way of what PB is caplable to do.
Then i see you are saying that he is looking with the use of

Code: Select all

ForEach
...
next
in the Element list. Beyond the list. THATS NOT TRUE.
When PB is reaching the end of the list and you try to look behind the list. PB keep looking to the last found element. There is never a way PB i looking beyond the list.

E.g.

Code: Select all


Structure Piece
  name.s
  index.l
EndStructure

Global NewList Part.piece()

Procedure FindElement(number)
  ForEach part()
    If part()\index = number
      ProcedureReturn
    EndIf
  Next
 EndProcedure

For l = 0 To 20
  AddElement(Part())
  Part()\name = "Echo " + Str(l)
  part()\index = l
Next

For l = 0 To 21
  Debug part()\name
Next

For l = 0 To 21
  FindElement(l)
  Debug part()\name
Next
When you start the code above the first loop create the list of elements
then the second is just looping giving 22 time the last created element
then the thrith loop is finding the element calling the procedure to look for every element.
When it is at the end of the list the last element is showed twice. That show you that PB never goes beyond the list.

When doing this with a normal loop the variable used for that loop is indeed one higher then the last given number. For a normal For Next loop.But not for Elements list.

Beside this tiny little thing your solution is an big solution for Jamie. Keep up doing the good work.

Bart.

Re: [2DGame] AddMapElement not works, crached!

Posted: Mon Jul 09, 2018 10:52 pm
by Demivec
@bfernhout: You probably understand the things that affect the current element of a list (I know infratec definitely does) but I thought I would modify your last code example to demonstrate some of them a little more clearly to less informed people, perhaps for JamieVanCadsand:

Code: Select all

Structure Piece
  name.s
  index.l
EndStructure

Global NewList Part.piece()

Procedure FindElement(number)
  ForEach part()
    If part()\index = number
      ProcedureReturn ListIndex(part())
    EndIf
  Next
  ProcedureReturn -1 ;no valid index found
 EndProcedure

For l = 0 To 20
  AddElement(Part())
  Part()\name = "Echo " + Str(l)
  part()\index = l
Next

Select  Random(3)
  Case 1
    ResetList(part()) ;set no current element
    Debug "Reset list"
  Case 2
    If ListSize(part()) > 0
      SelectElement(part(), Random(ListSize(part())) - 1) ;select random element #0 to size of list
    Else
      Debug "list is empty"
    EndIf
  Default
    ;leave current element, if there is one, selected
EndSelect

    
Debug "current list element:"
If ListIndex(part()) <> -1
  Debug part()\name ;current element
Else
  Debug "no current element selected"
EndIf



For l = 0 To 21
  If FindElement(l) >= 0
    Debug part()\name
  EndIf
Next

Re: [2DGame] AddMapElement not works, crached!

Posted: Tue Jul 10, 2018 9:26 pm
by bfernhout
Demivec thanks for the extra info.

I am not saying that Infratec is doing somting bad. The only thing I got out of his text was, that he got fustration over the way the program was written. I am not a well trained programmer for PB. I am programming dayle With Siemens and B&R. Who are written in STL and Ladder. Accordingle they are OOP and Time related programming. That is way differend then PB. But i like PB for the game programming and trying to work in Windows.

There for i stumble upone some tiny things like the Element list of structure. In B&R i am used to get a NULL Pointer back when i am going out of reach of the elements. And PB does not do that. Which i also like more. And that is a thing that when you say someting differend you might bring somebody on the wrong leg. And that i did try to correct it.

I accept that a lot of you guy are way better programmers then me in PB. The only benefit I have is that I also give to older people windows training. These people ask quetions you wil not think of. And a lot of times we are stepping over thing we take for granted. Even in PB. So i think i put up my finger to say watch out, this guy is new in PB and do not know all the moves in PB. (Neither do i).

That was what i wanted to say with the example. Which you turned it into a be better one then what i had made.

So if i offend somebody sorry was never my intention. :oops:

Bart.

Re: [2DGame] AddMapElement not works, crached!

Posted: Wed Jul 11, 2018 7:14 am
by #NULL
@bfernhout
If you need the last element or any other, then you should set the position and not rely on a previous loop. If the list might be empty the loop will be safe but the following code would have to check about that for itself. Also if you modify the loop later like adding a break for example you will break the other code that follows etc.

and what crached?
Image
:D

Re: [2DGame] AddMapElement not works, crached!

Posted: Thu Jul 12, 2018 7:49 pm
by bfernhout
NULL:
Its not getting the last element from the list. That is not the problem. When I need a element I search for it in a ForEach loop. But when the element is not in the list. PB keeps the last element as the pointed element. So when the return from the search comes. And i did not checkt out the out of reach sign. The program is using the last element whil it's not that element that i was searching for.

That was a remark because someone suggested that on a search in the list a NULL pointer was giving back. That was the thing i pointed out that was wrong.

Althrough I am not a advanced programmer on PB i do know some things on programming. And because PB has a lot of thing i like and its easy. I did payed for the program (After testing a long time an illigal version) To get the full out of it. But my goal will never to be a skilled programmer but a game programmer that is enjoying to do it.

Bart.

Re: [2DGame] AddMapElement not works, crached!

Posted: Fri Jul 13, 2018 7:56 am
by #NULL
Your remark was certainly valid :)

Re: [2DGame] AddMapElement not works, crached!

Posted: Sat Jul 28, 2018 8:36 pm
by JamieVanCadsand
Thanks all, but now I understand structures or reather said element an lot better...
Thanks for help, Jamie.


Now I plans to try understand programming an lot better, just starts to make games...
Bfernhout is just my hobby programming Teacher (think not thad hè is from school),
Hè said thad I am too focesed on diffren things, just now I want to focus my self on
programming, just starts with make 2d games and later to try complex things of
programming...

BFrenhout, Thanks for our tip just I can focus my self on programming it's self, just starts
with make games and do complexer things like 3D and arduino later...

For the comming time, I plans to make the game 'Zool', called from an game maker book,
just I get at home...

Jamie...