LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Custom sprite

Root / General / [.]

Wubdubb94Created:
How can I make custom sprites using data commands like on Commodore 64

First, I want to answer some of your questions from your previous thread. You asked how to use data blocks. Here's an example code:
RESTORE @MYDATA
READ DATASTRING1$
PRINT DATASTRING1$
READ DATASTRING2$
PRINT DATASTRING2$
READ DATASTRING3$
PRINT DATASTRING3$

@MYDATA
DATA "Hello World!"
DATA "I love data!"
DATA "String #3"
explanationLet's go through each line! RESTORE @MYDATA This command "goes" to the label you gave it. That's all it does. It will be important to use if you have different sections of data. For example, you might have three different levels in your game and you might want to load @LEVEL1, then @LEVEL2, and so on. Make sure you use this command before reading the data. READ DATASTRING1$ This command reads the data and puts it in the variable that you gave it. You can give your variable any name you want, by the way. So after this line runs, the variable "DATASTRING1$" will contain the string "Hello World!" Then, when you use this command again, it will read the next line of data! So "DATASTRING2$" will contain "I love data!". PRINT DATASTRING1$ This command draws the string on your screen. Awesome! @MYDATA This is the label I talked about. With this label, you can tell your commands where to go. DATA "Hello World!" This is the data. It's not really a command; it doesn't do anything. But by using the RESTORE and READ commands, you can retrieve the data and use it!
If you still struggle with this, this website has a search bar in the top-right corner of your screen. You can use it to search for forum posts that talk about what you need. For example, when searching for "DATA", you may find a lot of threads that talk about it! Also, inside of SmileBASIC, you can touch the "?" button on your keyboard to open up the manual. This thing is very very useful, touch it while your cursor is on a command you don't understand and it will tell you what it does!! Try writing these commands in your program and open the manual to learn about them: SPSET SPCHR SPOFS These commands will allow you to draw a sprite and apply changes to it. Once you've learned how to use them, check out the other commands that begin with "SP", you might find some very useful commands :) I think people kinda gave up on trying to help you because you seem to be asking for things that you are not quite ready to understand. Everyone here will tell you this: you should try learning the basic stuff before getting to the more advanced stuff :) Learning about data blocks and drawing normal sprites is a great way to start. Just learn one small thing at a time, and I'm sure you'll be able to figure it out :D

First, I want to answer some of your questions from your previous thread. You asked how to use data blocks. Here's an example code:
RESTORE @MYDATA
READ DATASTRING1$
PRINT DATASTRING1$
READ DATASTRING2$
PRINT DATASTRING2$
READ DATASTRING3$
PRINT DATASTRING3$

@MYDATA
DATA "Hello World!"
DATA "I love data!"
DATA "String #3"
explanationLet's go through each line! RESTORE @MYDATA This command "goes" to the label you gave it. That's all it does. It will be important to use if you have different sections of data. For example, you might have three different levels in your game and you might want to load @LEVEL1, then @LEVEL2, and so on. Make sure you use this command before reading the data. READ DATASTRING1$ This command reads the data and puts it in the variable that you gave it. You can give your variable any name you want, by the way. So after this line runs, the variable "DATASTRING1$" will contain the string "Hello World!" Then, when you use this command again, it will read the next line of data! So "DATASTRING2$" will contain "I love data!". PRINT DATASTRING1$ This command draws the string on your screen. Awesome! @MYDATA This is the label I talked about. With this label, you can tell your commands where to go. DATA "Hello World!" This is the data. It's not really a command; it doesn't do anything. But by using the RESTORE and READ commands, you can retrieve the data and use it!
If you still struggle with this, this website has a search bar in the top-right corner of your screen. You can use it to search for forum posts that talk about what you need. For example, when searching for "DATA", you may find a lot of threads that talk about it! Also, inside of SmileBASIC, you can touch the "?" button on your keyboard to open up the manual. This thing is very very useful, touch it while your cursor is on a command you don't understand and it will tell you what it does!! Try writing these commands in your program and open the manual to learn about them: SPSET SPCHR SPOFS These commands will allow you to draw a sprite and apply changes to it. Once you've learned how to use them, check out the other commands that begin with "SP", you might find some very useful commands :) I think people kinda gave up on trying to help you because you seem to be asking for things that you are not quite ready to understand. Everyone here will tell you this: you should try learning the basic stuff before getting to the more advanced stuff :) Learning about data blocks and drawing normal sprites is a great way to start. Just learn one small thing at a time, and I'm sure you'll be able to figure it out :D
So I was wondering if I could use gpset to set the pixel and then use data to create a sprite like a bitmap could you give me an example on how that would work?

