Jump to content
Sign in to follow this  
KevKelley

Constants and rand (Using Less bytes)

Recommended Posts

I was trying to find ways to get more space in my game.  I have been avoiding adding additional banks to force myself to learn some tricks, so to speak.  

 

I noticed that when I swapped something like this:

temp5=(rand&3)+20

into this:

const c_R3=rand&3

temp5=(c_R3)+20

it saved 30 bytes but I did not see any difference in performance.

 

I was wondering if anyone understood the reason as to why.  I was looking up through the forums and Random Terrain's site but saw nothing that indicated why.  I assume it is just how it compiles?

Share this post


Link to post
Share on other sites

Now I had noticed that in some instances it would generate the same number each time, like with the temp variables.  Would this because they are wiped each frame and the constant draws up the same number in the same order the next time it is called? 

Share this post


Link to post
Share on other sites

You can't set a const to a random value. What you are doing in this case is taking the memory address of the "rand" declaration, doing a bitwise AND of 3, then adding 20 to the value, so this will have the same value every time. The rand function never gets run in this case.

  • Like 2

Share this post


Link to post
Share on other sites

Ah. I see.  I just went through to see why I thought it was working and I noticed that there was one line where I didn't replace the rand command so it seemed like it was random, but it was just one of the coordinates.  Had I ran it longer I probably would have noticed my mistake when everything would spawn in a column.

 

So I guess the 30 bytes I saved was it not running rand at all then and not some magic trick.  Darn.  I must have missed somewhere where it stated constants cannot set const to a rand value. Thanks!   :thumbsup:

Share this post


Link to post
Share on other sites

I think const and def are more or less doing a text substitution swap before compilation.  Using that assumption not sure how the space savings is happening.

Share this post


Link to post
Share on other sites
Posted (edited)

 

20 minutes ago, Gemintronic said:

I think const and def are more or less doing a text substitution swap before compilation.  Using that assumption not sure how the space savings is happening.

def does, const needs to be a value I guess, not code

Edited by Lillapojkenpåön
  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...