So I was wondering if I could use gpset to set the pixel and then use data to create a sprite like a bitmap could you give me an example on how that would work?
Yes, I could. But I think it would be more helpful to teach you how to do it yourself, so that is what I will do :P You already found an idea on how to do it, with GPSET. Let's enumerate, out loud, the things that will need to happen. 1. We would load the data into some variable 2. Draw a pixel at the current pixel position with GPSET 3. Change the current pixel position 4. Repeat all of this for each piece of data I assume you somewhat understand how to use DATA. So you should already be able to do step 1 :D Step 2 should also be a piece of cake if you know how to use GPSET! If you don't know how to use it, use the manual like I showed you in my last post. For step 3, you will need to change the value of a variable. Do you understand how variables work? And for step 4, we would need a command that repeats parts of the code... Hm... Perhaps the manual might help us find such command? Open the manual while your cursor is on an empty line to open the complete list of commands (in SB4 only). From there, you can scroll left and right to look through different categories of commands. Now, I don't think you'll be able to make it work with just what I gave you here. But I want you to try it anyway, and tell me how far you got. And I want you to have fun doing it :D Here are some more questions you might want to ask yourself... - What does the DATA contain? - What instructions should I give to the GPSET command? - How would I know when I've reached the end of the data? Ok I'm gonna end my post here, pls take your time, no rush, after all this is meant to be a fun experience ^^

Here’s what I did
RESTORE @MYDATA
READ DATA1
PRINT DATA1
READ DATA2
PRINT DATA2

@MYDATA
DATA gpset 1,1-1,2-1,3
DATA gpset,2,1——2,3
and I got an error

GPSET is a command, not data :) The DATA command can only take data, like numbers, or strings (text inside quotation marks).

GPSET is a command, not data :) The DATA command can only take data, like numbers, or strings (text inside quotation marks).
Ok this video should explain what I’m trying to do but I’m trying to see if there’s a way to do this on smile basic without the sprite editor tool https://youtu.be/Tfh0ytz8S0k let me know how this would work after watching the full video

I watched the full video, but I don't understand what you're trying to do... If you want to make a custom sprite/artwork for your game... well, I'm not going to tell you what to do, but... you should really just use the sprite editor like everyone else. It's way more simple! If you want to try creating a sprite the same way as the video because it looks fun to do, then what I've been explaining will allow you to do just that.

Ok I think I figured it out I was looking in the reference manual and i came across gArray not gpset but now I have to figure out how to I assign the pixel cords to data would what you said earlier work if I used gArray?

Yes, that can work too :) Similarly to before... 1. Read the data into some variable 2. Set the array's value at the current pixel coordinates 3. Change the current pixel position 4. Repeat for each piece of data Were you able to make your code from earlier work? It was very good progress, I'd like to know if you were able to make it run without errors. Again, some important questions: Do you understand what you want to put in your DATA? Do you understand how the GARRAY function works? Maybe try using it to change one pixel on the screen, to see if it works the way you think. Do you understand how to use arrays? You definitely won't be able to make it work by yourself if you can't answer these questions ^^ that's why it's so important to go one step at a time

Yes, that can work too :) Similarly to before... 1. Read the data into some variable 2. Set the array's value at the current pixel coordinates 3. Change the current pixel position 4. Repeat for each piece of data Were you able to make your code from earlier work? It was very good progress, I'd like to know if you were able to make it run without errors. Again, some important questions: Do you understand what you want to put in your DATA? Do you understand how the GARRAY function works? Maybe try using it to change one pixel on the screen, to see if it works the way you think. Do you understand how to use arrays? You definitely won't be able to make it work by yourself if you can't answer these questions ^^ that's why it's so important to go one step at a time
I’m fairly new to coding but I know what some commands do like color sets the text color to whatever you choose and gpset sets the pixel to coordinates x,y on the screen and I’ve done those practice guides they have on smilebasic for the switch but otherwise I’ve been trying like you said to store the data into variables

That's good. I think once you'll figure out how to use variables, you'll be able to do a lot more things. Variables are very powerful. There are many kinds of variables, but for now, let's focus on the integer variable and on the string variable.
A=3    'Integer (number)
B="Hi" 'String  (text)
Simply put, you give a value to the variable, and the variable will remember what you gave it. So now you can ask "Hey B, can I have that string of text I just gave you?" and it'll give it back to you :D
PRINT B 'Prints "Hi"
Whenever you use your good friend the variable, it will always remember what you gave it.
C=A     'What's A? A is 3 of course. So now C gets the number 3.
PRINT C 'Prints the number 3
This is very very useful. In the DATA code I showed you in my first post, the READ function gives variables something to remember. So instead of manually giving the variable a value every time, the code does it for us!
READ DATASTRING1  'DATASTRING1 is told to remember the string "Hello world!"
PRINT DATASTRING1 'Prints "Hello world!"
That's why it's important for the DATA commands to only contain numbers and strings. If you give it something else, once the READ command reads your data and tries to give it to the variable, it's going to freak out and give an error, because variables in SmileBASIC can only remember numbers and strings :) Here's a recap of what you need to do, one step at a time: - Make your code read your data and give it to a variable, without any errors - Using the example found in the reference manual, set the array's value at current pixel coordinates
example from the reference manual
G=GARRAY(0)     'Hey G, can you hold this for me? *gives G a massive array of every pixel on screen*
G[0,0]=#C_WHITE 'The pixel at coordinates 0,0 is now white! MUAHAHAHA
GUPDATE 0       'very complex function that you shouldnt worry about, just use it here and move on with your day :)
- Look in the manual for a command that will allow you to repeat all of this for every piece of data

Sorry, it's completely fine if you don't understand the things I'm saying. Instead I'll give you a working example as you've been asking and perhaps you can start learning from there.
'This is where the sprite will be drawn on screen
X=420
Y=69


SPRITEX=0
SPRITEY=0
G=GARRAY(0)

RESTORE @SPRITEDATA
READ SPRITEWIDTH
READ SPRITEHEIGHT

LOOP
 READ DATANUMBER
 IF DATANUMBER==1 THEN G[X+SPRITEX,Y+SPRITEY]=#C_WHITE
 INC SPRITEX
 IF SPRITEX>=SPRITEWIDTH THEN
  SPRITEX=0
  INC SPRITEY
  IF SPRITEY>=SPRITEHEIGHT THEN BREAK
 ENDIF
ENDLOOP
GUPDATE 0

'Example data: the letter E.
'("5,5" at the start is the sprite's size, 1s are a white pixel, 0s are nothing)
@SPRITEDATA
DATA 5,5
DATA 1,1,1,1,1
DATA 1,0,0,0,0
DATA 1,1,1,1,1
DATA 1,0,0,0,0
DATA 1,1,1,1,1

Sorry, it's completely fine if you don't understand the things I'm saying. Instead I'll give you a working example as you've been asking and perhaps you can start learning from there.
'This is where the sprite will be drawn on screen
X=420
Y=69


SPRITEX=0
SPRITEY=0
G=GARRAY(0)

RESTORE @SPRITEDATA
READ SPRITEWIDTH
READ SPRITEHEIGHT

LOOP
 READ DATANUMBER
 IF DATANUMBER==1 THEN G[X+SPRITEX,Y+SPRITEY]=#C_WHITE
 INC SPRITEX
 IF SPRITEX>=SPRITEWIDTH THEN
  SPRITEX=0
  INC SPRITEY
  IF SPRITEY>=SPRITEHEIGHT THEN BREAK
 ENDIF
ENDLOOP
GUPDATE 0

'Example data: the letter E.
'("5,5" at the start is the sprite's size, 1s are a white pixel, 0s are nothing)
@SPRITEDATA
DATA 5,5
DATA 1,1,1,1,1
DATA 1,0,0,0,0
DATA 1,1,1,1,1
DATA 1,0,0,0,0
DATA 1,1,1,1,1
Thanks now that I’m reading your latest example I think I’m starting to understand